RestTemplate上傳、下載文件實例代碼
更新時間:2025年02月10日 08:54:48 作者:淋雨一直走啊
介紹了文件上傳和下載的基本方法,包括處理本地文件和文件流,上傳時區(qū)分了文件是否在本地,下載時強調返回值為byte[],并提供了工具類進行進一步處理
上傳
上傳文件分兩種
- ①要處理的文件本身存在當前文件系統(tǒng)下
- ②要處理的文件只是個臨時文件,或者說是文件流的形式存在
在以下代碼中會分別標注
private static void upAttach() { String uploadUrl = "http://192.168.1.10/upload"; String appId = ""; String accessKey = ""; String filePath = ""; String originalFilename = "test.xlsx"; long currentTimeMillis = System.currentTimeMillis(); String timeMillis = String.valueOf(currentTimeMillis); // 文件路徑 String base64key = Base64.encodeBase64String(filePath.getBytes()); String sign = getSign(appId, base64key, timeMillis, accessKey); MultiValueMap<String, Object> params = new LinkedMultiValueMap<>(3); // @Deprecated // Write to temporary file and then process, need to be deleted and tends to generate too much garbage // Path tempFile = Files.createTempFile("upload-file", ".txt"); // Files.write(tempFile, file.getBytes()); // params.add("file", new FileSystemResource(tempFile.toFile())); // ============================== 這里是重點 ============================== // ① 文件存在,直接獲取文件輸入流即可 File file = new File("c:\\test.xlsx"); params.add("file", new InputStreamResource(file.getInputStream(), originalFilename)); // ② 臨時文件,如MultipartFile,獲取其字節(jié)流來處理 MultipartFile multipartFile = null; byte[] byteArray = multipartFile.getBytes(); ByteArrayResource byteArrayResource = new ByteArrayResource(byteArray) { @Override public String getFilename() { // 這里不指定文件名的話,上傳過去的文件名會亂碼 return originalFilename; } }; params.add("file", byteArrayResource); // ======================================================================== // 其他參數(shù)按需采用 // key為文件路徑 params.add("key", filePath); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.MULTIPART_FORM_DATA); headers.add("x-fs-timestamp", timeMillis); headers.add("x-fs-appid", appId); headers.add("x-fs-sign", sign); HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers); RestTemplate restTemplate = SpringUtils.getBean(RestTemplate.class); restTemplate.postForObject(uploadUrl, requestEntity, JSONObject.class); } /** 加密 **/ private static String getSign(String appId, String base64key, String timestamp, String accessKey) { String sign = appId + "|" + base64key + "|" + timestamp; return new HmacUtils(HmacAlgorithms.HMAC_SHA_256, accessKey).hmacHex(sign); }
下載
下載只需要注意返回值為byte[]
即可:
// resource為文件唯一標識,按需采用 public static final byte[] dwAttach(String resource) { String appId = ""; String accessKey = ""; long currentTimeMillis = System.currentTimeMillis(); String timeMillis = String.valueOf(currentTimeMillis); String base64key = Base64.encodeBase64String(resource.getBytes()); String sign = getSign(appId, base64key, timeMillis, accessKey); String downloadUrl = "http://192.168.1.10?appid={appId}×tamp={timestamp}&sign={sign}"; HashMap<String, Object> uriVariables = new HashMap<>(5); uriVariables.put("appId", appId); uriVariables.put("timestamp", timeMillis); uriVariables.put("sign", sign); RestTemplate restTemplate = SpringUtils.getBean(RestTemplate.class); return restTemplate.getForObject(downloadUrl, byte[].class, uriVariables); }
后續(xù)可通過new ByteArrayInputStream(byte[])
轉換成流,或者org.springframework.util.FileCopyUtils
, org.apache.commons.io.FileUtils
等工具類,進行其他處理。
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
springMVC的RequestMapping請求不到路徑的解決
這篇文章主要介紹了springMVC的RequestMapping請求不到路徑的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08深入分析java并發(fā)編程中volatile的實現(xiàn)原理
這篇文章主要介紹了深入分析java并發(fā)編程中Volatile的實現(xiàn)原理,涉及Volatile的官方定義,實現(xiàn)原理,使用優(yōu)化等相關內容,具有一定參考價值,需要的朋友可以了解下。2017-11-11