jfinal cos 2019.8 上传视频文件错误

jfinal-文件上传-问题记录

参考官方文档编写文件上传代码,相同的代码,上传图片和文档都正常,但是上传视频出现异常

java.lang.RuntimeException: java.io.IOException: Corrupt form data: premature ending


编写代码

package com.litongjava.jfinal.controler;
import com.jfinal.core.Controller;
import com.jfinal.kit.Kv;
import com.jfinal.upload.UploadFile;
public class UploadController extends Controller {
  /**
   * 上传文件
   */
  public void index() {
    UploadFile uploadFile = getFile();
    String uploadPath = uploadFile.getUploadPath();
    String fileName = uploadFile.getFileName();
    String contentType = uploadFile.getContentType();
    String originalFileName = uploadFile.getOriginalFileName();
    String parameterName = uploadFile.getParameterName();
    Kv kv = Kv.create();
    kv.set("uploadPath", uploadPath);
    kv.set("fileName", fileName);
    kv.set("contentType", contentType);
    kv.set("originalFileName", originalFileName);
    kv.set("parameterName", parameterName);
    renderJson(kv);
  }
}

使用Postman测试,调用接口上传一个文档,上传成功


image.png

再测试一张图片,上传成功

image.png

测试视频上传,出现下面的错误

image.png


异常信息

2021-12-06 11:27:12.044 [main] INFO  Application.main:37 - create server
2021-12-06 11:27:12.064 [main] INFO  Application.main:41 - start server
Starting JFinal 4.9.16 -> http://0.0.0.0:10043/litongjava-jfinal-ffmpeg-server
Info: jfinal-undertow 2.7, undertow 2.2.9.Final, jvm 1.8.0_121
2021-12-06 11:27:12.247 [main] INFO  undertow.start:120 - starting server: Undertow - 2.2.9.Final
2021-12-06 11:27:12.256 [main] INFO  xnio.<clinit>:95 - XNIO version 3.8.4.Final
2021-12-06 11:27:12.265 [main] INFO  nio.<clinit>:58 - XNIO NIO Implementation Version 3.8.4.Final
2021-12-06 11:27:12.450 [main] INFO  threads.<clinit>:52 - JBoss Threads version 3.1.0.Final
Starting Complete in 0.4 seconds. Welcome To The JFinal World (^_^)
http://10.11.63.222:10043/litongjava-jfinal-ffmpeg-server
http://10.11.63.222:10043/litongjava-jfinal-ffmpeg-server
启动完成,共使用了446ms
java.lang.RuntimeException: java.io.IOException: Corrupt form data: premature ending
at com.jfinal.upload.MultipartRequest.wrapMultipartRequest(MultipartRequest.java:126)
at com.jfinal.upload.MultipartRequest.<init>(MultipartRequest.java:68)
at com.jfinal.core.Controller.getFiles(Controller.java:868)
at com.jfinal.core.Controller.getFile(Controller.java:873)
at com.litongjava.jfinal.controler.UploadController.index(UploadController.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.jfinal.aop.Invocation.invoke(Invocation.java:97)
at com.litongjava.jfinal.interceptor.ExceptionInterceptor.intercept(ExceptionInterceptor.java:23)
at com.jfinal.aop.Invocation.invoke(Invocation.java:91)
at com.jfinal.core.ActionHandler.handle(ActionHandler.java:96)
at com.litongjava.jfinal.handler.ActionSuffixHandler.handle(ActionSuffixHandler.java:28)
at com.jfinal.core.JFinalFilter.doFilter(JFinalFilter.java:90)
at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
at io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:68)
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
at io.undertow.servlet.handlers.RedirectDirHandler.handleRequest(RedirectDirHandler.java:68)
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:117)
at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.SendErrorPageHandler.handleRequest(SendErrorPageHandler.java:52)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.SessionRestoringHandler.handleRequest(SessionRestoringHandler.java:119)
at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:280)
at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:79)
at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:134)
at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:131)
at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:260)
at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:79)
at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:100)
at io.undertow.server.Connectors.executeRootHandler(Connectors.java:387)
at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:852)
at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:2019)
at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1558)
at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1423)
at org.xnio.XnioWorker$WorkerThreadFactory$1$1.run(XnioWorker.java:1280)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.IOException: Corrupt form data: premature ending
at com.oreilly.servlet.multipart.MultipartParser.<init>(MultipartParser.java:207)
at com.oreilly.servlet.MultipartRequest.parse(MultipartRequest.java:238)
at com.oreilly.servlet.MultipartRequest.<init>(MultipartRequest.java:180)
at com.jfinal.upload.MultipartRequest.wrapMultipartRequest(MultipartRequest.java:107)
... 50 more
2021-12-06 11:27:16.541 [XNIO-1 task-2] ERROR ExceptionInterceptor.intercept:26 - java.io.IOException: Corrupt form data: premature ending

同样都是视频流,为什么偏偏就视频出现异常呢?

没有找到原因是是什么,换一种接收上传文件的方式,使用commons-fileupload接收

修改代码后上传成功

添依赖

<dependency>
  <groupId>commons-fileupload</groupId>
  <artifactId>commons-fileupload</artifactId>
  <version>1.4</version>
</dependency>

编写代码

package com.litongjava.jfinal.controler;
import java.io.File;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.servlet.ServletRequestContext;
import com.jfinal.core.Controller;
import com.jfinal.kit.Kv;
import com.jfinal.upload.UploadFile;
public class UploadController extends Controller {
  /**
   * 上传文件
   */
  public void index() {
    HttpServletRequest request = getRequest();
    
    ServletFileUpload servletFileUpload = new ServletFileUpload(new DiskFileItemFactory());
    List<FileItem> fileItemList = null;
    
    try {
      fileItemList = servletFileUpload.parseRequest(new ServletRequestContext(request));
    } catch (FileUploadException e1) {
      e1.printStackTrace();
      return;
    }
    Kv kv = Kv.create();
    for (FileItem fileItem : fileItemList) {
      String name = fileItem.getName();
      String fieldName = fileItem.getFieldName();
      
      kv.set("fileName", name);
      kv.set("fieldName", fieldName);
      
      System.out.println(name);// 上传的文件名
      System.out.println(fieldName);// input元素中name值
      try {
        File file = new File(name);
        fileItem.write(file);
        System.out.println(file.getAbsolutePath());
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    renderJson(kv);
  }
  public void v1() {
    UploadFile uploadFile = getFile();
    String uploadPath = uploadFile.getUploadPath();
    String fileName = uploadFile.getFileName();
    String contentType = uploadFile.getContentType();
    String originalFileName = uploadFile.getOriginalFileName();
    String parameterName = uploadFile.getParameterName();
    Kv kv = Kv.create();
    kv.set("uploadPath", uploadPath);
    kv.set("fileName", fileName);
    kv.set("contentType", contentType);
    kv.set("originalFileName", originalFileName);
    kv.set("parameterName", parameterName);
  }
}

测试上传成功

image.png


评论区

zzutligang

2021-12-07 03:03

我这里一直用的cos接收上传文件,没发现视频文件出错啊。

李通

2021-12-07 14:13

@zzutligang 环境非常复杂,我在我的运行环境下出错误了,如果解决错误预计非常麻烦,所以换了一种接收方式

李通

2021-12-07 14:14

@zzutligang 关于cos的bug已经是早有定论的.cos已经2年没有更新了

杜福忠

2021-12-07 14:44

@zzutligang 你用的
#Generated by Maven
#Sat Apr 11 16:16:56 CST 2020
version=2020.4
groupId=com.jfinal
artifactId=cos
2020.4这个波总出的版本吧?我也是这个版本,正常

zzutligang

2021-12-08 03:21

@杜福忠 @李通 确实,我的是cos-2020.4.jar这个库,我的项目没用maven,就只直接在maven官网上搜的版本,下载放到我的项目里的。反正这个是没问题的。我的项目利用到上传视频文件,并且要在页面预览播放。

李通

2021-12-08 16:07

@杜福忠 @zzutligang 谢谢两位,我更新到2020.4版本后测试正常
``

com.jfinal
cos
2020.4


``

热门分享

扫码入社