首页 > 解决方案 > FileSystemResource 到 MultipartFile

问题描述

我想使用这样的路径从我的存储中发送我的图像:

MultiValueMap<String, Object> image = new LinkedMultiValueMap<String, Object>(); 
image.add("image", new FileSystemResource("C:\\xx\\xx\\xx\\xx\\xx\\xx\\xx\\img\\xx.jpg"));

所以我使用下面的代码调用对象:

MultiValueMap<String, Object> body = new LinkedMultiValueMap<String,Object>();
body.add("image", image);

然后,我收到此错误:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class sun.nio.ch.ChannelInputStream]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class sun.nio.ch.ChannelInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.springframework.util.LinkedMultiValueMap["image"]->java.util.LinkedList[0]->org.springframework.core.io.FileSystemResource["inputStream"])] with root cause

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class sun.nio.ch.ChannelInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.springframework.util.LinkedMultiValueMap["image"]->java.util.LinkedList[0]->org.springframework.core.io.FileSystemResource["inputStream"])

我正在使用 spring boot 的 Rest Template,这需要我通过 POST 方法发送MultipartFile对象。现在,我不知道如何转换成MultipartFile类型,
所以 Rest Template 可以接受我的请求帖子。

注意:我想使用,body.add("image", image.getResource());但它没有出现在选择中,因为图像不是MultipartFile类型。

标签: springrestspring-bootfiletemplates

解决方案


这对我有用。

    @GetMapping(path = "/copy", produces = MediaType.TEXT_PLAIN_VALUE)
    public ResponseEntity<String> copyFile() {

        MultiValueMap<String, Object> body
                = new LinkedMultiValueMap<>();
        body.add("image", new FileSystemResource("test.jpg"));

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        HttpEntity<MultiValueMap<String, Object>> requestEntity
                = new HttpEntity<>(body, headers);

        String serverUrl = "http://localhost:8080/upload";

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate
                .postForEntity(serverUrl, requestEntity, String.class);

        return response;
    }

    @PostMapping(path = "/upload", produces = MediaType.TEXT_PLAIN_VALUE)
    public String uploadFile(@RequestParam("image") MultipartFile file) {

        try {

            byte[] bytes = file.getBytes();
            Path path = Paths.get("./uploadedImages/" + file.getOriginalFilename());
            Files.write(path, bytes);

        } catch (IOException e) {
            e.printStackTrace();
            return "Error";
        }

        return "File Uploaded";
    }

推荐阅读