使用RestTemplate上传文件

简介: 写作目的最近维护一个项目,里面用了RestTemplate进行服务之前的调用,不过最近有一个Excel解析的需求,百度了几篇,内容不是很全,所以写篇博客记录一下,不过我还是推荐使用Feign调用,毕竟面向接口编程,方便。

代码


亲测可用


@RestController
public class DataExcelImportController {
    private static final String REST_URL_PRFIX = "http://abc";
    @Autowired
    private RestTemplate restTemplate;
    @PostMapping("/importExcel")
    public Object explainExcel(Integer stationId, @RequestPart("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            return "文件为空";
        }
        FdcpResult result = null;
        File file1 = null;
        try {
            //转换为file
            file1 = multipartFileToFile(file);
            FileSystemResource resource = new FileSystemResource(file1);
            MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
            //参数
            param.add("file", resource);
            param.add("stationId", stationId);
            HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(param);
            ResponseEntity<FdcpResult> responseEntity = restTemplate.exchange(REST_URL_PRFIX + "/importExcel", HttpMethod.POST, httpEntity, FdcpResult.class);
            result = responseEntity.getBody();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != file) {
                //最后要删除
                file1.delete();
            }
        }
        return result;
    }
    public static File multipartFileToFile(MultipartFile file) throws Exception {
        File toFile = null;
        if (file.equals("") || file.getSize() <= 0) {
            file = null;
        } else {
            InputStream ins = null;
            ins = file.getInputStream();
            toFile = new File(file.getOriginalFilename());
            inputStreamToFile(ins, toFile);
            ins.close();
        }
        return toFile;
    }
    //获取流文件
    private static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
目录
相关文章
|
5月前
SpringMVC上传文件的三种方式
SpringMVC上传文件的三种方式
|
Java Spring
RestTemplate上传文件解决方案
当对接文件上传模块时,需要对接上传文件的接口,而我们模块的数据是以字节数组存在的(已经操作过了的字节数组,存在于内存中)接口是以form-data的形式上传的,其中需要上传MultipartFIle,如果使用MultipartFile放入到请求的 fromMap中,然后再上传这个文件,会报(ByteArrayInputStream no serialized)的错误,也就是没有注入对应的bean的错误。。
3879 0
|
7月前
|
缓存
RestTemplate请求访问简单使用
RestTemplate请求访问简单使用
44 1
|
4月前
restTemplate 发送http post请求带有文件流、参数
restTemplate 发送http post请求带有文件流、参数
63 1
|
8月前
SpringBoot-29-RestTemplate的Get请求使用详解
SpringBoot-29-RestTemplate的Get请求使用详解
56 0
SpringBoot-29-RestTemplate的Get请求使用详解
|
JSON 缓存 前端开发
|
JSON 前端开发 Java
|
前端开发 API
SpringMVC请求(下)-文件上传
SpringMVC请求(下)-文件上传
SpringMVC请求(下)-文件上传
|
Java 网络架构 Spring
详解 RestTemplate 操作
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xmt1139057136/article/details/82761642 作为开发人员,我们经常关注于构建伟大的软件来解决业务问题。
1490 0