HttpClient多文件上传

发布时间:2020-07-23 07:54:52 作者:wyait
来源:网络 阅读:1223

这辈子没办法做太多事情,所以每一件都要做到精彩绝伦!

People can't do too many things in my life,so everything will be wonderful 

 

项目使用技术SpringMVC + spring + mybatis

HttpClient技术

1       HttpClientService工具类

该工具类封装了getpostputdelete以及post上传多个文件等方法;包含了有无参数。日常开发,够用了!

一般来说,有些公司会有自己独立的上传下载文件服务器;有些是单应用服务!以上这两种情况,暂用不到该方法。

 

本文重点讲解使用httpClient进行post请求多文件上传功能(底层是模拟表单提交)。

<dependency>

                            <groupId>org.apache.httpcomponents</groupId>

                            <artifactId>httpclient</artifactId>

<version>4.3.5</version>

                   </dependency>

<dependency>

                            <groupId>org.apache.httpcomponents</groupId>

                            <artifactId>httpmime</artifactId>

                            <version>4.3.5</version>

                   </dependency>

//注入httpClientrequestConfig(spring集成httpClient配置)

@Autowired

         privateCloseableHttpClient httpClient;

 

         @Autowired

         privateRequestConfig requestConfig;

/**

          *

          * @描述:httpCilent多图片上传和多个参数

          * @创建人:wyait

          * @创建时间:201752 下午1:47:41

          * @param url 请求url

          * @param params 请求参数

          * @param files file对象

          * @return

          * @throws IOException

          */

         publicHttpResult postUploadFile(String url, Map<String, Object> params,

                            Map<String,File> files) throws IOException {

                   HttpPosthttpPost = new HttpPost(url);// 创建 HTTP POST 请求

                   httpPost.setConfig(this.requestConfig);

                   MultipartEntityBuilderbuilder = MultipartEntityBuilder.create();

                   builder.setCharset(Charset.forName("UTF-8"));//设置请求的编码格式

                   builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);//设置浏览器兼容模式

                   //设置参数

                   if(files != null) {

                            //设置图片参数

                            for(Map.Entry<String, File> entry : files.entrySet()) {

                                     builder.addBinaryBody(entry.getKey(),entry.getValue());

                            }

                   }

                   //设置参数

                   if(params != null) {

                            //设置post参数

                            for(Map.Entry<String, Object> entry : params.entrySet()) {

                                     //指定编码,防止中文乱码问题。但是某些情况下会导致上传失败

                                     builder.addTextBody(entry.getKey(),

                                                        String.valueOf(entry.getValue()),

                                                        ContentType.create("text/plain","UTF-8"));

                            }

                   }

                   //生成 HTTP POST 实体

                   HttpEntityentity = builder.build();

                   //设置请求参数

                   httpPost.setEntity(entity);

                   CloseableHttpResponseresponse = null;

                   try{

                            //执行请求

                            response= httpClient.execute(httpPost);

                            if(response.getEntity() != null) {

                                     returnnew HttpResult(response.getStatusLine().getStatusCode(),

                                                        EntityUtils.toString(response.getEntity(),"UTF-8"));

                            }

                            returnnew HttpResult(response.getStatusLine().getStatusCode(),

                                               null);

                   }finally {

                            if(response != null) {

                                     response.close();

                            }

                   }

         }

2       业务场景

现有A前台系统和B服务系统;业务流程:

JSP页面提交文件—>A系统(发送httpClient)—>B系统进行更新数据!

考虑的方案:

1,直接跳过A系统,jsp页面请求B更新数据;

2A系统进行文件保存后,将路径带到B系统进行更新操作;

以上两种方案的问题点;

方案1,线上B服务,一般是内网服务,不对外开放;否则会有安全问题;

方案2,涉及分布式事务问题;

在网上也百度了很多方法,最终退而求其次,进行两次读写操作,更新数据!

 

3       实现流程

3.1    jsp页面上传文件

此处略

3.2    A系统处理文件

主要功能是:A系统接收文件,合法性校验后,对图片进行判断和压缩处理,生成一个临时文件;

对于对图片的判断处理这里就不做过多说明了。

重点说一下:

MultipartFile 转成 File对象实现(结合网上资料,总结出最佳实践):

                   Stringpath=”自定义”+MultipartFile. getOriginalFilename();

File newFile=new File(path);

        //直接写文件到指定路径下

        MultipartFile.transferTo(newFile);

 

3.3    调用httpCilent发送请求

主要功能是:调用httpClient已经封装好的postUploadFileur,params,files)方法,发送请求;

Map<String, File> files = new HashMap<String, File>();

                   files.put("newFile", newFile);

                   //发送请求,并对请求数据进行处理,params自定义

Obj 自定义=httpCilentService.postUploadFile(url, params, files);

3.4    B系统处理数据

B系统通过MultipartFile接收文件数据,并进行更新操作,返回结果;

HttpClient多文件上传

// file

                            FilenewFile = new File(newFile);

                            //写文件到磁盘

                            newPic.transferTo(newFile);

3.5    A系统回调处理数据

A系统在调用B系统后,无论结果ok,还是fail。都删除临时图片;

将该段代码写在finally代码块中:

boolean flag = new File(path).delete();

                                     if(!flag) {

                                               //删除原始图片失败

                                               return"删除临时图片失败,请稍后再试";

                                     }

 

最后,返回结果到jsp页面

 

关于HttpClient工具类最好链接:http://blog.csdn.net/column/details/httpclient-arron.html

 


推荐阅读:
  1. Httpclient处理摘要认证
  2. httpclient请求

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

http client clien

上一篇:前端完整详细学习路线

下一篇:PostgreSQL编译安装

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》