private void httpUpload() {
//定义HttpClient对象
HttpClient client = new DefaultHttpClient();
//获得HttpPost对象
HttpPost post = new HttpPost("http://192.168.1.106:8001/2012/upload.php");
post.addHeader("charset", HTTP.UTF_8);
//实例化
MultipartEntity me = new MultipartEntity();
try {
me.addPart("content",new StringBody("12cccafasdfasdf"));
me.addPart("title",new StringBody("csdnliwei"));
me.addPart("local",new StringBody("beijing"));
//设置流文件
me.addPart("file", new InputStreamBody(new FileInputStream("/mnt/sdcard/test.jpg"), "image/pjpeg", "fengjie.jpg"));
post.setEntity(me);
//获得响应消息
HttpResponse resp = client.execute(post);
if(resp.getStatusLine().getStatusCode()==200){
Toast.makeText(this, "文件上传文成!", 1).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
本文介绍了一个使用Java在Android平台上实现文件上传的例子。通过HttpClient组件完成文件及参数的上传操作,包括设置请求头、构建MultipartEntity实体以及处理服务器响应等关键步骤。
1526

被折叠的 条评论
为什么被折叠?



