android httpclient 上傳文件
public void uploadFileClient()
{
Toast.makeText(this, "現在已經開始上傳了!", Toast.LENGTH_LONG).show();
String targetURL = actionUrl;// 上傳指定URL
File targetFile = new File(uploadFile);// 指定上傳文件
PostMethod filePost = new PostMethod(targetURL);
try
{
// 通過以下方法可以模擬頁麵參數提交
// filePost.setParameter("name", "中文");
// filePost.setParameter("pass", "1234");
byte[] buffer = new byte[1024];
Part[] parts =
{ new FilePart(targetFile.getName() + System.currentTimeMillis(),
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();
}
}
最後更新:2017-04-03 22:30:57