图片文件上传
[java] //一些定义的变量
private String newName = "image.jpg";
private String uploadFile = "/sdcard/apple.jpg";
//private String actionUrl = "http://localhost:8080/testCutPic/upload.php";
//上传图片地址,上传的图片文件参数名为fileToUpload
private String actionUrl="http://192.168.1.64/svn_ys/sousoutu/api/api_imgupload.php";
//一些定义的变量
private String newName = "image.jpg";
private String uploadFile = "/sdcard/apple.jpg";
//private String actionUrl = "http://localhost:8080/testCutPic/upload.php";
//上传图片地址,上传的图片文件参数名为fileToUpload
private String actionUrl="http://192.168.1.64/svn_ys/sousoutu/api/api_imgupload.php";
HttpClient请求客户端方式:
[html] private void postFile(){
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost("http://192.168.1.64/svn_ys/sousoutu/api/api_imgupload.php");
File file = new File(uploadFile);
// File file2=new File(uploadFile);
ContentBody cbFile = new FileBody(file, "image/jpeg");
// ContentBody cbFile2 = new FileBody(file2, "image/jpeg");
MultipartEntity mpEntity = new MultipartEntity();
mpEntity.addPart("fileToUpload", cbFile);
// mpEntity.addPart("fileToUpload2", cbFile2);
// mpEntity.addPart("字符串参数", new StringBody("user"));
httppost.setEntity(mpEntity);
Log.d("log", "请求信息: " + httppost.getRequestLine());
// System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = null;
try {
response = httpclient.execute(httppost);
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpEntity resEntity = response.getEntity();
Log.d("log", "响应信息:"+response.getStatusLine().toString());
// System.out.println(response.getStatusLine());
if (resEntity != null) {
try {
final String response_str=EntityUtils.toString(resEntity);
Log.d("log", response_str);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (resEntity != null) {
try {
resEntity.consumeContent();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
httpclient.getConnectionManager().shutdown();
}
private void postFile(){
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost("http://192.168.1.64/svn_ys/sousoutu/api/api_imgupload.php");
File file = new File(uploadFile);
// File file2=new File(uploadFile);
ContentBody cbFile = new FileBody(file, "image/jpeg");
// ContentBody cbFile2 = new FileBody(file2, "image/jpeg");
MultipartEntity mpEntity = new MultipartEntity();
mpEntity.addPart("fileToUpload", cbFile);
// mpEntity.addPart("fileToUpload2", cbFile2);
// mpEntity.addPart("字符串参数", new StringBody("user"));
httppost.setEntity(mpEntity);
Log.d("log", "请求信息: " + httppost.getRequestLine());
// System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = null;
try {
response = httpclient.execute(httppost);
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpEntity resEntity = response.getEntity();
Log.d("log", "响应信息:"+response.getStatusLine().toString());
// System.out.println(response.getStatusLine());
if (resEntity != null) {
try {
final String response_str=EntityUtils.toString(resEntity);
Log.d("log", response_str);
}
补充:移动开发 , Android ,