Java 用于处理HttpClient进行的文件上载的服务器代码
我有一个Java HttpClient,它执行以下代码:Java 用于处理HttpClient进行的文件上载的服务器代码,java,spring,apache-httpclient-4.x,multipartentity,Java,Spring,Apache Httpclient 4.x,Multipartentity,我有一个Java HttpClient,它执行以下代码: HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("http://exampleutl.com/upload/"); File file = new File("C:/src_path/binary.doc"); MultipartEntityBuilder builder = MultipartEntityBuilder.create(
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://exampleutl.com/upload/");
File file = new File("C:/src_path/binary.doc");
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.STRICT);
FileBody fileBody = new FileBody(file); //image should be a String
builder.addPart("file", fileBody);
post.setEntity(builder.build());
client.execute(post);
我无法确定映射到/upload/path的服务器方法应该是什么样子
接受此文件上载请求的服务器是Spring 4.0。大概是这样的:
@RequestMapping(method = RequestMethod.POST, value = "/upload/")
public @ResponseBody String saveUpload(UploadDto dto) throws IOException,ServletException {
File file = new File("C:/dest_path/" + dto.getFile().getOriginalFilename());
FileUtils.writeByteArrayToFile(file, dto.getFile().getBytes());
return "success";
}
@RequestMapping(method = RequestMethod.POST, value = "/upload/")
public @ResponseBody String saveUpload(@RequestParam("file") final MultipartFile mpf) throws IOException, ServletException, FileUploadException {
File file = new File("C:/dest_path/" + mpf.getOriginalFilename());
FileUtils.writeByteArrayToFile(file, mpf.getBytes());
return "success";
}
上面的服务器方法由client.execute()调用,但UploadDto为空。
以下是上传到:
public class UploadDto {
private MultipartFile file;
public MultipartFile getFile() {
return file;
}
public void setFile(MultipartFile file) {
this.file = file;
}
}
任何帮助都将不胜感激 您似乎在SpringServlet上下文中缺少了一个
MultipartResolver
bean。差不多
@Bean
public MultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
return multipartResolver;
}
您正在将请求发送到
HttpPost post = new HttpPost("http://exampleutl.com/upload/");
假设您的上下文路径是ROOT,即空的,您的处理程序方法应该映射到/upload
@RequestMapping(method = RequestMethod.POST, value = "/upload")
最终的答案是服务器方法应该如下所示:
@RequestMapping(method = RequestMethod.POST, value = "/upload/")
public @ResponseBody String saveUpload(UploadDto dto) throws IOException,ServletException {
File file = new File("C:/dest_path/" + dto.getFile().getOriginalFilename());
FileUtils.writeByteArrayToFile(file, dto.getFile().getBytes());
return "success";
}
@RequestMapping(method = RequestMethod.POST, value = "/upload/")
public @ResponseBody String saveUpload(@RequestParam("file") final MultipartFile mpf) throws IOException, ServletException, FileUploadException {
File file = new File("C:/dest_path/" + mpf.getOriginalFilename());
FileUtils.writeByteArrayToFile(file, mpf.getBytes());
return "success";
}
正如Sotirios Delimanois所提到的,多部件解析器确实也是解决方案的必要组成部分:
@Bean
public MultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
return multipartResolver;
}
上传的dto是空的
是什么意思?@SotiriosDelimanolis我的意思是说dto.getFile()是空的。你是对的,谢谢你注意到这个错误。不幸的是,这并不能解决问题。我已经编辑了这个问题来解决这个变化,并添加了更多的信息。@vanceagle您的上下文中似乎缺少了一个MultipartResolver
bean。没有多部分解析器
,Spring无法正确处理请求。