HttpServletRequest接收前端文件使用HttpClient发送

使用HttpClients发送从HttpServletRequest获取的文件以及接受

一:接收发送

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.Charset;
import java.util.Map;
public void doPost(HttpServletRequest request, HttpServletResponse response) {
        GgRiskConfigValueDto poaserialno_switch2 = ServiceManager.prpall.getGgRiskConfigValueService().findByPrimaryKey("0303", "0000", "TPIS-00053");
        String url = poaserialno_switch2.getConfigValue();
        try {
            MultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
            MultipartHttpServletRequest multipartHttpServletRequest = resolver.resolveMultipart(request);
            Map<String, MultipartFile> fileMap = multipartHttpServletRequest.getFileMap();
            MultipartFile file = null;

            CloseableHttpClient httpClient = HttpClients.createDefault();
            String result = "";
            HttpEntity httpEntity = null;
            HttpEntity responseEntity = null;
            HttpPost httpPost = new HttpPost(url);
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.setCharset(Charset.forName("utf-8"));
            builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);//加上此行代码解决返回中文乱码问题

            for (Map.Entry<String,MultipartFile> entry : fileMap.entrySet()){
                file =entry.getValue();
                if (!file.isEmpty()){
                    builder.addBinaryBody("file", file.getInputStream(), ContentType.MULTIPART_FORM_DATA, file.getName());// 文件流
                    httpEntity = builder.build();
                    httpPost.setEntity(httpEntity);
                    HttpResponse response1 = httpClient.execute(httpPost);// 执行提交
                    responseEntity = response1.getEntity();
                    if (responseEntity != null) {
                        // 将响应内容转换为字符串
                        result = EntityUtils.toString(responseEntity,Charset.forName("UTF-8"));
                        JSONObject jsonObject = JSONObject.parseObject(result);
                    }
                }

            }
        } catch (Exception e) {
           e.printStackTrace();
        }

二:接收端

public Result<List<实体类>> uploadConfigFilterExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        EasyExceLlistener.getFaillist().clear();
        Long l = System.currentTimeMillis();
        
        ServletContext context = request.getSession().getServletContext();
        //将当前上下文初始化给  CommonsMultipartResolver (多部分解析器)
        CommonsMultipartResolver resolver = new CommonsMultipartResolver(context);
        //判断是否多文件上传
        if (resolver.isMultipart(request)) {
            MultipartHttpServletRequest multRequest = (MultipartHttpServletRequest) request;
            MultipartFile file = multRequest.getFile("file");
           获取文件后可以对文件进行其他操作
        }
        System.out.println("所有数据存储完毕总耗时:" + (System.currentTimeMillis() - l) / 1000 + "秒");
        return ResultUtil.success(EasyExceLlistener.getFaillist());
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值