安卓手机端程序第一次上传多图时候,没有错误,然后返回修改重新上传,出现错误报错信息,内存溢出
<p style="font-size:13.3333px;">
<br>
</p>
<p style="font-size:13.3333px;">
<br>
</p>
<p style="font-size:13.3333px;">
<span style="font-family:微软雅黑, 'MS Sans Serif', sans-serif;font-size:16px;line-height:normal;background-color:#FFEDC4;">下面是上传图片</span>
</p>
<p style="font-size:13.3333px;">
<span style="font-family:微软雅黑, 'MS Sans Serif', sans-serif;font-size:16px;line-height:normal;background-color:#FFEDC4;">try {</span>
</p>
<p style="font-size:13.3333px;">
<span style="font-family:微软雅黑, 'MS Sans Serif', sans-serif;font-size:16px;line-height:normal;background-color:#FFEDC4;">URL url = new URL(uploadUrl);</span><br>
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(true);// 允许使用缓存
httpURLConnection.setRequestMethod("POST");
// 设置Http请求头
httpURLConnection
.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setRequestProperty("Charset", "UTF-8");
httpURLConnection.setRequestProperty("Cookie", "JSESSIONID="
+ jsession);// 要加个试试
// 必须在Content-Type 请求头中指定分界符中的任意字符串
httpURLConnection.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
// 定义数据写入流,准备上传文件,,这个tmd就是点了以后就上传,,呵呵,,要保存起来,!!!
DataOutputStream dos = new DataOutputStream(httpURLConnection.getOutputStream());
dos.writeBytes(twoHyphens + boundary + end);
// 设置与上传文件相关的信息
dos.writeBytes("Content-Disposition: form-data; name="file"; filename=""
+ filename.substring(filename.lastIndexOf("/") + 1)
+ """ + end);
dos.writeBytes(end);
FileInputStream fis = new FileInputStream(filename);
byte[] buffer = new byte[8192]; // 8k
int count = 0;
// 读取文件夹内容,并写入OutputStream对象
while ((count = fis.read(buffer)) != -1) {
dos.write(buffer, 0, count);
}
fis.close();
dos.writeBytes(end);
dos.writeBytes(twoHyphens + boundary + twoHyphens + end);
dos.flush();
// 开始读取从服务器传过来的信息
InputStream is = httpURLConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
String result = br.readLine() + ";";
pathname += result;
dos.close();
is.close();
} catch (Exception e) {
}
下面是从服务器下载图片
try {
URL url = new URL(uploadUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(true);// 允许使用缓存
httpURLConnection.setRequestMethod("POST");
// 设置Http请求头
httpURLConnection
.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setRequestProperty("Charset", "UTF-8");
httpURLConnection.setRequestProperty("Cookie", "JSESSIONID="
+ jsession);// 要加个试试
// 必须在Content-Type 请求头中指定分界符中的任意字符串
httpURLConnection.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
// 定义数据写入流,准备上传文件,,这个tmd就是点了以后就上传,,呵呵,,要保存起来,!!!
DataOutputStream dos = new DataOutputStream(httpURLConnection.getOutputStream());
dos.writeBytes(twoHyphens + boundary + end);
// 设置与上传文件相关的信息
dos.writeBytes("Content-Disposition: form-data; name="file"; filename=""
+ filename.substring(filename.lastIndexOf("/") + 1)
+ """ + end);
dos.writeBytes(end);
FileInputStream fis = new FileInputStream(filename);
byte[] buffer = new byte[8192]; // 8k
int count = 0;
// 读取文件夹内容,并写入OutputStream对象
while ((count = fis.read(buffer)) != -1) {
dos.write(buffer, 0, count);
}
fis.close();
dos.writeBytes(end);
dos.writeBytes(twoHyphens + boundary + twoHyphens + end);
dos.flush();
// 开始读取从服务器传过来的信息
InputStream is = httpURLConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
String result = br.readLine() + ";";
pathname += result;
dos.close();
is.close();
} catch (Exception e) {
}
请问有高人可以指导吗?读文件写输出流时,循环体里也要加flush,否则会因为输出流的内存缓冲区溢出而出错。第一次上传没有出现错误,然后返回来修改,在上传就出现错误了
这个问题应该和往服务端读流写流没什么关系,看你的日志报错信息,你的图片貌似解码需要的内存大于4m,你查查你是否每次上传时,gridview的getview方法都在解码图片,这样的做法消耗内存资源很大。
第一次上传,没有出现错误,然后返回来修改,在上传就出现错误了版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。