当前位置:网站首页>Method of converting file to multipartfile

Method of converting file to multipartfile

2022-06-10 23:55:00 Li_ XiaoJin

File To MultipartFile

introduce spring-mock rely on

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-mock</artifactId>
            <version>2.0.8</version>
        </dependency>

Code implementation :

  File file = new File("PATH");

  InputStream fileInputStream = new FileInputStream(file);

  MultipartFile multipartFile = new MockMultipartFile(fileName, fileName, ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);

What we should pay attention to here is ContentType.APPLICATION_OCTET_STREAM.toString() , The following tape must be selected contentType Construction method of , I used to use no contentType Of , Because there are no settings contentType Resulting in subsequent generation MultipartFile Can't upload successfully . Bear in mind

public MockMultipartFile(String name, 
            @Nullable String originalFilename,
            @Nullable String contentType, 
            InputStream contentStream) throws IOException 

Copyright: use Creative Commons signature 4.0 International license agreement to license Links:https://lixj.fun/archives/file-to-multipartfile

原网站

版权声明
本文为[Li_ XiaoJin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206102238037913.html