/**
* 上传文件到服务器
*
* @param file
*/
public static void uploadFile(Context context, File file) {
// 创建一个httppost的请求
PostMethod filePost = new PostMethod(
"http://192.168.1.250:8080/web/UploadFileServlet");
try {
// 组拼上传的数据
Part[] parts = { new FilePart("file", file) };
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: " + status);
} catch (Exception e) {
e.printStackTrace();
} finally {
filePost.releaseConnection();
}
}
* 上传文件到服务器
*
* @param file
*/
public static void uploadFile(Context context, File file) {
// 创建一个httppost的请求
PostMethod filePost = new PostMethod(
"http://192.168.1.250:8080/web/UploadFileServlet");
try {
// 组拼上传的数据
Part[] parts = { new FilePart("file", file) };
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: " + status);
} catch (Exception e) {
e.printStackTrace();
} finally {
filePost.releaseConnection();
}
}