Java Spring:从类到RESTWeb服务的文件上载
我创建了一个SpringWeb服务文件exhange,这样客户端就可以从服务器下载一个文件(流式传输,因为文件可能很大),现在我可以从客户端实现文件上传(客户端是一个java类,它调用web服务并上传文件)。 对于第一步,下载部分,我使用以下代码: 服务器端(此代码将得到改进) 现在,对于文件上传,我应该使用上面的代码,但是使用反向的客户机-服务器代码。所以在客户端上,我可以使用Java Spring:从类到RESTWeb服务的文件上载,java,spring,rest,http,spring-mvc,Java,Spring,Rest,Http,Spring Mvc,我创建了一个SpringWeb服务文件exhange,这样客户端就可以从服务器下载一个文件(流式传输,因为文件可能很大),现在我可以从客户端实现文件上传(客户端是一个java类,它调用web服务并上传文件)。 对于第一步,下载部分,我使用以下代码: 服务器端(此代码将得到改进) 现在,对于文件上传,我应该使用上面的代码,但是使用反向的客户机-服务器代码。所以在客户端上,我可以使用 String response= restTemplate.postForEntity(serverIp + "/c
String response= restTemplate.postForEntity(serverIp + "/client/file/?toStorePath={toStorePath}", responseSend, String.class, toStorePath);
但如何设置HttpServletResponse
并将文件流发送到服务器?
谢谢
暂时更新这段代码,但我想使用spring而不是http方法
论客户
@Override
public Response sendFile(String serverIp, String toStorePath, String filePath) {
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()){
HttpPost httppost = new HttpPost(serverIp + "ATS/client/file");
File file = new File(filePath);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
ContentBody cbFile = new FileBody(file);
ContentBody cbPath= new StringBody(toStorePath,ContentType.TEXT_PLAIN);
builder.addPart("file", cbFile);
builder.addPart("toStorePath",cbPath);
httppost.setEntity(builder.build());
CloseableHttpResponse httpResponse = httpClient.execute(httppost);
HttpEntity resEntity = httpResponse.getEntity();
System.out.println(httpResponse.getStatusLine());
if (resEntity != null) {
ObjectMapper mapper = new ObjectMapper();
Response response = mapper.readValue(resEntity.getContent(), Response.class);
EntityUtils.consume(resEntity) ;
return response;
}
//It should never be thrown
else return new Response(false, false, "Error with server response" , null);
}catch(Exception e){
ErrorResponse errorResponse= ErrorResponseBuilder.buildErrorResponse(e);
LOG.error("Threw exception in FileServicesImpl::sendFile :" + errorResponse.getStacktrace());
return new Response(false, false,"Error sending the file!" , errorResponse);
}
而是在服务器上:
@Override
@RequestMapping(value = "/file", method = RequestMethod.POST)
public @ResponseBody Response storeAcquisition(@RequestParam("file") MultipartFile file, @RequestParam("toStorePath") String toStorePath){
String name= file.getOriginalFilename();
if (!file.isEmpty()) {
try {
file.transferTo(new File(toStorePath + "/" + name));
return new Response(true, true, "You successfully uploaded " + name + " into " + toStorePath, null);
} catch (Exception e) {
ErrorResponse errorResponse= ErrorResponseBuilder.buildErrorResponse(e);
LOG.error("Threw exception in MatlabClientControllerImpl::storeAcquisition :" + errorResponse.getStacktrace());
return new Response(false, false, "You failed to upload " + name, errorResponse);
}
} else {
return new Response(false, false, "You failed to upload " + name + " because the file was empty.", null);
}
}
我更新了工作代码,但我想使用spring库我更新了工作代码,但我想使用spring库
@Override
public Response sendFile(String serverIp, String toStorePath, String filePath) {
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()){
HttpPost httppost = new HttpPost(serverIp + "ATS/client/file");
File file = new File(filePath);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
ContentBody cbFile = new FileBody(file);
ContentBody cbPath= new StringBody(toStorePath,ContentType.TEXT_PLAIN);
builder.addPart("file", cbFile);
builder.addPart("toStorePath",cbPath);
httppost.setEntity(builder.build());
CloseableHttpResponse httpResponse = httpClient.execute(httppost);
HttpEntity resEntity = httpResponse.getEntity();
System.out.println(httpResponse.getStatusLine());
if (resEntity != null) {
ObjectMapper mapper = new ObjectMapper();
Response response = mapper.readValue(resEntity.getContent(), Response.class);
EntityUtils.consume(resEntity) ;
return response;
}
//It should never be thrown
else return new Response(false, false, "Error with server response" , null);
}catch(Exception e){
ErrorResponse errorResponse= ErrorResponseBuilder.buildErrorResponse(e);
LOG.error("Threw exception in FileServicesImpl::sendFile :" + errorResponse.getStacktrace());
return new Response(false, false,"Error sending the file!" , errorResponse);
}
@Override
@RequestMapping(value = "/file", method = RequestMethod.POST)
public @ResponseBody Response storeAcquisition(@RequestParam("file") MultipartFile file, @RequestParam("toStorePath") String toStorePath){
String name= file.getOriginalFilename();
if (!file.isEmpty()) {
try {
file.transferTo(new File(toStorePath + "/" + name));
return new Response(true, true, "You successfully uploaded " + name + " into " + toStorePath, null);
} catch (Exception e) {
ErrorResponse errorResponse= ErrorResponseBuilder.buildErrorResponse(e);
LOG.error("Threw exception in MatlabClientControllerImpl::storeAcquisition :" + errorResponse.getStacktrace());
return new Response(false, false, "You failed to upload " + name, errorResponse);
}
} else {
return new Response(false, false, "You failed to upload " + name + " because the file was empty.", null);
}
}