不同公司間傳數據是件麻煩的事情,協商格式會搞死人。主機廠會利用EDI。低成本而可靠的方式是己方系統導出excel文檔,發送;對方接收,導入對方系統。看似麻煩,實際上前期協調和后期升級都會很簡單。
為了可靠,不能直接發到對方系統上,而是先注冊個網站,租個服務器,將excel發到服務器上。對方去服務器上取文件。
本地服務器上傳到遠程服務器
// 上傳文件
FileSystemResource fileSystemResource = new FileSystemResource(fileNameWithPath);
if (!fileSystemResource.exists()) {//文件不存在處理
}
MediaType type = MediaType.parseMediaType("multipart/form-data");
headers.setContentType(type);
//headers.set("Cookie", cookies);//session用於驗證身份
MultiValueMap<String, Object> form = new LinkedMultiValueMap<String, Object>();
form.add("file", fileSystemResource);
//form.add("pass", finalpass);;//用於驗證身份
HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<>(form, headers);
responseEntity = restTemplate.postForEntity(urlTransfer, files, String.class);//發送
//此處要添加錯誤處理
String result = responseEntity.getBody().toString();
從遠程服務器下載到本地服務器
HttpHeaders headers = new HttpHeaders();
HttpEntity<Resource> httpEntity = new HttpEntity<Resource>(headers);
ResponseEntity<byte[]> response = restTemplate.exchange(getFilePath, HttpMethod.GET, httpEntity, byte[].class);
if (response.getStatusCodeValue() != 200) {//異常處理
}
//寫入本地
try {
File file = new File(path + File.separator + fileName);
FileOutputStream fos = new FileOutputStream(file);
fos.write(response.getBody());
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}