开发者社区> 问答> 正文

spring Boot上传excel文件报错。 1、http请求 2、上传类Mu?400报错

spring Boot上传excel文件报错。 1、http请求 2、上传类MultipartConfigFactory 设置? 400 报错

spring boot上传excel文件时报错(本地可以,线上报错),请大神解决...谢谢

问题见下描述

1、http请求

@RequestMapping("/batchAddPermissionVehicle")
@ResponseBody
public ResponseBase<Object> batchAddPermissionVehicle(MultipartFile file, @Param("userid") Integer userid) {
    ResponseBase<Object> res = new ResponseBase<>();
    HashMap<String, Object> data = new HashMap<>();
    int count = 0;
    try {
        String excelFileName = file.getOriginalFilename();
        boolean is03Excel = excelFileName.matches("^.+\\.(?i)(xls)$");
        //1、读取工作簿
        Workbook workbook = is03Excel ? new HSSFWorkbook(file.getInputStream()) : new XSSFWorkbook(file.getInputStream());
        //2、读取工作表
        Sheet sheet = workbook.getSheetAt(0);
        //3、读取行
        data.put("count", count);
        res.setResult("true");
        res.setData(data);
        res.setMessage("数据导入成功,本次导入" + count + "条数据");
        // workbook.close();
        file.getInputStream().close();
    } catch (Exception e) {
        data.put("count", count);
        res.setResult("false");
        res.setData(data);
        res.setMessage("数据导入失败,本次导入" + count + "条数据");
        e.printStackTrace();
    }
    return res;
}

2、上传类MultipartConfigFactory 设置

@Bean
    public MultipartConfigElement multipartConfigElement(){
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //文件最大KB,MB
        factory.setMaxFileSize("10MB");
        //设置总上传数据总大小
        factory.setMaxRequestSize("15MB");
        return factory.createMultipartConfig();
    }

3、线上服务器报错:

SEVERE: Servlet.service() for servlet [dispatcherServlet] 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 java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException] with root cause
java.net.SocketTimeoutException
        at org.apache.tomcat.util.net.NioBlockingSelector.read(NioBlockingSelector.java:201)
        at org.apache.tomcat.util.net.NioSelectorPool.read(NioSelectorPool.java:235)
        at org.apache.tomcat.util.net.NioSelectorPool.read(NioSelectorPool.java:216)
        at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.fillReadBuffer(NioEndpoint.java:1241)
        at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.read(NioEndpoint.java:1190)
        at org.apache.coyote.http11.Http11InputBuffer.fill(Http11InputBuffer.java:717)
        at org.apache.coyote.http11.Http11InputBuffer.access$300(Http11InputBuffer.java:40)
        at org.apache.coyote.http11.Http11InputBuffer$SocketInputBuffer.doRead(Http11InputBuffer.java:1072)
        at org.apache.coyote.http11.filters.IdentityInputFilter.doRead(IdentityInputFilter.java:140)
        at org.apache.coyote.http11.Http11InputBuffer.doRead(Http11InputBuffer.java:261)
        at org.apache.coyote.Request.doRead(Request.java:581)
        at org.apache.catalina.connector.InputBuffer.realReadBytes(InputBuffer.java:326)
        at org.apache.catalina.connector.InputBuffer.checkByteBufferEof(InputBuffer.java:642)
        at org.apache.catalina.connector.InputBuffer.read(InputBuffer.java:349)
        at org.apache.catalina.connector.CoyoteInputStream.read(CoyoteInputStream.java:183)
        at java.io.FilterInputStream.read(FilterInputStream.java:133)
        at org.apache.tomcat.util.http.fileupload.util.LimitedInputStream.read(LimitedInputStream.java:132)
        at org.apache.tomcat.util.http.fileupload.MultipartStream$ItemInputStream.makeAvailable(MultipartStream.java:977)
        at org.apache.tomcat.util.http.fileupload.MultipartStream$ItemInputStream.read(MultipartStream.java:881)
        at java.io.InputStream.read(InputStream.java:101)
        at org.apache.tomcat.util.http.fileupload.util.Streams.copy(Streams.java:98)
        at org.apache.tomcat.util.http.fileupload.util.Streams.copy(Streams.java:68)
        at org.apache.tomcat.util.http.fileupload.MultipartStream.readBodyData(MultipartStream.java:571)
        at org.apache.tomcat.util.http.fileupload.MultipartStream.discardBodyData(MultipartStream.java:595)
        at org.apache.tomcat.util.http.fileupload.MultipartStream.skipPreamble(MultipartStream.java:613)
        at org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl.findNextItem(FileUploadBase.java:865)
        at org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:845)
        at org.apache.tomcat.util.http.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:256)
        at org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:280)
        at org.apache.catalina.connector.Request.parseParts(Request.java:2864)
        at org.apache.catalina.connector.Request.parseParameters(Request.java:3211)
        at org.apache.catalina.connector.Request.getParameter(Request.java:1137)
        at org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:381)
        at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:75)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:106)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Thread.java:748)

 

 

 

 

展开
收起
爱吃鱼的程序员 2020-06-02 13:43:46 893 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    没遇到过这个错误 帮顶 看大佬怎么说

    ######

    去查看引入的包是否正确,上传的文件应该引用这个包org.springframework.web.multipart.MultipartFile

    ######回复 @北巷远人 : 感觉是上传文件超时导致的######检查过引用的包,应该是没有问题的,这个只是在线上(部署在linux服务中)会报此错误,谢谢...######

    SokcetTimeout和代理层有关,也和TOMCAT有关,上线以后,生产用户的网速就可能没那么好了,如果连接在指定时间内没有完成操作就很容易出现SocketTimeout

    ######明白,谢谢回复######

    1、检查带宽

    2、加大tomcat的连接超时时间

    ######好的,我尝试一下######

    我也遇到这个问题了,本地没问题,开发/测试环境(linux)就报错,请问大佬,这个问题是怎么解决的了?

    ######

    请问大佬,这个问题是怎么解决的了?紧急求助,在线等。。。

    ######抱歉,刚看到。这个问题我没有找到好的解决方法,应该是开发环境中的网络问题,我根据我的业务换了一种方法,如果有更好的方法,欢迎交流。
    2020-06-02 13:44:04
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载