Android http post 上传图片不成功
客户端:Android,服务端:php 我在 Android客户端写了几种上传文件的方法,都没上传成功, (1) 基于Android HttpClient的 Post方法
Java code?/** * 用Post模式访问http,包括附件上传 * @param url URL * @param nameValuePairs 参数信息 * @return 返回的字符串 */ public static String post(String url, List<NameValuePair> nameValuePairs) { String strResult = ""; HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost(url); try { MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); for(int index = 0; index < nameValuePairs.size(); index++) { if(nameValuePairs.get(index).getName().equalsIgnoreCase("upload")) { // If the key equals to "upload", we use FileBody to transfer the data GiantLog.d("httpServerAccess","nameValuePair:name" + nameValuePairs.get(index).getName() + nameValuePairs.get(index).getValue()); entity.addPart(nameValuePairs.get(index).getName(), new FileBody(new File (nameValuePairs.get(index).getValue()))); } else { // Normal string data entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue())); } } httpPost.setEntity(entity); HttpResponse httpResponse = httpClient.execute(httpPost, localContext); if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { // 取出响应字符 strResult = EntityUtils.toString(httpResponse.getEntity(),HTTP.UTF_8); // byte[] bytesResult = EntityUtils.toByteArray(httpResponse // .getEntity()); // if (bytesResult != null) { // strResult = new String(bytesResult, "utf-8"); // } } } catch (IOException e) { e.printStackTrace(); strResult = ""; } return strResult; }
(2) 基于HttpUrlConnection,需要拼接Http头
Java code?package com.giant.android.utils; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.*; /** * 文件名称:UploadImage.java * 创建日期:2012-12-28 * 实现图片文件上传 */public class UploadImage { String multipart_form_data = "multipart/form-data"; String twoHyphens = "--"; String boundary = "****************fD4fH3gL0hK7aI6"; // 数据分隔符 String lineEnd = System.getProperty("line.separator"); // The value is "\r\n" in Windows. private final static String LINEND = "\r\n"; private final static String BOUNDARY = "---------------------------7da2137580612"; //数据分隔线 private final static String PREFIX = "--"; private static final String TAG = "UploadImage"; /* * 上传图片内容,格式参考HTTP 协议格式。 * 格式如下所示: * --****************fD4fH3hK7aI6 * Content-Disposition: form-data; name="upload_file"; filename="apple.jpg" * Content-Type: image/jpeg * * 这儿是文件的内容,二进制流的形式 */ private void addImageContent(Image[] files, DataOutputStream output) { for(Image file : files) { StringBuilder split = new StringBuilder(); split.append(twoHyphens + boundary + lineEnd); split.append("Content-Disposition: form-data; name=\"" + file.getFormName() + "\"; filename=\"" + file.getFileName() + "\"" + lineEnd); split.append("Content-Type: " + "image/jpg" + lineEnd); split.append(lineEnd); try { // 发送图片数据 output.writeBytes(split.toString()); GiantLog.d(TAG,"image formdata:" + split.toString() ); output.write(file.getData(), 0, file.getData().length); GiantLog.d(TAG,"image data length:" + file.getData().length ); output.writeBytes(lineEnd); } catch (IOException e) { throw new RuntimeException(e); } } } /* * 构建表单字段内容,格式请参考HTTP 协议格式(用FireBug可以抓取到相关数据)。(以便上传表单相对应的参数值) * 格式如下所示: * --****************fD4fH3hK7aI6 * Content-Disposition: form-data; name="action" * // 一空行,必须有 * upload */ private void addFormField(Map<String,String> params, DataOutputStream output) { StringBuilder sb = new StringBuilder(); for(Map.Entry<String,String> entry : params.entrySet()) { sb.append(twoHyphens + boundary + lineEnd); sb.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + lineEnd); sb.append(lineEnd); sb.append(entry.getValue() + lineEnd); } try { output.writeBytes(sb.toString());// 发送表单字段数据 } catch (IOException e) { throw new RuntimeException(e); } } /** * 封装表单文本数据 * @param paramText * @return */ private String bulidFormText(Map<String,String> paramText){ if(paramText==null || paramText.isEmpty()) return ""; StringBuffer sb = new StringBuffer(""); for(Map.Entry<String,String> entry : paramText.entrySet()){ sb.append(PREFIX).append(BOUNDARY).append(LINEND); sb.append("Content-Disposition:form-data;name=\"" + entry.getKey() + "\"" + LINEND); // sb.append("Content-Type:text/plain;charset=" + CHARSET + LINEND); sb.append(LINEND); sb.append(entry.getValue()); sb.append(LINEND); } return sb.toString(); } /** * 直接通过 HTTP 协议提交数据到服务器,实现表单提交功能。 * @param actionUrl 上传路径 * @param params 请求参数key为参数名,value为参数值 * @param files 上传文件信息 * @return 返回请求结果 */ public String post(String actionUrl, Map<String,String> params, Image[] files) { HttpURLConnection conn = null; DataOutputStream dataOutputStream = null; BufferedReader input = null; try { URL url = new URL(actionUrl); conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(120000); conn.setDoInput(true); // 允许输入 conn.setDoOutput(true); // 允许输出 conn.setUseCaches(false); // 不使用Cache conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "keep-alive"); conn.setRequestProperty("Content-Type", multipart_form_data + "; boundary=" + boundary); conn.connect(); dataOutputStream = new DataOutputStream(conn.getOutputStream()); addImageContent(files, dataOutputStream); // 添加图片内容 addFormField(params, dataOutputStream); // 添加表单字段内容 dataOutputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);// 数据结束标志 dataOutputStream.flush(); // output.close(); int code = conn.getResponseCode(); if(code != 200) { throw new RuntimeException("请求‘" + actionUrl +"’失败!"); } input = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuilder response = new StringBuilder(); String oneLine; while((oneLine = input.readLine()) != null) { response.append(oneLine + lineEnd); } // input.close(); return response.toString(); } catch (IOException e) { throw new RuntimeException(e); } finally { // 统一释放资源 try { if(dataOutputStream != null) { dataOutputStream.close(); } if(input != null) { input.close(); } } catch (IOException e) { throw new RuntimeException(e); } if(conn != null) { conn.disconnect(); } } } public static void main(String[] args) { try { String response = ""; BufferedReader in = new BufferedReader(new FileReader("config/actionUrl.properties")); String actionUrl = in.readLine(); // 读取表单对应的字段名称及其值 Properties formDataParams = new Properties(); formDataParams.load(new FileInputStream(new File("config/formDataParams.properties"))); Set<Map.Entry<Object,Object>> params = formDataParams.entrySet(); // 读取图片所对应的表单字段名称及图片路径 Properties imageParams = new Properties(); imageParams.load(new FileInputStream(new File("config/imageParams.properties"))); Set<Map.Entry<Object,Object>> images = imageParams.entrySet(); Image[] files = new Image[images.size()]; int i = 0; for(Map.Entry<Object,Object> image : images) { Image file = new Image(image.getValue().toString(), image.getKey().toString()); files[i++] = file; } // Image file = new Image("images/apple.jpg", "upload_file"); // Image[] files = new Image[0]; // files[0] = file; // response = new UploadImage().post(actionUrl, params, files); System.out.println("返回结果:" + response); } catch (IOException e) { e.printStackTrace(); } } } MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 去掉参数:HttpMultipartMode.BROWSER_COMPATIBLE 试试。我以前也碰到这样的问题过
[1] [2] 下一页
|