上传文件停止,并在套接字Exception上读取意外的EOF

有时当我尝试在远程vps上传文件时,我得到此exception(上传进程停止在60%)

06-Jan-2016 11:59:36.801 SEVERE [http-nio-54000-exec-9] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [mvc-dispatcher] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Unexpected EOF read on the socket] with root cause java.io.EOFException: Unexpected EOF read on the socket 

Google Chrome ,连接丢失就像服务器关闭一样,我得到了ERR_CONNECTION_ABORTED

我在spring mvc上传这样的文件

 public void save_file(MultipartFile upfile , String path){ try { File fichier = new File( path ) ; byte[] bytes = upfile.getBytes(); BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream( fichier )); stream.write(bytes); stream.close(); System.out.println( "You successfully uploaded " + upfile.getOriginalFilename() + "!" ); } catch (Exception e) { System.out.println( "You failed to upload " + upfile.getOriginalFilename() + " => " + e.getMessage() ); ; } } 

我的控制器:

 @RequestMapping(value = "/administration/upload", method = RequestMethod.POST) public String Upload_AO_journal( @ModelAttribute UploadForm uploadForm, Model map , HttpServletRequest request, HttpSession session ) throws ParseException, UnsupportedEncodingException { 

我的豆子

 public class UploadForm { ... public MultipartFile scan; 

那怎么能解决这个问题呢?

你试过流吗?

Jsp代码:

控制器:

  @RequestMapping( value = "url", method = RequestMethod.POST ) public void uploadFile( @RequestParam("file") MultipartFile file ) throws IOException { InputStream input = upfile.getInputStream(); Path path = Paths.get(path);//check path OutputStream output = Files.newOutputStream(path); IOUtils.copy(in, out); //org.apache.commons.io.IOUtils or you can create IOUtils.copy } 

所有这些都适用于我的弹簧4.0和弹簧安全。

其次,您应该检查http连接是否超时。 Chrome不支持该配置。 所以你可以使用firefox并点击这里http://morgb.blogspot.com.es/2014/05/firefox-29-and-http-response-timeout.html 。

出现此问题是因为您关闭流直到流写入整个数据。

错误的方法:

 stream.write(bytes); stream.close(); 

正确的方法:

 try (BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(fichier))) { stream.write(data); } 

您应该在写完整个数据后关闭流。

不确定upfile上的getBytes()方法。 更合适的方法是使用InputStream来管理任何大小的文件,并在必要时进行缓冲。 就像是:

 public void save_file(MultipartFile upfile , String path){ try { File fichier = new File( path ) ; BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream( fichier )); InputStream is = upfile.getInputStream(); byte [] bytes = new byte[1024]; int sizeRead; while ((sizeRead = is.read(bytes,0, 1024)) > 0) { stream.write(bytes, 0, sizeRead); } stream.flush(); stream.close(); System.out.println( "You successfully uploaded " + upfile.getOriginalFilename() + "!" ); } catch (Exception e) { System.out.println( "You failed to upload " + upfile.getOriginalFilename() + " => " + e.getMessage() ); ; } } 

我有类似的问题,问题是当你上传文件时,你使用的是Multipart Form POST Request。 您可以阅读有关MIME的信息 。

 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary=frontier This is a message with multiple parts in MIME format. --frontier Content-Type: text/plain This is the body of the message. --frontier Content-Type: application/octet-stream Content-Transfer-Encoding: base64 PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUg Ym9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg== --frontier-- 

基本上我遇到的问题是来自请求的多部分有元信息部分,文本部分和实际文件编码我相信base64。 每个部分都按边界分割。 如果您没有设置此边界(在上面的示例中称为“边界”),则服务器开始正在读取文件,但在到达EOF之前不知道它的结束位置并返回有关意外EOF的错误,因为它未找到所需边界。

您的代码问题在于您正在将文件写入ByteOutputStream,这适用于将文件从服务器返回给用户而不是其他方式。 服务器期望此Multipart请求具有一些标准的预定义格式。 使用Apache Commons HttpClient库为您完成所有这些请求格式化和边界设置。 如果你使用Maven那么这个链接 。

 File f = new File("/path/fileToUpload.txt"); PostMethod filePost = new PostMethod("http://host/some_path"); Part[] parts = { new StringPart("param_name", "value"), new FilePart(f.getName(), f) }; filePost.setRequestEntity( new MultipartRequestEntity(parts, filePost.getParams()) ); HttpClient client = new HttpClient(); int status = client.executeMethod(filePost); 

免责声明:代码不是我的代码来自Apache网站的多部分请求