请教spring mvc处理上传文件的问题
在delicacy-serlvet.xml中添加了:
<!-- 文件上传 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >
<property name="uploadTempDir" value="/uploadTmp"/>
</bean>
调试的时候服务端代码items = upload.parseRequest(request);得到的结果一直为0.
客户端代码:
/**
* @param 只发送普通数据,调用此方法
* @param urlString 对应的Php 页面
* @param params 需要发送的相关数据 包括调用的方法
* @param imageuri 图片或文件手机上的地址 如:sdcard/photo/123.jpg
* @param img 图片名称
* @return Json
*/
public String uploadFile(String uploadUrl,Map<String, Object> params,String imageuri ,String img){
String result="";
String end = "\r\n";
String MULTIPART_FORM_DATA = "multipart/form-data";
String BOUNDARY = "---------7d4a6d158c9"; //数据分隔线
String imguri ="";
if (!imageuri.equals("")) {
imguri = imageuri.substring(imageuri.lastIndexOf("/") + 1);//获得图片或文件名称
}
try {
URL url = new URL(uploadUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);//允许输入
conn.setDoOutput(true);//允许输出
conn.setUseCaches(false);//不使用Cache
conn.setConnectTimeout(60000);// 6秒钟连接超时
conn.setReadTimeout(60000);// 6秒钟读数据超时
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Content-Type", MULTIPART_FORM_DATA + "; boundary=" + BOUNDARY);
StringBuilder sb = new StringBuilder();
//上传的表单参数部分,格式请参考文章
if(params!=null)
{
for (Map.Entry<String, Object> entry : params.entrySet()) {//构建表单字段内容
sb.append("--");
sb.append(BOUNDARY);
sb.append("\r\n");
sb.append("Content-Disposition: form-data; name=""+ entry.getKey() + ""\r\n\r\n");
sb.append(entry.getValue());
sb.append("\r\n");
}
}
//
sb.append("--");
sb.append(BOUNDARY);
sb.append("\r\n");
sb.append("Content-Disposition: form-data; name=""+ "userName" + ""\r\n\r\n");
sb.append("lindy");
sb.append("\r\n");
//
sb.append("--");
sb.append(BOUNDARY);
sb.append("\r\n");
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.write(sb.toString().getBytes());
if (!imageuri.equals("")&&!imageuri.equals(null)) {
dos.writeBytes("Content-Disposition: form-data; name=""+img+""; filename="" + imguri + """ + "\r\n"+"Content-Type: image/jpeg\r\n\r\n");
FileInputStream fis = new FileInputStream(imageuri);
byte[] buffer = new byte[1024]; // 8k
int count = 0;
while ((count = fis.read(buffer)) != -1)
{
dos.write(buffer, 0, count);
}
dos.writeBytes(end);
fis.close();
}
dos.writeBytes("--" + BOUNDARY + "--\r\n");
dos.flush();
InputStream is = conn.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
result = br.readLine();
}catch (Exception e) {
result = "{"ret":"898"}";
}
return result;
}
@RequestMapping(value = "/uploadFiles", method = RequestMethod.POST)
public void uploadFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
Map<String,Object> map = this.getParameter(request);
PrintWriter out = null;
response.setContentType("text/xml;charset=UTF-8");
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
File directory = null;
List items = new ArrayList();
try {
items = upload.parseRequest(request);//得到的结果一直为0.
// 得到所有的文件
Iterator<FileItem> it = items.iterator();
while (it.hasNext()) {
} catch (Exception e) {
System.out.println("读取http请求属性值出错!");
//e.printStackTrace();
logger.error("读取http请求属性值出错");
map.put("ack", 607);
writeUtf8Xml(response,XmlRequestParser.getInstance().mapToXml(map, "uploadFile"));
return;
}
// 数据处理
writeUtf8Xml(response,XmlRequestParser.getInstance().mapToXml(map, "uploadFile"));
return;
}