httpURLConnection 模拟post请求 发送图片 在Action中怎么接? public  String uploadBitmap1(String urlString,byte[] imageBytes){
String endString = "\r\n";
String twoHyphen = "--";
String boundary = "*****";

try {
URL url = new URL(urlString);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
//con.setRequestProperty("content-type", "text/html");
//允许input、Output,不使用Cache
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);

//设置传送的method=POST
con.setRequestMethod("POST");

//setRequestProperty
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "utf-8");
con.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

//设置DataOutputStream
DataOutputStream dsDataOutputStream = new DataOutputStream(con.getOutputStream());
dsDataOutputStream.writeBytes(twoHyphen + boundary + endString);
dsDataOutputStream.writeBytes("Content-Disposition:form-data;" + "name=\"upload\";filename=\"" +
"11.jpg\"" + endString);
dsDataOutputStream.writeBytes(endString);

//取得文件的FileInputStream
dsDataOutputStream.write(imageBytes,0,imageBytes.length);

dsDataOutputStream.writeBytes(endString);
dsDataOutputStream.writeBytes(twoHyphen + boundary + twoHyphen + endString); dsDataOutputStream.close();

int cah = con.getResponseCode();
if(cah == 200){
InputStream isInputStream = con.getInputStream();
int ch;
StringBuffer buffer = new StringBuffer();
while((ch = isInputStream.read()) != -1){
buffer.append((char)ch);
}
return buffer.toString();
}else{
return "false";
}
} catch (Exception e) {
e.printStackTrace();
}
return "false";
}服务器 action中代码final long MAX_SIZE = 3 * 1024 * 1024;// 设置上传文件最大为 3M

HttpServletRequest request = ServletActionContext.getRequest (); 
DiskFileItemFactory dfif = new DiskFileItemFactory();
  dfif.setSizeThreshold(4096);// 设置上传文件时用于临时存放文件的内存大小,这里是4K.多于的部分将临时存在硬盘
  dfif.setRepository(new File(request.getRealPath("/")
    + "ImagesUploadTemp"));// 设置存放临时文件的目录,web根目录下的ImagesUploadTemp目录   // 用以上工厂实例化上传组件
  ServletFileUpload sfu = new ServletFileUpload(dfif);
  // 设置最大上传尺寸
  sfu.setSizeMax(MAX_SIZE);
  // 从request得到 所有 上传域的列表
  System.out.println();
  List fileList = null;
  try {
   fileList = sfu.parseRequest(request);
  } catch (FileUploadException e) {// 处理文件尺寸过大异常    e.printStackTrace();
  }接受不到啊 
异常信息
2010-09-02 17:23:58 [org.apache.struts2.interceptor.FileUploadInterceptor]-[ERROR] Could not find a Content-Type for upload. Verify that a valid file was submitted.因为我需要在手机上上传图片 要在网站中接受 可是上面的代码如果在Servlet中就可以接受到文件可是在Action中就被拦截 各位 有哪位懂得 给我看看 帮帮忙 谢谢了

解决方案 »

  1.   

    http://www.javaeye.com/problems/22318
      

  2.   

    我上传的是的image图像 这个不是类型问题
      

  3.   

    multipart/form-data的一个文件段应该大致如下,检查下吧,你构造的段落应该是不符合标准的。缺少Content-Type。
    ------------------------------8cd19635feab242
    Content-Disposition: form-data; name="file0"; filename="e:\xx\Release\user\logo.jpg"
    Content-Type: application/octet-stream<文件二进制内容>
      

  4.   

    参考buff.append("Content-Disposition: form-data; name=\"").append(fileField)
        .append("\"; filename=\"").append(fileName).append("\"\r\n")
        .append("Content-Type: ").append(fileType).append("\r\n\r\n");
      

  5.   

    参考:http://lapulande.javaeye.com/blog/719581