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.mzph.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;是虚拟机存…

在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;大家提到元宇宙的企业级前景、数字藏品和数字人案例的亲身体会。在会上盆盆分享了自己关于企业级…

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…

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…

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…

.NET跨平台框架选择之一 - Avalonia UI

本文阅读目录1. Avalonia UI简介Avalonia UI文档教程&#xff1a;https://docs.avaloniaui.net/docs/getting-started随着跨平台越来越流行&#xff0c;.NET支持跨平台至今也有十几年的光景了(Mono[1]开始)。但是目前基于.NET[2]的跨平台&#xff0c;大多数还是在使用B/S架构的…

网络串流_串流NBA篮球的最便宜方式(无需电缆)

网络串流I love NBA basketball. Every year, I get really excited around the beginning of September because I know tip-off is approaching. This year, I also had to figure out how I’m going to watch the Bulls (lose almost every game) with a combination of st…

你认识的C# foreach语法糖,真的是全部吗?

本文的知识点其实由golang知名的for循环陷阱发散而来&#xff0c; 对应到我的主力语言C#&#xff0c; 其实牵涉到闭包、foreach。为了便于理解&#xff0c;我重新组织了语言&#xff0c;以倒叙结构行文。先给大家提炼出一个C#题&#xff1a;观察for、foreach闭包的差异左边输出…

C#对window 硬件类操作,ManagementObjectSearcher

原文转载&#xff1a;http://blog.csdn.net/da_keng/article/details/50589145 纯属转载&#xff0c;复制过来方便编程时寻找。感谢作者&#xff1a;I-Awakening复制前补充&#xff1a; 在刚学C#&#xff0c;用ManagementObjectSearcher 竟然不能解析到头文件&#xff0c;需要手…

twitter批量取消关注_如何在Twitter上取消阻止“潜在敏感内容”

twitter批量取消关注Twitter推特Twitter blocks some tweets with a “potentially sensitive content” warning. You can disable this warning—even on an iPhone or iPad, where the option isn’t normally available. You can also disable sensitive content warnings …

Semantic-UI的React实现(二):CSS类构造模块

更简单的类名标签 Semantic-UI使用了更简单的类名声明。用过Bootstrap的同学都会被其复杂的类名标签折磨过&#xff0c;例如一个简单的按键样式&#xff0c;不论颜色或是大小&#xff0c;都需要btn-前缀声明&#xff1a; <button type"button" class"btn btn…