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;
}

 


版权声明:本文为lianzhang861原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。