httpclient上传文件崩溃问题 oom崩溃了现在改用httpurlconnection 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /** * 上传文件 * @param file * @return * @throws Exception */ public Json uploadLocal(File file) throws Exception { fileSize = (int) file.length(); if (null != progress) {progress.setMax(fileSize);} String url = "http://192.168.1.58:8088/test/other/httpTakeFile.htm"; HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.setDoOutput(true);conn.setDoInput(true); conn.setChunkedStreamingMode(1024 * 1024); conn.setRequestMethod(Domain.REQUEST_POST); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("Charsert", HTTP.UTF_8); conn.setRequestProperty("Content-Type", "multipart/form-data;file=" + file.getName()); conn.setRequestProperty("ext", file.getName().substring(file.getName().lastIndexOf("."))); OutputStream output = conn.getOutputStream();InputStream input = new FileInputStream(file); int i = 0, total = 0; byte[] buffer = new byte[1024]; while ((i = input.read(buffer)) != -1) { output.write(buffer, 0, i); total += i; if (null != progress) {progress.setProgress(total);} } String response = readFile(conn.getInputStream(), HTTP.UTF_8, 1, false); input.close();output.flush();output.close();conn.disconnect(); JSONObject object = new JSONObject(response); Json vo = new Json(object.getBoolean("success"), object.getString("message")); JSONObject data = null;try {data = (JSONObject) object.get("data");} catch (Exception e) {} if (null != data) { Iterator iterator = data.keys(); while (iterator.hasNext()) { String key = (String) iterator.next();vo.getData().put(key, data.get(key)); } } return vo; }带进度条上传文件,文件大小通吃 MultipartEntity这些都是在apache官网有源码的,下来代码调试一下,找到问题所在,也可为开源社区做点贡献。 HTTP POST协议 输出流不阻塞是什么情况 Gallery放在layout中如何设置显示的位置呢? 在android原码下打包自己的jar包,供开发使用 android开发者的春天 android 画图 Android平台 可以在ubuntu下搭建吗 求windows上的不随android sdk一起发布的工具 请问有研究过火狐浏览器安卓版的源代码吗? 请问各位在编写代码的时候出现xxx cannot to be resloved要怎么破? Android如何接受服务器的图片呀 使用 NDK android.mk ,LOCAL_LDLIBS 找不到库,怎么解决?? 如何加速二维码扫描
* 上传文件
* @param file
* @return
* @throws Exception
*/
public Json uploadLocal(File file) throws Exception {
fileSize = (int) file.length();
if (null != progress) {progress.setMax(fileSize);}
String url = "http://192.168.1.58:8088/test/other/httpTakeFile.htm";
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setDoOutput(true);conn.setDoInput(true);
conn.setChunkedStreamingMode(1024 * 1024);
conn.setRequestMethod(Domain.REQUEST_POST);
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("Charsert", HTTP.UTF_8);
conn.setRequestProperty("Content-Type", "multipart/form-data;file=" + file.getName());
conn.setRequestProperty("ext", file.getName().substring(file.getName().lastIndexOf(".")));
OutputStream output = conn.getOutputStream();InputStream input = new FileInputStream(file);
int i = 0, total = 0;
byte[] buffer = new byte[1024];
while ((i = input.read(buffer)) != -1) {
output.write(buffer, 0, i);
total += i;
if (null != progress) {progress.setProgress(total);}
}
String response = readFile(conn.getInputStream(), HTTP.UTF_8, 1, false);
input.close();output.flush();output.close();conn.disconnect();
JSONObject object = new JSONObject(response);
Json vo = new Json(object.getBoolean("success"), object.getString("message"));
JSONObject data = null;try {data = (JSONObject) object.get("data");} catch (Exception e) {}
if (null != data) {
Iterator iterator = data.keys();
while (iterator.hasNext()) {
String key = (String) iterator.next();vo.getData().put(key, data.get(key));
}
}
return vo;
}
带进度条上传文件,文件大小通吃
MultipartEntity这些都是在apache官网有源码的,下来代码调试一下,找到问题所在,也可为开源社区做点贡献。