收藏
回答

上传文件如何调试,开发工具下正常,上传体验版不正常?

1,在开发工具下,用测试号连接后台服务器,上传文件正常,上传多少次都正常;

2,在1)的条件下,用真机调试,第一次上传正常,第二次上传,开发工具端输出信息正常,但服务器报错

3,小程序上传到管理平台,用体验版测试,第一次上传正常,第二次以后报错,和2)报错信息一样。后台服务器重启后,又能正常上传一次。

服务端错误信息如下:

org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly

at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:350) ~[commons-fileupload-1.4.jar!/:1.4]

at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:113) ~[commons-fileupload-1.4.jar!/:1.4]

at com.yingxin.wxbackserver.controller.UploadController.uploadPicture(UploadController.java:55) ~[classes!/:0.0.1-SNAPSHOT]

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_171]

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_171]

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_171]

at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_171]

at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) [spring-web-5.2.4.RELEASE.jar!/:5.2.4.RELEASE]

at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) [spring-web-5.2.4.RELEASE.jar!/:5.2.4.RELEASE]

at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106) [spring-webmvc-5.2.4.RELEASE.jar!/:5.2.4.RELEASE]

at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879) [spring-webmvc-5.2.4.RELEASE.jar!/:5.2.4.RELEASE]

at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793) [spring-webmvc-5.2.4.RELEASE.jar!/:5.2.4.RELEASE]

at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) [spring-webmvc-5.2.4.RELEASE.jar!/:5.2.4.RELEASE]

at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) [spring-webmvc-5.2.4.RELEASE.jar!/:5.2.4.RELEASE]

at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) [spring-webmvc-5.2.4.RELEASE.jar!/:5.2.4.RELEASE]

at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) [spring-webmvc-5.2.4.RELEASE.jar!/:5.2.4.RELEASE]

at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) [spring-webmvc-5.2.4.RELEASE.jar!/:5.2.4.RELEASE]

at javax.servlet.http.HttpServlet.service(HttpServlet.java:660) [tomcat-embed-core-9.0.31.jar!/:9.0.31]

at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) [spring-webmvc-5.2.4.RELEASE.jar!/:5.2.4.RELEASE]

at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) [tomcat-embed-core-9.0.31.jar!/:9.0.31]

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [tomcat-embed-core-9.0.31.jar!/:9.0.31]

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.31.jar!/:9.0.31]

at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) [tomcat-embed-websocket-9.0.31.jar!/:9.0.31]

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.31.jar!/:9.0.31]

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.31.jar!/:9.0.31]

at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) [spring-web-5.2.4.RELEASE.jar!/:5.2.4.RELEASE]

at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.4.RELEASE.jar!/:5.2.4.RELEASE]

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.31.jar!/:9.0.31]

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.31.jar!/:9.0.31]

at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) [spring-web-5.2.4.RELEASE.jar!/:5.2.4.RELEASE]

at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.4.RELEASE.jar!/:5.2.4.RELEASE]

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.31.jar!/:9.0.31]

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.31.jar!/:9.0.31]

at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) [spring-web-5.2.4.RELEASE.jar!/:5.2.4.RELEASE]

at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.4.RELEASE.jar!/:5.2.4.RELEASE]

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.31.jar!/:9.0.31]

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.31.jar!/:9.0.31]

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) [tomcat-embed-core-9.0.31.jar!/:9.0.31]

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.31.jar!/:9.0.31]

at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) [tomcat-embed-core-9.0.31.jar!/:9.0.31]

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.31.jar!/:9.0.31]

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.31.jar!/:9.0.31]

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.31.jar!/:9.0.31]

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.31.jar!/:9.0.31]

at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367) [tomcat-embed-core-9.0.31.jar!/:9.0.31]

at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.31.jar!/:9.0.31]

at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-9.0.31.jar!/:9.0.31]

at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1639) [tomcat-embed-core-9.0.31.jar!/:9.0.31]

at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.31.jar!/:9.0.31]

at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_171]

at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_171]

at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.31.jar!/:9.0.31]

at java.lang.Thread.run(Unknown Source) [na:1.8.0_171]

Caused by: org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly

at org.apache.commons.fileupload.MultipartStream$ItemInputStream.makeAvailable(MultipartStream.java:1033) ~[commons-fileupload-1.4.jar!/:1.4]

at org.apache.commons.fileupload.MultipartStream$ItemInputStream.read(MultipartStream.java:931) ~[commons-fileupload-1.4.jar!/:1.4]

at java.io.InputStream.read(Unknown Source) ~[na:1.8.0_171]

at org.apache.commons.fileupload.util.Streams.copy(Streams.java:98) ~[commons-fileupload-1.4.jar!/:1.4]

at org.apache.commons.fileupload.util.Streams.copy(Streams.java:68) ~[commons-fileupload-1.4.jar!/:1.4]

at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:346) ~[commons-fileupload-1.4.jar!/:1.4]

... 52 common frames omitted



回答关注问题邀请回答
收藏

2 个回答

  • 李忠
    李忠
    2020-04-16

    用postman测试无数次都正常

    2020-04-16
    有用
    回复
  • Mr.Zhao
    Mr.Zhao
    2020-04-16

    前后端的代码都亮一下

    2020-04-16
    有用
    回复 14
    • 李忠
      李忠
      2020-04-16
      就是:List<FileItem> list = upload.parseRequest(request);这句抛出的异常
      2020-04-16
      回复
    • 李忠
      李忠
      2020-04-16
      const uploadFile = function (filefullpath, openid) {
        let fileType = getFileType(filefullpath);
        let uploadTask = wx.uploadFile({
          url: wxconfig.config.api_base_url + '/api/upload/picture',
          filePath: filefullpath,
          name: 'fileData',
          header: {
            "Content-Type": "multipart/form-data"
          },
          formData: {
            //....
            'FileType':fileType,
            'userId': openid
          },
          success: function (res) {
            console.log(res)
          },
          fail(res) {
            console.log(res)
          },
          complete(res) {
            // console.log(res)
          }
        });
        return uploadTask;
      }
      2020-04-16
      回复
    • Mr.Zhao
      Mr.Zhao
      2020-04-16回复李忠
      https://stackoverflow.com/questions/21395142/exception-multipart-form-data-request-failed-read-timed-out
      2020-04-16
      回复
    • 李忠
      李忠
      2020-04-16
      @RequestMapping("/picture")
          public void uploadPicture(HttpServletRequest request, HttpServletResponse response) throws Exception {
              log.info("== uploadPicture 1===");
              //获取文件需要上传到的路径
              String path = UserConstantInterface.UPLOAD_FILEPATH;//"D:/upload/test";
              // 用上面传入的这个路径创建一个文件并检查文件是否存在,不存在就创建一个。
              File dir = new File(path);
              if (!dir.exists()) {
                  dir.mkdirs();
                  //dir.mkdir();
              }
              request.setCharacterEncoding("utf-8"); //设置编码
              //创建DiskFileItemFactory对象,设置缓冲区大小和临时文件目录
              DiskFileItemFactory factory = new DiskFileItemFactory();
              factory.setRepository(dir);
              factory.setSizeThreshold(10*1024 * 1024);
              //使用DiskFileItemFactory 对象创建ServletFileUpload对象
              ServletFileUpload upload = new ServletFileUpload(factory);
              String orgFileName="",fileType="";
              log.info("== uploadPicture 2===");
              try {
                  // 调用ServletFileUpload.parseRequest方法解析request对象,得到一个保存了所有上传内容的List对象。
                  List<FileItem> list = upload.parseRequest(request);
                  FileItem picture = null;
                  // 对list进行迭代,每迭代一个FileItem对象,调用其isFormField方法判断是否是上传文件(false是上传文件的类型,true是普通表单类型)
                  log.info("== uploadPicture 3===");
                  for (FileItem item : list) {
                      //获取表单的属性名字
                      String name = item.getFieldName();
                      log.info("字段:"+name);// 每一个属性都是一个单独的对象存在。
                      //如果获取的表单信息是普通的 文本 信息
                      if (item.isFormField()) {
                          //获取用户具体输入的字符串
                          String value = item.getString();
                          request.setAttribute(name, value);
                          log.info(String.format("%s=%s",name,value));
                      } else {
                          if ("fileData".equals(item.getFieldName())) {
                              orgFileName = item.getName();
                              fileType = item.getContentType().substring(item.getContentType().lastIndexOf('/')+1);
                          }
                          picture = item;
                      }
                  }
                  // 使用IO流,给文件命名,指定存放文件地址
                  //String fileName = request.getAttribute("userId") + ".jpg";
      // String destPath = path + "/" + fileName;
                  String destPath=request.getAttribute("userId")+fileType;
                  if (!orgFileName.isEmpty()) {
                      destPath = path +"/"+ orgFileName;
                  }
                  log.info("== uploadPicture 4===");
                  //真正写到磁盘上
                  File file = new File(destPath);
                  OutputStream out = new FileOutputStream(file);
                  InputStream in = picture.getInputStream();
                  int length = 0;
                  byte[] buf = new byte[1024];
                  log.info("== uploadPicture 5===");
                  // in.read(buf) 每次读到的数据存放在buf 数组中
                  while ((length = in.read(buf)) != -1) {
                      //在buf数组中取出数据写到(输出流)磁盘上
                      out.write(buf, 0, length);
                  }
                  in.close();
                  out.close();
              } catch (FileUploadException e1) {
                  log.error("FileUploadException错误:", e1);
              } catch (Exception e) {
                  log.error("未知错误:", e);
              }
          }
      2020-04-16
      回复
    • Mr.Zhao
      Mr.Zhao
      2020-04-16回复李忠
      不知道你这个错误怎么复现,我没出过这个问题
      2020-04-16
      回复
    查看更多(9)
登录 后发表内容
问题标签