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());
}
}
与网络传输相比,可以忽略压缩解压带来的性能影响。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());
}
}
解决方案 »
- 如何设置imageView的高和宽?
- you are office feint requires an active network connection
- Gallery设置Adapter时候出现空指针~~求教
- android模拟器 EditText输入问题
- android小菜鸟求救
- 帮忙看个简单的异常
- android开发 使用系统图片浏览器浏览图片问题
- android自动打包
- 关于安卓业务流程的问题
- android 记事本在edittext插入图片重新获取图片问题
- 新手求助!设计一个android听力考试app,每道题都对应一段录音,录音应该存成文件还是存在SQLite数据库?
- Android工程中如何导入jar包,覆盖android.jar包中已有的包?
File image1;
File image2;
postBinary("the request url", true,
"oauth_token", "the token data",
"oauth_token_secret", "the secret data",
"content", "content string",
"form", 2,
"img1", image1,
"img2", image2);