java 如何在 spring 中将图像转换为多部分文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38269876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:16:40  来源:igfitidea点击:

How to convert Image to multipart file in spring

javaspringimage

提问by subbu royal

I have an image file and creating Fileobject with that image

我有一个图像文件并File使用该图像创建对象

File file = new File("E://Shared Data/Images/xyz.jpg");

File file = new File("E://Shared Data/Images/xyz.jpg");

The above line is creating a fileobject with some size like 440272, I need to convert the above image fileinto multipart filefor that I did

上面的行正在创建一个file具有某种大小的对象440272,我需要将上面的内容image file转换multipart file为我所做的

DiskFileItem fileItem = new DiskFileItem("file", "image/png", false, file.getName(),
                                    (int) file.length(), file.getParentFile());
                            fileItem.getOutputStream();
                            MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
                            adminAssetUploadController.upload(request, multipartFile, "product", null);

the problem is after converting I am getting size=0instead of getting some sizelike 440272. If I get sizefor the imageso that I can store into some some location by passing it to uploadmethod.

问题是在转换后我得到size=0而不是得到一些size440272. 如果我得到size了,image这样我就可以通过将它传递给upload方法来存储到某个位置。

below is the upload method

下面是上传方法

public ResponseEntity<Map<String, Object>> upload(HttpServletRequest request,
            @RequestParam("file") MultipartFile file, @PathVariable(value = "sectionKey") String sectionKey,
            @PathVariable(value = "id") String id) throws IOException {
        Map<String, Object> responseMap = new HashMap<String, Object>();

        Map<String, String> properties = new HashMap<String, String>();
        properties.put("entityType", sectionKey);
        properties.put("entityId", id);

        StaticAsset staticAsset = staticAssetService.createStaticAssetFromFile(file, properties);
        staticAssetStorageService.createStaticAssetStorageFromFile(file, staticAsset);
         ........
         .......

}

can anyone help me why I am getting zero sizeafter converting into multipart fileis my approach is correct to convert image fileto multipart file? or I need to follow any other approachfor this?

谁能帮我为什么我收到zero size转换成后multipart file是我的做法是正确的转换image filemultipart file?或者我需要为此关注任何其他人approach

采纳答案by Periklis Douvitsas

I would suggest convert the image by using this code

我建议使用此代码转换图像

FileInputStream input = new FileInputStream(fileItem);
MultipartFile multipartFile = new MockMultipartFile("fileItem",
            fileItem.getName(), "image/png", IOUtils.toByteArray(input));

If you would like to use CommonsMultipartFile, I think you should have into your pom file the commons-fileupload

如果你想使用 CommonsMultipartFile,我认为你应该在你的 pom 文件中加入 commons-fileupload

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

The documentation of CommonsMultipartFile states that

CommonsMultipartFile 的文档指出

NOTE: As of Spring 2.0, this class requires Commons FileUpload 1.1 or higher. The implementation does not use any deprecated FileUpload 1.0 API anymore, to be compatible with future Commons FileUpload releases. http://docs.spring.io/spring-framework/docs/2.0.8/api/org/springframework/web/multipart/commons/CommonsMultipartFile.html

注意:从 Spring 2.0 开始,此类需要 Commons FileUpload 1.1 或更高版本。该实现不再使用任何已弃用的 FileUpload 1.0 API,以与未来的 Commons FileUpload 版本兼容。 http://docs.spring.io/spring-framework/docs/2.0.8/api/org/springframework/web/multipart/commons/CommonsMultipartFile.html

Let me know if this worked for you

让我知道这是否对你有用