public static void uploadFile(File file, String url) {
if (!file.exists()) {
return;
}
PostMethod postMethod = new PostMethod(url);
try {
//FilePart
FilePart fp = new FilePart("filedata", file);
Part[] parts = { fp };
MultipartRequestEntity mre = new MultipartRequestEntity(parts, postMethod.getParams());
postMethod.setRequestEntity(mre);
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(50000);//set connection time out
int status = client.executeMethod(postMethod);
if (status == HttpStatus.SC_OK) {
System.out.println(postMethod.getResponseBodyAsString());
} else {
System.out.println("fail");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//release connection
postMethod.releaseConnection();
}
}
HTTPClient 传输文件
最新推荐文章于 2024-02-18 14:44:39 发布