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.pswp.cn/news/212040.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/212040.shtml
英文地址,請注明出處:http://en.pswp.cn/news/212040.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【接口分享】熱門好用的API,含免費次數

語音驗證碼短信&#xff1a;撥打電話告知用戶驗證碼&#xff0c;實現信息驗證。短信驗證碼&#xff1a;可用于登錄、注冊、找回密碼、支付認證等等應用場景。支持三大運營商&#xff0c;3秒可達&#xff0c;99.99&#xff05;到達率&#xff0c;支持大容量高并發。通知短信&…

基于SSM的點餐系統的設計與實現

末尾獲取源碼 開發語言&#xff1a;Java Java開發工具&#xff1a;JDK1.8 后端框架&#xff1a;SSM 前端&#xff1a;Vue 數據庫&#xff1a;MySQL5.7和Navicat管理工具結合 服務器&#xff1a;Tomcat8.5 開發軟件&#xff1a;IDEA / Eclipse 是否Maven項目&#xff1a;是 目錄…

mysql設置為密碼登錄

要設置Ubuntu上的MySQL需要密碼登錄&#xff0c;你可以使用以下步驟&#xff1a; 打開終端。 輸入以下命令登錄到 MySQL 服務器&#xff1a; sudo mysql -u root -p按Enter后&#xff0c;系統會要求輸入密碼。如果是第一次登錄&#xff0c;你可能需要直接按Enter鍵&#xff08…

【已解決】解決UbuntuKali無法進行SSH遠程連接

目錄 Ubuntu20.04配置SSH遠程連接Kali Linux配置SSH遠程連接 Ubuntu20.04配置SSH遠程連接 首先更新安裝包 sudo apt-get update 下載SSH服務 sudo apt install openssh-server 查看SSH服務 service ssh status 打開 /etc/ssh/sshd_config文件修改配置文件 將PermitRootLog…

知識筆記(五十二)———MySQL 刪除數據表

MySQL中刪除數據表是非常容易操作的&#xff0c;但是你在進行刪除表操作時要非常小心&#xff0c;因為執行刪除命令后所有數據都會消失。 語法 以下為刪除 MySQL 數據表的通用語法&#xff1a; DROP TABLE table_name ; -- 直接刪除表&#xff0c;不檢查是否存在 或 DROP…

基于Debain安裝 Docker 和 Docker Compose

一、安裝Docker # 先升級一下系統 (Ubuntu / Debian 系) sudo apt-get update sudo apt-get upgrade# 如果你是 CentOS、紅帽系列則使用&#xff1a; yum update yum upgrade# 安裝 Docker curl -fsSL https://get.docker.com -o get-docker.sh sudo sh get-docker.sh二、Dock…

LeetCode 0070. 爬樓梯:動態規劃(遞推)

【LetMeFly】70.爬樓梯&#xff1a;動態規劃&#xff08;遞推&#xff09; 力扣題目鏈接&#xff1a;https://leetcode.cn/problems/climbing-stairs/ 假設你正在爬樓梯。需要 n 階你才能到達樓頂。 每次你可以爬 1 或 2 個臺階。你有多少種不同的方法可以爬到樓頂呢&#x…

NVIDIA Jetson NX ubuntu20.04刪除多余版本沖突的Boost庫

參考Ubuntu16.04 卸載舊版本Boost庫并安裝新版本 卸載 刪除/usr/local/include/boost文件夾&#xff0c;刪除/usr/local/lib中和boost有關的文件,以及/usr/local/lib/cmake/中boost的cmake文件 cd /usr/local/lib/ ls | grep boost sudo rm -rf /usr/local/include/boost su…

藍橋杯 day01 奇怪的數列 特殊日期

奇怪的數列 題目描述 奇怪的數列 從 X 星截獲一份電碼&#xff0c;是一些數字&#xff0c;如下&#xff1a; 13 1113 3113 132113 1113122113 ?? YY 博士經徹夜研究&#xff0c;發現了規律&#xff1a; 第一行的數字隨便是什么&#xff0c;以后每一行都是對上一行…

redis+springsecurity+mybtais-plus+JWT

redisspringsecuritymybtais-plusJWT 01 引入依賴 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.mysql</groupId&g…

12.視圖

目錄 1.視圖的含義與作用 2.視圖的創建與查看 1.創建視圖的語法形式 2、查看視圖&#xff1a; 1.使用DESCRIBE語句查看視圖基本信息 2.使用SHOW TABLE STATUS語查看視圖基本信息查看視圖的信息 3.使用SHOW CREATE VIEW語查看視圖詳細信息 4.在views表中查看視圖詳細信息…

【DP】Yarik and Array—CF1899C

Yarik and Array—CF1899C 一道不難的DP&#xff0c;根據代碼就能看出思路。 C o d e Code Code #include <bits/stdc.h> #define int long long #define sz(a) ((int)a.size()) #define all(a) a.begin(), a.end() using namespace std; using PII pair<int, int&…

codeforces

分析 刪去 k k k 個之后要是回文串&#xff0c;不過題目會給字符串rearrange&#xff0c;所以對于奇數個數的字符最多留一個&#xff0c;偶數的不用管。 Think Twice, Code Once #include<bits/stdc.h> #define il inline #define get getchar #define put putchar #…

SAP-PP:PP模塊新手顧問入門尋找解決方案途徑

在學習PP模塊時我們可以參考一下部分資源: SAP Help SAP Help SAP 幫助門戶是了解任何功能基礎知識的起點。在這里&#xff0c;您將了解每個功能可以做什么&#xff0c;以及如何使用每個功能的一些基本示例 F1 Help 每個事務中的每個字段都有自己的文檔&#xff0c;您可以通過…

案例015:基于微信小程序的校園防疫系統

文末獲取源碼 開發語言&#xff1a;Java 框架&#xff1a;SSM JDK版本&#xff1a;JDK1.8 數據庫&#xff1a;mysql 5.7 開發軟件&#xff1a;eclipse/myeclipse/idea Maven包&#xff1a;Maven3.5.4 小程序框架&#xff1a;uniapp 小程序開發軟件&#xff1a;HBuilder X 小程序…

wangzherongyao milaidi

王者榮耀米萊狄&#xff0c; 1&#xff09;大多數人知道的是這個英雄很能拆塔&#xff0c; 2&#xff09;他也有個致命缺陷&#xff0c;當對面有前排&#xff0c;同樣拆塔的時候&#xff0c;他也清不動線&#xff0c;而且對于前排來說他的爆發力遠沒有安其拉等爆發型順傷秒傷…

論文閱讀_反思模型_Reflexion

英文名稱: Reflexion: Language Agents with Verbal Reinforcement Learning 中文名稱: 反思&#xff1a;具有言語強化學習的語言智能體 文章: http://arxiv.org/abs/2303.11366 代碼: https://github.com/noahshinn/reflexion 作者: Noah Shinn (Northeastern University) 日期…

docker 一鍵尋找容器在服務器存儲位置

docker ps -a找到容器id/容器名稱 docker inspect 容器id/容器名稱 | grep UpperDir找出該容器在物理機的位置 inspect作用:查看docker詳細信息 cd到UpperDir所指向的地址&#xff0c;找到配置文件并修改,到這后,這個位置和你用exec命令進入容器內看到文件是一致的

AtCoder Beginner Contest 328

A - Not Too Hard (atcoder.jp) AC代碼: #include<bits/stdc.h> #define endl \n //#define int long long using namespace std; const int N10; int s[N]; int n,x; void solve() {cin>>n>>x;for(int i1;i<n;i) cin>>s[i];int ans0;for(int i1;…

go-zero 開發之安裝 etcd

本文只涉及 Linux 上的安裝。 二進制安裝 下載二進制安裝包 #ETCD_VERv3.4.28 ETCD_VERv3.5.10 DOWNLOAD_URLhttps://github.com/etcd-io/etcd/releases/download INSTALL_DIR/tmprm -f ${INSTALL_DIR}/etcd-${ETCD_VER}-linux-amd64.tar.gz rm -rf ${INSTALL_DIR}/etcd-dow…