2015-11-04 52 views
0

我有一个应用程序上传视频文件到我的服务器,但最后一次尝试加载一个4GB的视频文件,上传文件只有368Mb总共4GB。麻烦上传大文件到tomcat与弹簧

这是控制器的代码。

@RequestMapping(value = "/uploadFile", method = RequestMethod.POST) 
public @ResponseBody List<String> handleFileUpload(@RequestParam("idClient") String idClient, 
     @RequestParam("idChannel") String idChannel, @RequestParam("idPlayList") String idPlayList, 
     MultipartHttpServletRequest request, HttpServletResponse response) { 

    StringBuffer sb = new StringBuffer(); 
    List<String> response = new ArrayList<String>(); 
    Iterator<String> itr = request.getFileNames(); 
    MultipartFile file = null; 

    while (itr.hasNext()) { 

     file = request.getFile(itr.next()); 

     if (!file.isEmpty()) { 

      sb.setLength(0); 

      sb.append(tmpFolder).append(idClient).append("_").append(idChannel).append("_").append(idPlayList) 
        .append("_").append(file.getOriginalFilename()); 

      try { 

       byte[] bytes = file.getBytes(); 

       BufferedOutputStream stream = new BufferedOutputStream(
         new FileOutputStream(new File(sb.toString()))); 

       stream.write(bytes); 

       stream.close(); 

       response.add("File " + file.getOriginalFilename() + " uploading."); 

      } catch (IOException e) { 

       response.add("File " + file.getOriginalFilename() + " not upload."); 

      } 

     } else { 

      response.add("File no subido " + file.getOriginalFilename() + " file is empty ! ."); 

     } 

    } 

    return response; 
} 

这会发生吗?

谢谢你的帮助!

+1

你排除tomcat配置是问题吗? http://tecadmin.net/increase-tomcat-upload-file-size-limit/ –

+0

它没有工作把参数,但仍然只收到4GB的368Mb,但不会给我任何错误,不只是总文件的副本。 –

+0

你可以发布包含节点的tomcat web.xml部分吗?确保输入的值是以字节为单位的,所以如果我没有被互联网误导,那么4GB的值就是4294967296.如果Tomcat运行的是32位,那将是限制。如果Tomcat的磁盘空间不足,我不确定你是否会得到一个错误....只是一个想法。 –

回答

0

我解决了!谢谢 问题是,当你加载一个超过2GB的文件超过字节数组的最大大小时,可以支持的原因是这个文件没有完全解决,这就创建了一个缓冲区,将文件读入块或块。

public void copyPartsBufferFile(InputStream input, Integer bufferSize, String fullPathDetiny) { 

BufferedOutputStream stream = null; 

    try { 

     stream = new BufferedOutputStream(new FileOutputStream(new File(fullPathDetiny))); 

     ReadableByteChannel rbc = Channels.newChannel(input); 
     ByteBuffer buffer = ByteBuffer.allocate(bufferSize); 
     byte[] bytes; 

     while (rbc.read(buffer) >= 0) { 
      buffer.flip(); 
      bytes = buffer.array(); 
      stream.write(bytes); 
      buffer.clear(); 
     } 

    } catch (FileNotFoundException e) { 

    } catch (IOException e) { 

    } catch (Exception e) { 

    } finally { 

    } 

} 

我离开我的代码,以防有人发生相同的情况!

问候。