代码
亲测可用
@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(); } } }