当前位置: 首页 > news >正文

HttpURLConnection OOM问题记录

使用HttpURLConnection 上传大文件,会出现内存溢出问题:

观察HttpURLConnection 源码:

@Overridepublic synchronized OutputStream getOutputStream() throws IOException {connecting = true;SocketPermission p = URLtoSocketPermission(this.url);if (p != null) {try {return AccessController.doPrivilegedWithCombiner(new PrivilegedExceptionAction<>() {public OutputStream run() throws IOException {return getOutputStream0();}}, null, p            );} catch (PrivilegedActionException e) {throw (IOException) e.getException();}} else {return getOutputStream0();}
}

private synchronized OutputStream getOutputStream0() throws IOException {try {if (!doOutput) {throw new ProtocolException("cannot write to a URLConnection"                           + " if doOutput=false - call setDoOutput(true)");}if (method.equals("GET")) {method = "POST"; // Backward compatibility        }if ("TRACE".equals(method) && "http".equals(url.getProtocol())) {throw new ProtocolException("HTTP method TRACE" +" doesn't support output");}// if there's already an input stream open, throw an exception        if (inputStream != null) {throw new ProtocolException("Cannot write output after reading input.");}if (!checkReuseConnection())connect();boolean expectContinue = false;String expects = requests.findValue("Expect");if ("100-Continue".equalsIgnoreCase(expects) && streaming()) {http.setIgnoreContinue(false);expectContinue = true;}if (streaming() && strOutputStream == null) {writeRequests();}if (expectContinue) {expect100Continue();}ps = (PrintStream)http.getOutputStream();if (streaming()) {if (strOutputStream == null) {if (chunkLength != -1) { /* chunked */                     strOutputStream = new StreamingOutputStream(new ChunkedOutputStream(ps, chunkLength), -1L);} else { /* must be fixed content length */                    long length = 0L;if (fixedContentLengthLong != -1) {length = fixedContentLengthLong;} else if (fixedContentLength != -1) {length = fixedContentLength;}strOutputStream = new StreamingOutputStream(ps, length);}}return strOutputStream;} else {if (poster == null) {poster = new PosterOutputStream();}return poster;}} catch (RuntimeException e) {disconnectInternal();throw e;} catch (ProtocolException e) {// Save the response code which may have been set while enforcing        // the 100-continue. disconnectInternal() forces it to -1        int i = responseCode;disconnectInternal();responseCode = i;throw e;} catch (IOException e) {disconnectInternal();throw e;}
}

public boolean streaming () {return (fixedContentLength != -1) || (fixedContentLengthLong != -1) ||(chunkLength != -1);
}

如上, 默认设置情况下streaming ()  为false。

package sun.net.www.http
public class PosterOutputStream extends ByteArrayOutputStream {
}

PosterOutputStream  默认为 ByteArrayOutputStream  子类

解决办法:

目标服务支持情况下,可以不使用HttpURLConnection的ByteArrayOutputStream缓存机制,直接将流提交到服务器上。如下函数设置:

httpConnection.setChunkedStreamingMode(0); // 或者设置自定义大小,0默认大小

    public void setChunkedStreamingMode (int chunklen) {if (connected) {throw new IllegalStateException ("Can't set streaming mode: already connected");}if (fixedContentLength != -1 || fixedContentLengthLong != -1) {throw new IllegalStateException ("Fixed length streaming mode set");}chunkLength = chunklen <=0? DEFAULT_CHUNK_SIZE : chunklen;}

遗憾的是,我上传的服务不支持这种模式。因此采用固定大小。

HttpURLConnection con = (HttpURLConnection)new URL("url").openConnection();
con.setFixedLengthStreamingMode(输出流的固定长度);
  /*** This method is used to enable streaming of a HTTP request body* without internal buffering, when the content length is known in* advance.* <p>* An exception will be thrown if the application* attempts to write more data than the indicated* content-length, or if the application closes the OutputStream* before writing the indicated amount.* <p>* When output streaming is enabled, authentication* and redirection cannot be handled automatically.* A HttpRetryException will be thrown when reading* the response if authentication or redirection are required.* This exception can be queried for the details of the error.* <p>* This method must be called before the URLConnection is connected.* <p>* <B>NOTE:</B> {@link #setFixedLengthStreamingMode(long)} is recommended* instead of this method as it allows larger content lengths to be set.** @param   contentLength The number of bytes which will be written*          to the OutputStream.** @throws  IllegalStateException if URLConnection is already connected*          or if a different streaming mode is already enabled.** @throws  IllegalArgumentException if a content length less than*          zero is specified.** @see     #setChunkedStreamingMode(int)* @since 1.5*/public void setFixedLengthStreamingMode (int contentLength) {if (connected) {throw new IllegalStateException ("Already connected");}if (chunkLength != -1) {throw new IllegalStateException ("Chunked encoding streaming mode set");}if (contentLength < 0) {throw new IllegalArgumentException ("invalid content length");}fixedContentLength = contentLength;}

我是上传文件场景: 使用文件的大小作为长度

FileInputStream fileInputStream = new FileInputStream(uploadFileName);
long totalLength= fileInputStream.getChannel().size();
var boundary = "someboundary";
var temUploadUrl = "url path";
//
var url = new URL(temUploadUrl);
var connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("PUT");
connection.setRequestProperty("Content-Type", MediaType.MULTIPART_FORM_DATA_VALUE + "; boundary=" + boundary);connection.setDoOutput(true);
// 设置 Content-Length
connection.setRequestProperty("Content-Length", String.valueOf(totalLength)); 
connection.setFixedLengthStreamingMode(totalLength);

http://www.lryc.cn/news/259795.html

相关文章:

  • WT588F02B单片机语音芯片在磁疗仪中的应用介绍
  • 深度学习——第5章 神经网络基础知识
  • 微信网页授权步骤说明
  • linux bash shell变量操作符 —— 筑梦之路
  • 2.61【Python生成器与迭代器】
  • devecho stuido npm 失败
  • postgreSql逻辑复制常用语句汇总和说明
  • 设置Ubuntu或树莓派系统,允许root用户ssh方式连接
  • Ubuntu安装向日葵【远程控制】
  • jquery 实现倒计时60秒
  • 单例模式:饿汉模式、懒汉模式
  • 提升方法AdaBoost
  • Python自动化测试系列[v1.0.0][多种数据驱动实现附源码]
  • 【论文笔记】Gemini: A Family of Highly Capable Multimodal Models——细看Gemini
  • iOS加密CoreML模型
  • Springboot自定义start首发预告
  • [GWCTF 2019]我有一个数据库1
  • 【LeetCode每日一题】1904. 你完成的完整对局数
  • +0和不+0的性能差异
  • 美颜技术讲解:视频美颜SDK的开发与集成
  • 期末数组函数加强练习
  • 如何下载B站视频?我来教你B站视频下载方法
  • AcWing 3709:单链表节点交换 ← 四川大学考研机试题
  • RocketMQ源码 Broker-ConsumerFilterManager 消费者数据过滤管理组件源码分析
  • 数据挖掘-07-航空公司客户价值分析(包括数据和代码)
  • 浏览器 css 默认的字体图表
  • JAVA:注册表窗口的实现
  • Liunx Centos 防火墙操作
  • VirtualBox 和 Vagrant 快速安装 Centos7 报错
  • 使用Python进行数学四则运算