Spring MVC 上传文件

展开阅读全文

Spring MVC 上传文件报错java.net.SocketException: Broken pipe

12-15

首先 我的windows本地是没问题的 跑到linux服务器上才出的问题rn我用的SpringMVCrnspring-servlet.xml配置了rn[code=java]rnrnrnrnrn[/code]rn后台rn[code=java]rn@RequestMapping(value = "/uploadfile/add")rnpublic String add(HttpServletRequest request, UploadFileModel model,rn @RequestParam(value = "myfile") MultipartFile file rn // XXX业务处理rn}rn[/code]rn我试过1M 2M左右及以下的上传没问题的rn超过就不行了rn上面的add方法都进不去rn浏览器反应:rn卡了40秒左右 之后跳出连接被重置rn之后后台报错rn[code=java]rnDec 15, 2015 9:06:59 AM org.apache.jk.core.MsgContext actionrnWARNING: Unable to send headersrnjava.net.SocketException: Broken pipern at java.net.SocketOutputStream.socketWrite0(Native Method)rn at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:113)rn at java.net.SocketOutputStream.write(SocketOutputStream.java:159)rn at org.apache.jk.common.ChannelSocket.send(ChannelSocket.java:532)rn at org.apache.jk.common.JkInputStream.appendHead(JkInputStream.java:326)rn at org.apache.jk.core.MsgContext.action(MsgContext.java:266)rn at org.apache.coyote.Response.action(Response.java:183)rn at org.apache.coyote.Response.sendHeaders(Response.java:379)rn at org.apache.catalina.connector.OutputBuffer.doFlush(OutputBuffer.java:305)rn at org.apache.catalina.connector.OutputBuffer.flush(OutputBuffer.java:288)rn at org.apache.catalina.connector.Response.flushBuffer(Response.java:548)rn at org.apache.catalina.core.StandardHostValve.status(StandardHostValve.java:344)rn at org.apache.catalina.core.StandardHostValve.throwable(StandardHostValve.java:286)rn at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)rn at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)rn at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)rn at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)rn at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:190)rn at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:291)rn at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:769)rn at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:698)rn at org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:891)rn at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:690)rn at java.lang.Thread.run(Thread.java:744)rnrnDec 15, 2015 9:06:59 AM org.apache.jk.common.ChannelSocket processConnectionrnWARNING: processCallbacks status 2rn[/code]rn还有疑问的可以问我rn在线等 论坛

请教spring mvc处理上传文件的问题

10-23

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

没有更多推荐了,返回首页