1. 在android里把多张图片压缩到一起,然后上传这个压缩包,在服务端解压。
与网络传输相比,可以忽略压缩解压带来的性能影响。2. 使用httpmime-4.0.1.jar和apache-mime4j-0.6.jar,可以一个request上传多个文件。代码如下:
public static byte[] postBinary(String uri, boolean resp, Object... params) throws IOException {
Log.d(TAG, "[POST] " + uri);

HttpClient client = new DefaultHttpClient();
HttpParams httpParams = client.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 15000);
HttpConnectionParams.setSoTimeout(httpParams, 15000);
HttpPost post = new HttpPost(uri);

MultipartEntity entity = new MultipartEntity();
String paramName = null;
for(Object param : params){
if(paramName == null){
paramName = param.toString();
}else{
Log.d(TAG, "[PARAM] " + paramName + "=" + (param == null ? "" : param.toString()));
if(param instanceof File){
entity.addPart(paramName, new FileBody((File)param));
}else if(param instanceof byte[]){
entity.addPart(paramName, new InputStreamBody(new ByteArrayInputStream((byte[])param), ""));
}else{
entity.addPart(paramName, new StringBody(param == null ? "" : param.toString()));
}
paramName = null;
}
}
post.setEntity(entity);

HttpResponse response = client.execute(post);
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
return resp ? EntityUtils.toByteArray(response.getEntity()) : null;
}else{
throw new IOException("Http status: " + response.getStatusLine().getStatusCode());
}
}