[Java]代码 01public void uploadFileClient()02 {03 Toast.makeText(this, "现在已经开始上传了!", Toast.LENGTH_LONG).show();04 String targetURL = actionUrl;// 上传指定URL05 06 File targetFile = new File(uploadFile);// 指定上传文件07 08 PostMeth…
[Java]代码
01 |
public void uploadFileClient() |
03 |
Toast.makeText(this, "现在已经开始上传了!", Toast.LENGTH_LONG).show(); |
04 |
String targetURL = actionUrl;// 上传指定URL |
06 |
File targetFile = new File(uploadFile);// 指定上传文件 |
08 |
PostMethod filePost = new PostMethod(targetURL); |
14 |
// filePost.setParameter("name", "中文"); |
15 |
// filePost.setParameter("pass", "1234"); |
16 |
byte[] buffer = new byte[1024]; |
18 |
{ new FilePart(targetFile.getName() + System.currentTimeMillis(), |
21 |
filePost.setRequestEntity(new MultipartRequestEntity(parts, |
22 |
filePost.getParams())); |
23 |
HttpClient client = new HttpClient(); |
24 |
client.getHttpConnectionManager().getParams() |
25 |
.setConnectionTimeout(5000); |
26 |
int status = client.executeMethod(filePost); |
28 |
if (status == HttpStatus.SC_OK) |
30 |
System.out.println("上传成功"); |
34 |
System.out.println("上传失败"); |
37 |
} catch (Exception ex) |
42 |
filePost.releaseConnection(); |