springboot接受表单multpartFile并使用HttpClient发送post请求模拟表单上传文件(multipart/form-data)

现在有个需求,就是表单那边提交上来文件,我后台需要获取文件流并向另一个服务器发起请求将文件传给另一个服务器。

后台使用springboot,spring接收文件流一般都会转换成MultipartFile类型

maven:

 <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.10</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.6</version>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.3.2</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>

 

// 上传文件
@RequestMapping(value = "upload1")
@ResponseBody
public Object upload1(
        @RequestParam(value = "files") MultipartFile[] files,  //这样接收文件
        @RequestParam(value = "file")  MultipartFile file,  //这样接收文件
        @RequestParam Map<String,Object> params,
        HttpServletRequest request
) {
    RetBase ret = new RetBase();
    
    File file2 = null;
    String fileName = file.getOriginalFilename();
    String prefix = fileName.substring(fileName.lastIndexOf("."));
    List<Map<String,Object>> data = new ArrayList<>();
    try {
        file2 = File.createTempFile(fileName, prefix);
        // 将上传文件复制到临时文件
        //Springboot中直接用transferTo会报找不到路径的错,得复制一份才行
        FileUtils.copyInputStreamToFile(file.getInputStream(),file2);
        HashMap param = new HashMap();
        //调用远程发送接口
        String content=getRemoteInterfaceStr("upload",param,file2);
        // 返回前台
        ret.setData(data);
        ret.setCode("0");
        ret.setMsg("上传成功");
        ret.setSuccess(true);
        return ret;

    } catch (Exception e) {
        e.printStackTrace();
        ret.setCode("-10");
        ret.setMsg("上传失败");
        ret.setSuccess(false);
        return ret;
    }

}
public String getRemoteInterfaceStr1(String actionName,HashMap params,File file1) {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost post = new HttpPost("http://test.cnmaker.top/data.cfrc?action=" + actionName);
    try {
        // 将上传文件复制到临时文件
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        ContentType strContent = ContentType.create("text/plain", Charset.forName("UTF-8"));
        builder.addTextBody("attachmentType","certificate" , strContent);
        FileBody bin = new FileBody(file1);
        builder.addPart("file", bin);
        //传文字参数
        // builder.addPart("attachmentType",
        //         new StringBody("certificate", ContentType.create("text/plain", Consts.UTF_8)));
        HttpEntity parameterEntity = builder.build();
        //这里不要自己制定header,它会自己模拟,你自己指定了就会报错
        //post.setHeader("Content-type", "multipart/form-data");
        post.setEntity(parameterEntity);
        CloseableHttpResponse response = httpClient.execute(post, HttpClientContext.create());
        String content = EntityUtils.toString(response.getEntity());
        //插入对应的表

        return content;
    } catch (Exception e) {
        e.printStackTrace();
    }finally {
        // 操作完上面的文件 需要删除在根目录下生成的临时文件
        MultipartFileToFile.deleteTempFile(file1);
    }
    return null;
}

 

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豆趣编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值