怎么构造表单上传图片给基于Struts2的服务器?

coffeesweet 2016-02-05 02:39:42
这几天在做图片上传,安卓、IOS客户端和SSH的服务器交互,想用客户端构造表单的方法提交。在客户端构造一个form表单然后用Struts2框架接收,可是试了半天,Struts2就是接收不到数据,一直显示file类型空指针,但是可以跳转。
想问问,构造表单提交上传图片的方式为什么Struts2框架接收不到?
或是其他图片上传的方法也请指教!debug几天了
这是struts.xml配置文件上传的部分

我是用Struts2框架自带的获取文件的方法,下面是定义多文件上传用的变量数组,已经有getter和setter了。

然后我就用post构造表单提交的,传入url能跳转,就是接收不到文件然后报空指针,

/**
* 直接通过HTTP协议提交数据到服务器,实现表单提交功能
* @param actionUrl 上传路径
* @param file 上传文件
*/
public static String post(String actionUrl,FormFile[] files) {
try {
String BOUNDARY = "----WebKitFormBoundaryBnnaepxTxlPONaHC"; //数据分隔线
String MULTIPART_FORM_DATA = "multipart/form-data";

URL url = new URL(actionUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);//允许输入
conn.setDoOutput(true);//允许输出
conn.setUseCaches(false);//不使用Cache
conn.setRequestMethod("POST");
conn.setRequestProperty("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Length", "640619");
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Content-Type", MULTIPART_FORM_DATA + "; boundary=" + BOUNDARY);
conn.setRequestProperty("user-agent",
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36");

DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());

//上传的文件部分,格式请参考文章
for(FormFile file : files){
StringBuilder split = new StringBuilder();
split.append("-");
split.append(BOUNDARY);
split.append("/r/n");
split.append("Content-Disposition: form-data; name=\""+ file.getFormname()+"\"; filename=\""+ file.getFilname() + "\"/r/n");
split.append("Content-Type: "+ file.getContentType()+"/r/n/r/n");
System.out.println(split.toString());
outStream.write(split.toString().getBytes());
outStream.write(file.getData(), 0, file.getData().length);
outStream.write("/r/n".getBytes());
}
byte[] end_data = ("--" + BOUNDARY + "--/r/n").getBytes();//数据结束标志
outStream.write(end_data);
outStream.flush();
int cah = conn.getResponseCode();
if (cah != 200) throw new RuntimeException("请求url失败");
InputStream is = conn.getInputStream();
int ch;
StringBuilder b = new StringBuilder();
while( (ch = is.read()) != -1 ){
b.append((char)ch);
}
outStream.close();
conn.disconnect();
return b.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

class FormFile {
/* 上传文件的数据 */
private byte[] data;
/* 文件名称 */
private String filname;
/* 表单字段名称*/
private String formname;
/* 内容类型 */
private String contentType = "application/octet-stream"; //需要查阅相关的资料

public FormFile(byte[] data, String formname,String filname,String contentType) {
this.data = data;
this.filname = filname;
this.formname = formname;
if(contentType!=null) this.contentType = contentType;
}

public byte[] getData() {
return data;
}

public void setData(byte[] data) {
this.data = data;
}

public String getFilname() {
return filname;
}

public void setFilname(String filname) {
this.filname = filname;
}

public String getFormname() {
return formname;
}

public void setFormname(String formname) {
this.formname = formname;
}

public String getContentType() {
return contentType;
}

public void setContentType(String contentType) {
this.contentType = contentType;
}

}
...全文
281 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
coffeesweet 2016-03-26
  • 打赏
  • 举报
回复
没有人回答就这样了
coffeesweet 2016-03-05
  • 打赏
  • 举报
回复
引用 2 楼 zpq19870824 的回复:
如果是http上传 可以使用AsyncHttpClient框架 上传图片很简单 先压缩在上传
可是有个问题,我用servlet接收的话构造表单传输图片就能成功,但是Struts2就不行,Struts2有哪里不一样吗?
_周星星 2016-02-15
  • 打赏
  • 举报
回复
如果是http上传 可以使用AsyncHttpClient框架 上传图片很简单 先压缩在上传
coffeesweet 2016-02-05
  • 打赏
  • 举报
回复
帮忙看看啊 或者建议其他客户端图片上传的方法也行啊!!!!

80,359

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧