嵌套异常为java.lang.IllegalArgumentException:大小不能大于Integer最大值:5410783931

时间:2018-08-15 07:27:02

标签: java spring download

正在下载文件,该如何解决?

我的代码:

@RequestMapping("/download")
public ResponseEntity
<byte[]> download(HttpServletRequest request, @RequestParam("filename") String filename, Model model) throws Exception {
String path = request.getServletContext().getRealPath("/avatars/");

filename = new String(filename.getBytes("UTF-8"), "UTF-8");

File file = new File(path + File.separator + filename);

HttpHeaders headers = new HttpHeaders();

String downloadFileName = new String(filename.getBytes("UTF-8"), "ISO-8859-1");

headers.setContentDispositionFormData("attachment", downloadFileName);

headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);

return new ResponseEntity
<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);

}

我的xml:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="defaultEncoding">
        <value>UTF-8</value>
    </property>
</bean>

页面错误:

HTTP Status 500 – Internal Server Error
Type Exception Report
Message Request processing failed; nested exception is java.lang.IllegalArgumentException: Size cannot be greater than Integer max value: 5410783931
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: Size cannot be greater than Integer max value: 5410783931
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
Root Cause
java.lang.IllegalArgumentException: Size cannot be greater than Integer max value: 5410783931
org.apache.commons.io.IOUtils.toByteArray(IOUtils.java:467)
org.apache.commons.io.FileUtils.readFileToByteArray(FileUtils.java:1675)
com.demo.controller.UploadController.download(UploadController.java:86)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
Note The full stack trace of the root cause is available in the server logs.
Apache Tomcat/8.5.29

消息错误: 消息请求处理失败;嵌套异常是java.lang.IllegalArgumentException:大小不能大于Integer最大值:5410783931

例外 org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常为java.lang.IllegalArgumentException:大小不能大于Integer最大值:5410783931 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)

1 个答案:

答案 0 :(得分:0)

感谢您帮助我解决问题。

我正确的解决方法是:

 InputStreamResource isr = new InputStreamResource(new FileInputStream(file));
 return new ResponseEntity<InputStreamResource>(isr, headers, HttpStatus.CREATED);

请勿使用字节,仅使用InputStreamResource可以下载2G以上的文件。