android客户端怎么把图片post给php服务端? 键值对那是hTTP的头,这个图片数据要放到http的body中。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Gangalf's fireworks求代码 public static String post(String actionUrl, Map<String, String> params, Map<String, File> files) throws IOException { StringBuilder sb2 = new StringBuilder(); String BOUNDARY = java.util.UUID.randomUUID().toString(); String PREFIX = "--" , LINEND = "\r\n"; String MULTIPART_FROM_DATA = "multipart/form-data"; String CHARSET = "UTF-8"; URL uri = new URL(actionUrl); HttpURLConnection conn = (HttpURLConnection) uri.openConnection(); conn.setReadTimeout(5 * 1000); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("connection", "keep-alive"); conn.setRequestProperty("Charsert", "UTF-8"); conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA + ";boundary=" + BOUNDARY); StringBuilder sb = new StringBuilder(); for (Map.Entry<String, String> entry : params.entrySet()) { sb.append(PREFIX); sb.append(BOUNDARY); sb.append(LINEND); sb.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + LINEND); sb.append("Content-Type: text/plain; charset=" + CHARSET+LINEND); sb.append("Content-Transfer-Encoding: 8bit" + LINEND); sb.append(LINEND); sb.append(entry.getValue()); sb.append(LINEND); } DataOutputStream outStream = new DataOutputStream(conn.getOutputStream()); outStream.write(sb.toString().getBytes()); if(files!=null){ //int i = 0; for (Map.Entry<String, File> file: files.entrySet()) { StringBuilder sb1 = new StringBuilder(); sb1.append(PREFIX); sb1.append(BOUNDARY); sb1.append(LINEND); //sb1.append("Content-Disposition: form-data; name=\"file"+(i++)+"\"; filename=\""+file.getKey()+"\""+LINEND); sb1.append("Content-Disposition: form-data; name=\"userupfile\"; filename=\""+file.getKey()+"\""+LINEND); sb1.append("Content-Type: application/octet-stream; charset="+CHARSET+LINEND); sb1.append(LINEND); outStream.write(sb1.toString().getBytes()); InputStream is = new FileInputStream(file.getValue()); byte[] buffer = new byte[1024]; int len = 0; while ((len = is.read(buffer)) != -1) { outStream.write(buffer, 0, len); } is.close(); outStream.write(LINEND.getBytes()); } } byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes(); outStream.write(end_data); outStream.flush(); int res = conn.getResponseCode(); InputStream in = null; if (res == 200) { in = conn.getInputStream(); int ch; while ((ch = in.read()) != -1) { sb2.append((char) ch); } Log.i("CAMERA", sb2.toString()); } return in == null ? null : sb2.toString(); } 果然可以用~还想请教一个问题,用相机拍摄之后,得到的uri地址是content://media/external/images/media/1309,怎么用它得到FILE呢? Uri uri = data.getData();String[] proj = { MediaStore.Images.Media.DATA };Cursor actualimagecursor = managedQuery(uri,proj,null,null,null);int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);actualimagecursor.moveToFirst();String img_path = actualimagecursor.getString(actual_image_column_index);File file = new File(img_path); 建议用apache库的实现方式,楼上给的那个java运行库的,可靠性不高。上传大文件二进制的,核心就是分段http报头 multipart/form-data 我想再请教一下,怎么获得文件的类型?是png,jpg还是啥 确实 可靠性不高,已经验证过,正在修改 这个应该不错 public static String newPost(Context context,String actionUrl, Map<String, String> params,String imagepath) { String code = "0"; Util.saveLog(context,"Start:" + actionUrl); Log.i("http", actionUrl); HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, 1000*30); HttpConnectionParams.setSoTimeout(httpParameters, 1000*30); HttpConnectionParams.setTcpNoDelay(httpParameters, true); HttpClient httpclient = new DefaultHttpClient(httpParameters); HttpPost httppost = new HttpPost(actionUrl); MultipartEntity mpEntity = new MultipartEntity(); try { File imageFile = new File(imagepath); if(!imageFile.exists()) { Log.i("http", "999"); code = "999"; } for (Map.Entry<String, String> entry : params.entrySet()) { String key = entry.getKey().toString(); String value = entry.getValue().toString(); Log.i("Http","KEY:" + key + ",Value:" + value ); mpEntity.addPart(key, new StringBody(value)); } FileBody file = new FileBody(imageFile); mpEntity.addPart("userupfile", file); httppost.setEntity(mpEntity); HttpResponse response = httpclient.execute(httppost); if (response.getStatusLine().getStatusCode() == 200) { code = EntityUtils.toString(response.getEntity()); System.out.println("result:" + code); Log.i("http", code); Util.saveLog(context,"End:" + code); } } catch (Exception e) { Util.saveLog(context,"ERROR:"+e.getMessage()); return "999"; } return code; }需要用到 apache的2个包apache-mime4j-0.6.jar,httpmime-4.0.jar android 怎么实现列表功能 如何获得WebView网页加载完成这个事件 多盟广告条 移植的xpdf到android没有字体~~ sdk manager怎么没有sdk2.0,api5的 情何以堪呐 Android传感器API之:温度传感器SensorTemperature功能源码 求高人指点,关于android的timepickerdialog 有简单办法上SD卡写一字符串到文件中吗? TableView怎么取消单击选中事件 R文件又消失!!! ViewFlipper一切换图片,其他view调用的layout方法就失效
求代码
Map<String, File> files) throws IOException {
StringBuilder sb2 = new StringBuilder();
String BOUNDARY = java.util.UUID.randomUUID().toString();
String PREFIX = "--" , LINEND = "\r\n";
String MULTIPART_FROM_DATA = "multipart/form-data";
String CHARSET = "UTF-8"; URL uri = new URL(actionUrl);
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
conn.setReadTimeout(5 * 1000);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("connection", "keep-alive");
conn.setRequestProperty("Charsert", "UTF-8");
conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA + ";boundary=" + BOUNDARY); StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : params.entrySet()) {
sb.append(PREFIX);
sb.append(BOUNDARY);
sb.append(LINEND);
sb.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + LINEND);
sb.append("Content-Type: text/plain; charset=" + CHARSET+LINEND);
sb.append("Content-Transfer-Encoding: 8bit" + LINEND);
sb.append(LINEND);
sb.append(entry.getValue());
sb.append(LINEND);
} DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
outStream.write(sb.toString().getBytes());
if(files!=null){
//int i = 0;
for (Map.Entry<String, File> file: files.entrySet()) {
StringBuilder sb1 = new StringBuilder();
sb1.append(PREFIX);
sb1.append(BOUNDARY);
sb1.append(LINEND);
//sb1.append("Content-Disposition: form-data; name=\"file"+(i++)+"\"; filename=\""+file.getKey()+"\""+LINEND);
sb1.append("Content-Disposition: form-data; name=\"userupfile\"; filename=\""+file.getKey()+"\""+LINEND);
sb1.append("Content-Type: application/octet-stream; charset="+CHARSET+LINEND);
sb1.append(LINEND);
outStream.write(sb1.toString().getBytes()); InputStream is = new FileInputStream(file.getValue());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
} is.close();
outStream.write(LINEND.getBytes());
}
}
byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();
outStream.write(end_data);
outStream.flush(); int res = conn.getResponseCode();
InputStream in = null;
if (res == 200) {
in = conn.getInputStream();
int ch;
while ((ch = in.read()) != -1) {
sb2.append((char) ch);
}
Log.i("CAMERA", sb2.toString());
}
return in == null ? null : sb2.toString();
}
我想再请教一下,怎么获得文件的类型?是png,jpg还是啥
这个应该不错
public static String newPost(Context context,String actionUrl, Map<String, String> params,String imagepath) {
String code = "0";
Util.saveLog(context,"Start:" + actionUrl);
Log.i("http", actionUrl);
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 1000*30);
HttpConnectionParams.setSoTimeout(httpParameters, 1000*30);
HttpConnectionParams.setTcpNoDelay(httpParameters, true); HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpPost httppost = new HttpPost(actionUrl);
MultipartEntity mpEntity = new MultipartEntity();
try {
File imageFile = new File(imagepath);
if(!imageFile.exists()) {
Log.i("http", "999");
code = "999";
}
for (Map.Entry<String, String> entry : params.entrySet()) {
String key = entry.getKey().toString();
String value = entry.getValue().toString();
Log.i("Http","KEY:" + key + ",Value:" + value );
mpEntity.addPart(key, new StringBody(value));
}
FileBody file = new FileBody(imageFile);
mpEntity.addPart("userupfile", file);
httppost.setEntity(mpEntity);
HttpResponse response = httpclient.execute(httppost);
if (response.getStatusLine().getStatusCode() == 200) {
code = EntityUtils.toString(response.getEntity());
System.out.println("result:" + code);
Log.i("http", code);
Util.saveLog(context,"End:" + code);
}
} catch (Exception e) {
Util.saveLog(context,"ERROR:"+e.getMessage());
return "999";
}
return code;
}需要用到 apache的2个包apache-mime4j-0.6.jar,httpmime-4.0.jar