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中就被拦截 各位 有哪位懂得 给我看看 帮帮忙 谢谢了
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中就被拦截 各位 有哪位懂得 给我看看 帮帮忙 谢谢了
解决方案 »
- Java_Web_应用开发项目-移动公司计费账务管理系统
- 关于iReport中子报表中交叉表的问题 在线等
- java里面怎么求两个日期之间的天数?
- 发起团购:基于SSH的大型人力资源管理系统全程开发课程
- structs中遇到的logic:equal问题
- 做过web开发,特别是网站开发的,来讨论一下你们采用什么方式来使用头部和底部页面?
- 【jsp求助】java.lang.NumberFormatException: null【跪谢】
- 数据库查询后返回多条记录,如何在页面中展现出来?
- 想做一个jsp的编辑器,用vc,请大家提供意见、资料。谢谢!
- 帮忙看看这个程序怎么回事?
- Java菜鸟提问
- 求教HTML CSS中的问题,请各位大神帮忙~
------------------------------8cd19635feab242
Content-Disposition: form-data; name="file0"; filename="e:\xx\Release\user\logo.jpg"
Content-Type: application/octet-stream<文件二进制内容>
.append("\"; filename=\"").append(fileName).append("\"\r\n")
.append("Content-Type: ").append(fileType).append("\r\n\r\n");