2016-04-21 64 views
0

我有一个控制器springboot的InputStream

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

我从来没有当用户上传文件通过流的句柄的InputStream。

我该如何配置?

正常的文件上传工作得很好。 我正在发送Bzip2内容以上载并在springboot中启用multipart默认值。

我得到这个错误。

产生的原因:java.lang.IllegalArgumentException异常:无法检索 的InputStream类路径资源[BZh91AY & SY90WTA%L !9DܑN $ L]:在 org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:449)

在 org.springframework.beans.propertyeditors.InputStreamEditor.setAsText(InputStreamEditor.java:77) 在 org.springframework.beans.TypeConverterDelegate.doConver tValue在 org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:107)在 org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:195)(TypeConverterDelegate.java:422) 在 有机.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:64)

+2

为什么你在'InputStream'作为参数传递achive文件上传?如果你想读取上传文件的内容,你应该使用'MultiPartFile.getInputStream()'中的'InputStream'来完成。 –

+0

看到这[post](http://stackoverflow.com/questions/25699727/multipart-file-upload-spring-boot)也许可以帮助你 – ElMariachi25

回答

0

控制器

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

    } 

} 

服务

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

您可以通过这种方式