Android上传多张图片卡死

Leo星星 2016-01-07 09:49:27
通过在网上找到的上传代码,我已经实现了文字+单一照片的上传,并且很成功顺利。
但是在制作类似发微博多张图片的功能时,不知道为什么就不行了。
PS:在华为G730手机上不行,在魅族的魅蓝note上居然可以!
我的代码:
private void sendRequestWithHttpURLConnection(){

//开启下称刘发起网络请求
new Thread(new Runnable(){
public void run(){
HttpURLConnection connection = null;
String BOUNDARY = UUID.randomUUID().toString(); // 边界标识 随机生成
String PREFIX = "--";
String LINEND = "\r\n";
String CONTENT_TYPE = "multipart/form-data"; // 内容类型
String CHARSET = "UTF-8";
try{
URL url;
if(IsEdit){
url = new URL("http://www.ylhuakai.com/alflower/Data/editActlog");
}else{
url = new URL("http://www.ylhuakai.com/alflower/Data/addActLog");
}
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setReadTimeout(600000);
connection.setConnectTimeout(100000000);
connection.setDoInput(true);// 允许输入
connection.setDoOutput(true);// 允许输出
connection.setUseCaches(false); // 不允许使用缓存
connection.setRequestProperty("Charsert", "UTF-8");
// connection.setChunkedStreamingMode(51200); //不知道为什么不注释掉就不行
connection.setRequestProperty("connection", "keep-alive");
connection.setRequestProperty("Content-Type", CONTENT_TYPE + ";boundary=" + BOUNDARY);


//这个是我上传的文本,没出现过问题
Map<String, String> params = new HashMap<String, String>();
params.put("user_id", User_id);
params.put("act_id", Act_id);
params.put("log_id", Log_id);
params.put("log_time", log_time.getText().toString());
params.put("log_address", log_address.getText().toString());
params.put("log_content", log_content.getText().toString());


// 首先组拼文本类型的参数
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);
}
connection.connect();

DataOutputStream dos = new DataOutputStream(connection.getOutputStream());
dos.write(sb.toString().getBytes());


// 发送文件数据

if (files != null) {

//这里可以显示出来,应该算是files没问题吧
System.out.println("=======files.size()========="+files.size()+"==========");

for (int i = 0; i < files.size(); i++) {

/**
* 当文件不为空,把文件包装并且上传
*/
StringBuffer sBuffer = new StringBuffer();
sBuffer.append(PREFIX);
sBuffer.append(BOUNDARY);
sBuffer.append(LINEND);
/**
* 这里重点注意: name里面的值为服务器端需要key 只有这个key 才可以得到对应的文件 filename是文件的名字,包含后缀名的 比如:abc.png
*/
sBuffer.append("Content-Disposition: form-data; name=\"img_list[]\"; filename=\"" + files.get(i).getName() + "\"" + LINEND);
sBuffer.append("Content-Type: application/octet-stream; charset=" + CHARSET + LINEND);
sBuffer.append(LINEND);
dos.write(sBuffer.toString().getBytes());
//这里的文件名也正常显示
System.out.println("=======files.get(i).getName()========="+files.get(i).getName()+"==========");

InputStream is = new FileInputStream(files.get(i));
byte[] bytes = new byte[1024];
int len = 0;
while ((len = is.read(bytes)) != -1) {
dos.write(bytes, 0, len);
}
is.close();
dos.write(LINEND.getBytes());
}
}
byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();
dos.write(end_data);
dos.flush();
//dos.close();
//下面对获取到的输入流进行读取

/**
* 获取响应码 200=成功 当响应成功,获取响应的流
*/
InputStream in = null;
int res = connection.getResponseCode();
if (res == 200) {
in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
while((line = reader.readLine()) !=null){
response.append(line);
}


Message message = new Message();
if(IsEdit){
message.what = Edit_SUCCESS;
}else{
message.what = Create_SUCCESS;
}
//将服务器返回的结果放到message中
message.obj = response.toString();
handler.sendMessage(message);
in.close();
}

}catch (Exception e){
e.printStackTrace();
}finally{
if(connection != null){
connection.disconnect();
}
}
}
}).start();

}

其实关于stream我确实搞不懂什么时候flush、close,所以都是通过网上代码来回试验的。
曾有一段时间,上面的代码在只选一张图片时,华为手机也能通过,但现在居然也不行了。

补充:在这个代码之前,有一段压缩图片的代码
files = new ArrayList<File>();



if (MyAdapter.mSelectedImage.size() > 0) {
for (int i = 0; i < MyAdapter.mSelectedImage.size(); i++) {
Bitmap md = ImageCompress.getSmallBitmap(MyAdapter.mSelectedImage.get(i));
File f = ImageCompress.saveMyBitmap(md, "huakaitemp"+i);
files.add(f);
}
}
sendRequestWithHttpURLConnection();
我觉得能输出文件名且找得到文件,就应该不是这个原因吧
求帮忙分析一下吧,整个项目最后的一个问题了。我参考了朋友的代码核心也是if (files != null) {for (int i = 0; i < files.size(); i++) {}}这个样子,只不过用了很多类。
...全文
406 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
梦想的开拓者 2016-06-12
  • 打赏
  • 举报
回复
这位同学,,,我也遇到一样的问题啊!!!你解决没啊?
_周星星 2016-01-13
  • 打赏
  • 举报
回复
如果是http连接的话 可以用AsyncHttpClient 上传文件so简单 上传图片的时候一张一张 的上传 上传之前压缩
huangxiaohu_coder 2016-01-13
  • 打赏
  • 举报
回复
不行是指上传不成功还是指上传block住了,加Log或者断点一步步调试看看
酒尽雪满天 2016-01-12
  • 打赏
  • 举报
回复
干嘛要自己写?重复造轮子,用okHttp上传不就行了?

80,490

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧