客户端代码,这是从网络在找的代码。 public static void fileupload() { String targetURL = null;
File targetFile = null; targetFile = new File("G:/1.jpg");
targetURL = "http://localhost:8080/image/upload"; // servleturl
PostMethod filePost = new PostMethod(targetURL); try {
filePost.setParameter("username", "admin");
filePost.setParameter("password", "admin");
filePost.setParameter("location", "location");
filePost.setParameter("mood", "mood");
filePost.setParameter("re", "re");
Part[] parts = { new FilePart("file", targetFile) };
filePost.setRequestEntity(new MultipartRequestEntity(parts,
filePost.getParams()));
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams()
.setConnectionTimeout(5000);
int status = client.executeMethod(filePost);
System.out.println(status);
if (status == HttpStatus.SC_OK) {
System.out.println("上传成功");
} else {
System.out.println("上传失败");
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
filePost.releaseConnection();
}
}服务端使用spring mvc,使用注解的方式,方法体如下。 @RequestMapping(value = "/upload", method = RequestMethod.POST)
public String upload(@RequestParam(value = "file", required=false) CommonsMultipartFile cmFile,
@RequestParam("username") String username,
@RequestParam("password") String password,
@RequestParam("location") String location,
@RequestParam("mood") int mood,
@RequestParam("re") String re,
HttpServletRequest request,
HttpServletResponse response) throws IOException {

错误代码是http 400.
因为换过很多种客户端实现方式,服务端均返回400.所以猜想可能是服务端哪里配置不对。
但是使用真正的html请求方式却可以成功上传,请问是哪里的问题。谢谢。