上傳檔案到實體路徑并下載下傳
-
上傳檔案到伺服器實體路徑,以便下載下傳使用
接口如下:/** * * @TODO: 小程式上傳單個檔案到伺服器 * @param request * @param file 檔案 * @throws IOException * @return(展示方法參數和傳回值) */ @ResponseBody @RequestMapping("/fileUpload") public String fileUpload(@RequestParam("file") MultipartFile file){ Map<String, Object> res = new HashMap<String, Object>(); String fileName = UUID.randomUUID().toString().replaceAll("-", "") + getSuffix(file); // 判斷是否有檔案 if (StringUtils.isNotBlank(fileName)) { String uploaddir= "d:/upload/"; File upload = new File(uploaddir); if(!upload.exists()) { upload.mkdirs(); } File fileinfo=new File(String.format("%s%s", uploaddir, fileName)); try { FileUtils.writeByteArrayToFile(fileinfo, file.getBytes()); } catch (IOException e) { e.printStackTrace(); } res.put("fileName", fileName); } res.put("success", "success"); String json = JSON.toJSONString(res); return json; }
該接口實作的是檔案上傳到指定路徑(“d:/upload/”)下,并傳回生成的檔案名用于下載下傳
-
下載下傳實體路徑中的圖檔
/** * * @TODO: 下載下傳圖檔 * @param fileId * @return(展示方法參數和傳回值) */ @ResponseBody @RequestMapping("/getImg") public BaseResponse getFile(@RequestParam("fileId") String fileId) { //檔案上傳到伺服器之後,檔案路徑儲存到資料庫中,在這裡擷取路徑以便下載下傳使用 String imgUrl = testService.selectImgUrlById(fileId); String filePath = "d:/upload/" + imgUrl; String imgBase64 = WechatDownload.downloadCheckEquipmentImg(filePath); return BaseResponseUtil.success("data:image/png;base64," + imgBase64); }