0

I have a controller

void upload(@RequestParam(value="file", MultiPartFile file, @RequestParam(value = "content", required = false) InputStream stream){}

I never get a handle to InputStream when user uploads a file through Stream.

How do i configure that?

The normal file upload works just fine. I am sending Bzip2 content to upload and multipart default enabled in springboot.

I do get this error.

Caused by: java.lang.IllegalArgumentException: Could not retrieve InputStream for class path resource [BZh91AY&SY90WT�A�%L !���!��9D�����ܑN�$�L��]:

at org.springframework.beans.propertyeditors.InputStreamEditor.setAsText(InputStreamEditor.java:77) at org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:449) at org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:422) at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:195) at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:107) at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:64)

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
javahelper
  • 1
  • 1
  • 1
  • 2
    Why are you passing in the `InputStream` as a parameter? If you want to read the uploaded file's content you should use the `InputStream` from `MultiPartFile.getInputStream()` to do so. – Andy Wilkinson Apr 21 '16 at 06:27
  • See this [post](http://stackoverflow.com/questions/25699727/multipart-file-upload-spring-boot) maybe can help you – ElMariachi25 Jul 05 '16 at 10:44

1 Answers1

0

Controller

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.flasher.service.ImageService;

@RestController
public class ImageControllerRest {
    @Autowired
    ImageService imageService;

    @PostMapping("/upload_img")
    public ResponseEntity<?> uploadImage(@RequestParam("file") MultipartFile file, Authentication authentication) {
        try {
            imageService.store(file, authentication.getName());
            return new ResponseEntity<>(HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }

}

Service

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.flasher.util.UUIDGenerator;

@Service
public class ImageService {
    @Value("${flasher.absolute.img.path}")
    String imageUploadPath;
    @Autowired
    UUIDGenerator uuidGenerator;

    public void store(MultipartFile file, String username) {
        try {
            File uploadFolder = new File(imageUploadPath+username);
            if (!uploadFolder.exists()) {
                uploadFolder.mkdirs();
            }           
            Files.copy(file.getInputStream(),
                    Paths.get(uploadFolder.getAbsolutePath()).resolve(uuidGenerator.generateUUID()+"."+file.getContentType().split("/")[1]));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

You can achive file upload by this way

Jackson Baby
  • 398
  • 3
  • 4
  • 20