httpclient 實現文件上傳中轉

開發功能:?
web前端提交上傳文件 —> a服務器接收 —> 轉發到b服務器進行文件處理?
下面是簡單實現的代碼,具體細節優化根本自己的需求更改。

    public String handleResponse(HttpServletRequest request, HttpServletResponse response)throws UnsupportedEncodingException, IOException {String method = request.getMethod();String url = "b服務器的api url";if (method.equals("POST")) { String contentType = "application/json; charset=UTF-8"; if (request.getContentType() != null) contentType = request.getContentType();// 會獲取到空指針 Map<String, String[]> tmp = new HashMap(request.getParameterMap()); if (contentType.toLowerCase().startsWith("multipart/")) { MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class); MultipartFile file = multipartRequest.getFile("file"); return httpClientUpload(url, file, tmp); } } return null; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
public String httpClientUpload(String url, MultipartFile file, Map<String, String[]> params)throws ClientProtocolException, IOException {HttpClient httpclient = new DefaultHttpClient();// 請求處理頁面 HttpPost httppost = new HttpPost(url); // 創建待處理的文件 String fileName = file.getOriginalFilename(); ContentBody files = new ByteArrayBody(file.getBytes(), fileName); // 對請求的表單域進行填充 MultipartEntity reqEntity = new MultipartEntity(); reqEntity.addPart("file", files); if (params != null) {//這里草草處理values[] for (String key : params.keySet()) { String[] values = params.get(key); for (int i = 0; i < values.length; i++) { String value = values[i]; try { value = URLEncoder.encode(value, "UTF-8"); reqEntity.addPart(key, new StringBody(value)); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } // 設置請求 httppost.setEntity(reqEntity); // 執行 HttpResponse response = httpclient.execute(httppost); if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) { HttpEntity entity = response.getEntity(); return EntityUtils.toString(entity, Charset.forName("UTF-8")); } return null; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244545
http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244541
http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244538
http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244527
http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244528
http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244529
http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244530

轉載于:https://www.cnblogs.com/sy646et/p/7266017.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/281084.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/281084.shtml
英文地址,請注明出處:http://en.pswp.cn/news/281084.shtml

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

相關文章

AngularJS $watch 性能殺手

雙向綁定是AngularJS核心概念之一&#xff0c;它給我們帶來了思維的轉變&#xff0c;不再是以DOM為驅動&#xff0c;而是以Model為核心&#xff0c;View中寫上聲明式標簽&#xff08;指令或{{}}&#xff09;,AngularJS會在后臺默默同步View到Model,并將Model的變化更新到View。…

ipad和iphone切圖_如何在iPhone和iPad上的Messages App中固定對話

ipad和iphone切圖Khamosh PathakKhamosh PathakBetween updates from your bank and group chats, the Messages app on your iPhone or iPad can be a mess. Use the pinned conversations feature introduced in iOS 14 and iPadOS 14 to access your favorite conversations…

這個WPF的企業級MES項目爆火,就是UI爭議大!

工業4.0時代&#xff0c;智能智造MES系統大行其道&#xff0c;然而基于.NET跨平臺的罕見&#xff01;這里有一套《.NET6WPF企業級MES實戰》教程&#xff0c;基于.NET6跨平臺開發&#xff0c;實現了MES多核心功能&#xff0c;尤其是開發框架完整&#xff0c;非常適合復用。這里分…

單調棧學習筆記

線性結構——單調棧①定義&#xff1a;棧內的元素&#xff0c;按照某種方式排序&#xff08;單調遞增或單調遞減&#xff09;如果新入棧的元素破壞了單調性&#xff0c;就彈出棧內元素&#xff0c;直到滿足單調性②優點&#xff1a;可以很方便地求出某個數左邊或者右邊第一個比…

《VMware Virtual SAN權威指南(原書第2版)》一1.5 什么是Virtual SAN

1.5 什么是Virtual SAN Virtual SAN是VMware推出的一種存儲解決方案&#xff0c;它的beta版本在2013年發布&#xff0c;2014年3月正式開放給公眾&#xff0c;并于2016年3月升級到6.2版。VSAN完全集成在vSphere中&#xff0c;它是一種基于對象的存儲系統&#xff0c;是虛擬機存…

js 控制超出字數顯示省略號

//多余顯示省略號 function wordlimit(cname, wordlength) {var cname document.getElementsByClassName(cname);for (var i 0; i < cname.length; i) {      var nowLength cname[i].innerHTML.length;if (nowLength > wordlength) {cname[i].innerHTML cname…

在Outlook 2007中查看您的Google日歷

Google Calendar is a phenomenal web application for managing your calendars, but so many of us are still forced to use Outlook at work. The good thing is you can have the best of both worlds by subscribing to your Google Calendar from Outlook. Google日歷是…

元宇宙、數字孿生和企業NFT

昨天參加了華為云上海開發者日活動&#xff0c;并客串主持了一場"元宇宙技術創新和商業實踐之路"的閉門研討會。研討會上大家討論熱烈&#xff0c;干貨多多&#xff0c;大家提到元宇宙的企業級前景、數字藏品和數字人案例的親身體會。在會上盆盆分享了自己關于企業級…

設置狀態欄和標題欄的樣式

設置狀態欄和標題欄的樣式Android setSystemUiVisibility(visible)方法詳解這個方法可以詳細的設置各種標題欄的狀態欄的樣式.visible的值來決定1.SYSTEM_ UI_ FLAG_ LOW_ PROFILE: 影藏不重要的狀態欄圖標&#xff0c;導航欄中相應的圖標都變成了一個小點。點擊狀態欄或者標題…

CMD命令硬盤/光驅掛載

使用Mountvol命令掛載時&#xff0c;發現GUID不對啊&#xff0c;哪應該到哪找呢&#xff1f; 1.首先可以用Mountvol命令&#xff1a; Mountvol 創建、刪除或列出卷的裝入點。Mountvol 是一種不需要驅動器號而連接卷的方式。 語法&#xff1a; mountvol [Drive:]Path VolumeName…

紐約大街上的免費WiFi,終于鋪起來了

紐約市的城市互聯網項目終于開始動工了。 這個被稱為 LinkNYC 的網絡服務項目&#xff0c;是將現有的 1 萬多個付費電話亭改造成提供 Wi-Fi 網絡的“熱點樁”&#xff0c;為紐約市民提供免費網絡。從 12 月 28 日開始&#xff0c;工人們已經開始安裝首批的 LinkNYC 熱點樁了&am…

解決Maven管理項目update Maven時,jre自動變為1.5

本文為博主原創&#xff0c;未經允許不得轉載&#xff1a; 在搭建一個maven web項目時&#xff0c;項目已經按步驟搭建完好&#xff0c;之后項目上就報了一個錯誤。 在控制臺看到錯誤提示如下&#xff1a;Dynamic Web Module 3.0 requires Java 1.6 or newer。。 已經改過項目中…

reddit_如何將多個子Reddit與多個Reddit合并

redditchrisdorney/Shutterstock.comchrisdorney / Shutterstock.comIf you’re subscribed to a lot of communities on Reddits, some of the content you want to see may get lost in the mix. For easier browsing, you can make your own “multireddit” that combines …

BeetleX之ServerBuilder對象使用

ServerBuilder是BeetleX新版本添加對象&#xff0c;用于進一步簡化TCP服務的構建。ServerBuilder對象提供兩個泛型版本&#xff1a;一個是針對網絡數據流操作&#xff0c;另一個則針對協議解釋器的對象處理操作。網絡數據流當需要解釋簡單的網絡數據流時使用ServerBuilder<A…

Unbuntu 自動重啟MySQL

上個月&#xff0c;通過Unbuntu搭建了WordPress&#xff0c;一切運行良好。 UBUNTU搭建WORDPRESS-MYSQL-APACHE 但是&#xff0c;最近幾天&#xff0c;不知道啥情況&#xff0c;MySQL偶爾會出現Stop&#xff1b;影響了blog的使用&#xff0c;所以&#xff0c;我這里嘗試了自動調…

識別Win10系統兩種方法

最近寫寫一個工具&#xff0c;需要識別當前系統。 首先&#xff0c;找到GetVersionEx函數&#xff0c;能識別win7和win8。但win10需要修改manifested&#xff0c;才能識別&#xff0c;具體參考如下鏈接&#xff1a; http://blog.csdn.net/k1988/article/details/47614529 實…

solidworks小金球_如何在沒有電纜的情況下傳送第77屆年度金球獎

solidworks小金球Gil C / Shutterstock吉爾C / ShutterstockAs the 77th annual Golden Globes Awards approach, you may be wondering how to watch it without paying a cable bill. These streaming services are the best way to watch the awards show tonight if you cu…

2017年,這兩個大數據崗位一定會火!

討論哪個大數據崗位會火之前&#xff0c;我們先來簡單的分析一下大數據領域的行情&#xff0c;這里重點說一下當前的情況。 2016年&#xff0c;互聯網行業遇到了資本寒冬&#xff0c;拋開大公司不說&#xff0c;一些中小型的公司不斷的縮減預算&#xff0c;因為很難融到錢。 但…

PHP7 學習筆記(十一)使用phpstudy快速配置一個虛擬主機

說明&#xff1a;為了windows本地開發php方便&#xff0c;這里推薦使用PHP集成環境phpstudy。 目的&#xff1a;使用域名訪問項目&#xff08;tinywan.test&#xff09; 1、官網&#xff1a;http://www.phpstudy.net 2、虛擬主機的配置 3、站點域名管理 &#xff08;1&#xff…

962-最大寬度坡

前言 Weekly Contest 116 的最大寬度坡&#xff1a; 給定一個整數數組 A&#xff0c;坡是元組 (i, j)&#xff0c;其中 i < j 且 A[i] < A[j]。這樣的坡的寬度為 j - i。 找出 A 中的坡的最大寬度&#xff0c;如果不存在&#xff0c;返回 0 。 示例1&#xff1a; 輸入&am…