Java 在独立应用程序中使用spring将文件上载到http
我正在用java开发一个独立的应用程序。我正在将Spring与Apache服务器一起使用,我想将一个文件上载到服务器。我的问题是,我在客户端收到了很多不推荐的警告。有人能给我一些提示吗 客户端:Java 在独立应用程序中使用spring将文件上载到http,java,spring,apache,http,file-upload,Java,Spring,Apache,Http,File Upload,我正在用java开发一个独立的应用程序。我正在将Spring与Apache服务器一起使用,我想将一个文件上载到服务器。我的问题是,我在客户端收到了很多不推荐的警告。有人能给我一些提示吗 客户端: public void addCourse(ActionEvent e) throws ClientProtocolException, IOException { File file=fileChooser.showOpenDialog(this.mainApp.getCurr
public void addCourse(ActionEvent e) throws ClientProtocolException, IOException
{
File file=fileChooser.showOpenDialog(this.mainApp.getCurrentStage());
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost("http://localhost:8080/ASCourses/filter/upload");
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file, "multipart/form-data");
mpEntity.addPart("file", cbFile);
httppost.setEntity(mpEntity);
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
System.out.println(response.getStatusLine());
if (resEntity != null) {
System.out.println(EntityUtils.toString(resEntity));
}
if (resEntity != null) {
resEntity.consumeContent();
}
httpclient.getConnectionManager().shutdown();
}
服务器端:
@RequestMapping(value="/upload", method=RequestMethod.POST)
public @ResponseBody String handleFileUpload(
@RequestParam("file") MultipartFile file){
String name = "test11";
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(new File(name + "-uploaded")));
stream.write(bytes);
stream.close();
return "You successfully uploaded " + name + " into " + name + "-uploaded !";
} catch (Exception e) {
return "You failed to upload " + name + " => " + e.getMessage();
}
} else {
return "You failed to upload " + name + " because the file was empty.";
}
}
另外,我想问你是否有其他上传文件的方法。请记住,我使用的是独立的应用程序,而不是web。
多谢各位