public static void main(String args[]) {
String targetURL = "http://xxxxxxx";
File targetFile = new File("c:/xx.txt");
PostMethod filePost = new PostMethod(targetURL);
try {
// 通过以下方法可以模拟页面参数提交
filePost.setParameter("name", "9999999");
filePost.setParameter("pass", "1234");
Part[] parts = { new FilePart("excel", targetFile) };
filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
int status = client.executeMethod(filePost);
if (status == HttpStatus.SC_OK) {
System.out.println("上传成功");
// 上传成功
} else {
System.out.println("上传失败");
// 上传失败
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
filePost.releaseConnection();
}
}