如何在Spring中将Image转换为multipart文件 [英] How to convert Image to multipart file in spring

查看:157
本文介绍了如何在Spring中将Image转换为multipart文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an image file and creating File object with that image

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

以上行正在创建 file 对象的大小如 440272 ,我需要转换上面的图像文件进入多部分文件为此我做了

The above line is creating a file object with some size like 440272, I need to convert the above image file into multipart file for that I did

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);

问题是在转换后我得到 size = 0 而不是像 440272 那样得到一些尺寸。如果我为图像获得 size ,以便我可以通过将其传递到<$ c $来存储到某个位置c>上传方法。

the problem is after converting I am getting size=0 instead of getting some size like 440272. If I get size for the image so that I can store into some some location by passing it to 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 size after converting into multipart file is my approach is correct to convert image file to multipart file? or I need to follow any other approach for this?

推荐答案

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

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));

如果你想使用CommonsMultipartFile,我想你应该把你的pom文件放到commons-fileupload中

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

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

CommonsMultipartFile的文档声明

The documentation of CommonsMultipartFile states that


注意:从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

让我知道这是否适合你

这篇关于如何在Spring中将Image转换为multipart文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆