67,512
社区成员
发帖
与我相关
我的任务
分享
public void uploadFile(File file, String url) {
if (!file.exists()) {
return;
}
PostMethod postMethod = new PostMethod(url);
try {
Part[] parts = { new FilePart(file.getName(), file) };
postMethod.setRequestEntity(new MultipartRequestEntity(parts,postMethod.getParams()));
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
int status = client.executeMethod(postMethod);
if (status == HttpStatus.SC_OK)
{
System.out.println("上传成功"+postMethod.getResponseBodyAsString());
// 上传成功
}
else
{
System.out.println("上传失败"+status);
// 上传失败
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//释放连接
postMethod.releaseConnection();
}
}
/**
* @param args
*/
public static void main(String[] args) {
Test test = new Test();
test.uploadFile(new File("D://toBOS/f.txt"),
"http://127.0.0.1:8080/test/");
}