Java RestTemplate上载图像文件
我需要创建RestTemplate请求,它将通过PHP应用程序发送图像上传 我的代码是:Java RestTemplate上载图像文件,java,spring,resttemplate,Java,Spring,Resttemplate,我需要创建RestTemplate请求,它将通过PHP应用程序发送图像上传 我的代码是: Resource resource = new FileSystemResource("/Users/user/Documents/MG_0599.jpg"); MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>(); parts.add("Content-Type", "
Resource resource = new FileSystemResource("/Users/user/Documents/MG_0599.jpg");
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
parts.add("Content-Type", "image/jpeg");
parts.add("file", resource);
parts.add("id_product", productId);
ResponseEntity<String> response = cookieRestTemplate.getForEntity(url, String.class, parts);
在海报插件上,我只是从我的文件系统中选择文件
您能帮助我吗?您没有正确使用
rest模板。您正在使用以下方法
public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> urlVariables)
请注意,使用GET
请求进行文件上载是非常少见的。我已经更新了descriptionOk,但我发现的信息都是,我应该像这样上载文件。你还有别的解决办法吗?我可以在标题中添加一些东西,但什么呢?我正在测试您的请求版本,但exchange请求并没有传递到PHP控制器。我不知道为什么,但我有500个错误。GetForEntity工作正常,但不发送映像:/@llkar 500是服务器端错误。也许它很难收到文件。你有最大文件上传大小吗?没有大小限制,文件有300KB。您知道如何在POSTER tool中将exchange请求模拟到firefox吗?@Ilkar本例中的exchange方法只是发送一个包含多部分内容类型的GET请求。
public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> urlVariables)
Resource resource = new FileSystemResource(
"/Users/user/Documents/MG_0599.jpg");
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
parts.add("Content-Type", "image/jpeg");
parts.add("file", resource);
parts.add("id_product", productId);
restTemplate.exchange(url, HttpMethod.GET,
new HttpEntity<MultiValueMap<String, Object>>(parts),
String.class); // make sure to use the generic type argument