spring - 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
类型。
解决方案
这对我有用。
@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";
}
推荐阅读
- python - 在 Pandas 中绘制带有溢出箱的直方图
- android - Android - 单击时带有 ViewPager2 的 TabLayout 禁用选项卡自动聚焦
- java - org.json.simple JSONObject 插入项目的顺序
- python - 如何从 python 抓取的 URL 列表中的 URL 抓取数据?
- terraform - 因为 data.zabbix_template.template 设置了“for_each”,所以必须在特定实例上访问其属性
- android - “android.app.ResourcesManager”中的 Android ANR
- sql-server - 从 SQL Server 中的大表中删除大部分数据的策略
- reactjs - 我可以在没有 create-react-app 的现有网页中使用来自 npm 的 React 组件吗?
- rust - Rust:修改 HashMap 中的值,同时不可变地借用整个 HashMap
- php - 悬停在图像上时会覆盖跨度徽章