HttpURLConnection 上传大文件 内存溢出 out of memery
来源:互联网 发布:网络解锁 编辑:程序博客网 时间:2024/06/05 03:43
项目中遇到问题总结一下:
在使用HttpURLConnection 上传大文件时,出现内存溢出的错误,这让我产生了错觉,输入和输出流咋会暂用内存,不就是一个数据传送的管道么,都没有把数据读取到内存中,为撒会报错。。。然后就纠结了。。。
不过实在与原来的经验相违背,然后写了一个示例直接从file中读出然后写入到输出流中,发现并没有问题
这下确认了问题出在HttpURLConnection,查看API发现,HTTP会有缓冲机制,缓存把JVM撑挂了导致内存溢出了。。。。
通过设置以下方法即可解决问题
setFixedLengthStreamingMode
public void setFixedLengthStreamingMode(int contentLength)
- 此方法用于在预先已知内容长度时启用没有进行内部缓冲的 HTTP 请求正文的流。
如果应用程序尝试写入的数据多于指示的内容长度,或者应用程序在写入指示的内容长度前关闭了 OutputStream,将抛出异常。
启用输出流时,不能自动处理验证和重定向。如果需要验证和重定向,则在读取响应时将抛出 HttpRetryException。可以查询此异常以获取错误的详细信息。
该方法必须在连接 URLConnection 前调用。
- private void uploadFile()
- {
- String urlResouce = null;
- // urlResouce = "http://localhost:8080/application_interface_manager/platform/wangpan/444.png";
- urlResouce = "http://localhost:8080/application_interface_manager/platform/wangpan/myeclipse111.exe";
- try{
- // File localFile = new File("C:\\Users\\maomao\\Documents\\111.png");
- File localFile = new File("Z:\\tools\\myeclipse-8.5.0-win32.exe");
- //创建客户端签名
- String clientToken = new CreateSignTokenImpl().getToken("caizhonghu",secretKey);
- HttpURLConnection urlConnection =
- (HttpURLConnection) (new URL(urlResouce)).openConnection();
- urlConnection.setChunkedStreamingMode(0);
- urlConnection.setRequestProperty("Charset", "UTF-8");
- urlConnection.setRequestProperty("Token", "jingdong "+accessKey+":"+clientToken);
- urlConnection.setDoInput(true);
- urlConnection.setDoOutput(true);
- urlConnection.setRequestMethod("PUT");
- OutputStream urlOutputStream = urlConnection.getOutputStream();
- FileInputStream fileInputStream = new FileInputStream(localFile);
- IOUtils.copy(fileInputStream, urlOutputStream);
- // byte[] buffer = new byte[10240];
- // long count = 0;
- // int n = 0;
- // while (-1 != (n = fileInputStream.read(buffer))) {
- // urlOutputStream.write(buffer, 0, n);
- // count += n;
- // urlOutputStream.flush();
- // }
- fileInputStream.close();
- urlOutputStream.close();
- System.out.println(urlConnection.getResponseCode());
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
1 0
- HttpURLConnection 上传大文件 内存溢出 out of memery
- HttpURLConnection上传大文件内存溢出问题
- HttpURLConnection上传大文件内存溢出的原因及解决办法
- Mina2 Out of Memery原因
- c3p0连接数据库 out of memery
- web工程下 tomcat 上传大文件 内存溢出问题解决
- 上传大文件报内存溢出错误OutOfMemory
- Matlab内存溢出错误 Out of memory
- Android内存溢出---out of menory
- IDEA 内存溢出 OUT of Memory问题
- sphinx内存溢出问题(out of memory)
- ByteArrayOutputStream 大文件内存溢出
- android内存泄露 out of memory Bitmap内存溢出
- 内存溢出 out of memory与内存泄露 memory leak
- 内存溢出 out of memory与内存泄露 memory leak
- Unity3d发布错误:could not allocate memery:system out of memery!
- 我的WEBLOGIC OUT OF MEMERY 错误及处理
- WAS OOM(out of memery)挂死的常规分析
- PyQt精彩实例地址
- 向上推动门效果
- shell界面下安装和卸载Android应用程序(apk包)
- JavaScript模拟map的实现及其性能分析
- Android 自定义视图 顶部导航
- HttpURLConnection 上传大文件 内存溢出 out of memery
- java application应用程序 使用JDBC和proxool两种方式连接数据库 的测试程序代码
- jquery click 传递参数
- android camera(一):camera模组CMM介绍 .
- HEVC ZOrder and RasterOrder
- 仔细推敲,这些说法都是无稽之谈
- C#多线程协同完成一个任务
- linphone mediastream2 分析-双向录音机
- filter过滤器的功能简要介绍