How to convert Image to multipart file in spring

14,910

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

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

The documentation of CommonsMultipartFile states that

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

Let me know if this worked for you

Share:
14,910
subbu royal
Author by

subbu royal

Java World

Updated on July 10, 2022

Comments

  • subbu royal
    subbu royal almost 2 years

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

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

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

    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.

    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 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?

  • subbu royal
    subbu royal almost 8 years
    MockMultipartFile yeah it worked thank you very much.