Java 使用Rest模板发送文件时,MultipartFile为空
嗨,我有两个独立的应用程序 第一个是发送post请求: 我正在尝试向第二个应用发送文件Java 使用Rest模板发送文件时,MultipartFile为空,java,spring,spring-mvc,multipartform-data,resttemplate,Java,Spring,Spring Mvc,Multipartform Data,Resttemplate,嗨,我有两个独立的应用程序 第一个是发送post请求: 我正在尝试向第二个应用发送文件 public static String httpPostMultipartFile(String url, File file) throws IOException{ RestTemplate restTemplate = new RestTemplate(); MultiValueMap<String, Object> parameters = new LinkedMulti
public static String httpPostMultipartFile(String url, File file) throws IOException{
RestTemplate restTemplate = new RestTemplate();
MultiValueMap<String, Object> parameters = new LinkedMultiValueMap<String, Object>();
parameters.add("file", file); // load file into parameter
HttpHeaders headers1 = new HttpHeaders();
headers1.setContentType(MediaType.MULTIPART_FORM_DATA);
String result = restTemplate.exchange(
url,
HttpMethod.POST,
new HttpEntity<MultiValueMap<String, Object>>(parameters, headers1),
String.class
).getBody();
return result;
但是在我的第二个应用程序中,从rest调用来看,MultipartFile实际上是空的
我发现这个链接对我没有帮助
您的httpPostMultipartFile方法应该如下所示。您需要使用
新文件系统资源(file.getPath())
添加文件
公共静态字符串httpPostMultipartFile(字符串url,文件文件)引发IOException{
RestTemplate RestTemplate=新RestTemplate();
MultiValueMap multipartMap=新链接的MultiValueMap();
add(“file”,新文件系统资源(file.getPath());
HttpHeaders=新的HttpHeaders();
headers.setContentType(MediaType.MULTIPART\u FORM\u DATA);
HttpEntity请求=新的HttpEntity(多部分映射,标题);
字符串结果=restTemplate.exchange(
网址,
HttpMethod.POST,
要求
String.class
).getBody();
返回结果;
}
这是一个如何使用RestTemplate调用包含@RequestParam文件的web服务的示例(我已经测试过):
import java.io.File;
import java.io.IOException;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;
public class Application {
public static void main(String[] args) throws IOException {
LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
FileSystemResource value = new FileSystemResource(new File("src/myFile.txt"));
map.add("file", value);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map, headers);
RestTemplate restTemplate = new RestTemplate();
restTemplate.exchange("http://localhost/api/ws/myws", HttpMethod.POST, requestEntity, String.class);
}
}
导入java.io.File;
导入java.io.IOException;
导入org.springframework.core.io.FileSystemResource;
导入org.springframework.http.HttpEntity;
导入org.springframework.http.HttpHeaders;
导入org.springframework.http.HttpMethod;
导入org.springframework.http.MediaType;
导入org.springframework.util.LinkedMultiValueMap;
导入org.springframework.web.client.rest模板;
公共类应用程序{
公共静态void main(字符串[]args)引发IOException{
LinkedMultiValueMap=新建LinkedMultiValueMap();
FileSystemResource值=新的FileSystemResource(新文件(“src/myFile.txt”);
添加(“文件”,值);
HttpHeaders=新的HttpHeaders();
headers.setContentType(MediaType.MULTIPART\u FORM\u DATA);
HttpEntity requestEntity=新的HttpEntity(映射、头);
RestTemplate RestTemplate=新RestTemplate();
restTemplate.exchange(“http://localhost/api/ws/myws,HttpMethod.POST,requestEntity,String.class);
}
}
如果您解释了更改的内容和原因,这将是一个更好的答案。
public static String httpPostMultipartFile(String url, File file) throws IOException{
RestTemplate restTemplate = new RestTemplate();
MultiValueMap<String, Object> multipartMap = new LinkedMultiValueMap<String, Object>();
multipartMap.add("file", new FileSystemResource(file.getPath()));
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<Object> request = new HttpEntity<Object>(multipartMap, headers);
String result = restTemplate.exchange(
url,
HttpMethod.POST,
request,
String.class
).getBody();
return result;
}
import java.io.File;
import java.io.IOException;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;
public class Application {
public static void main(String[] args) throws IOException {
LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
FileSystemResource value = new FileSystemResource(new File("src/myFile.txt"));
map.add("file", value);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map, headers);
RestTemplate restTemplate = new RestTemplate();
restTemplate.exchange("http://localhost/api/ws/myws", HttpMethod.POST, requestEntity, String.class);
}
}