@PostMapping("/downLoadFile/{id}")
public String downLoadFile (@PathVariable Long id,@RequestParam("file") MultipartFile file){
try {
// File mk = new File("D:\\test\\");
// if (!mk.mkdirs()) {
// mk.mkdirs();
// }
// // 获取上传的文件名
String fileName = StringUtils.cleanPath(file.getOriginalFilename());
// 保存文件到服务器
Path path = Paths.get("D:\\test\\", fileName);
String pathh = "D:\\test\\" + fileName;
File file1 = new File(pathh);
Files.copy(file.getInputStream(), path, StandardCopyOption.REPLACE_EXISTING);
ModelDataBean modelDataBean = modelDataMapper.selectById(id);
String modelurl = modelUrl + fileName;
modelDataBean.setModelUrl(modelurl);
modelDataMapper.updateById(modelDataBean);
return modelurl;
// return response;
}catch (Exception e) {
e.printStackTrace();
return null;
}
}
@GetMapping("/download-glb/{fileName:.+}")
public ResponseEntity<byte[]> downloadGlbFile(@PathVariable String fileName) {
try {
// 加载文件作为字节数组
Path filePath = Paths.get(FILE_STORAGE_PATH).resolve(fileName);
byte[] fileContent = Files.readAllBytes(filePath);
// 设置响应头,告知浏览器这是一个可下载的文件
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", fileName);
return ResponseEntity.ok()
.headers(headers)
.body(fileContent);
} catch (IOException e) {
// return ResponseEntity.status(404).body("File not found: " + fileName);
return null;
}
}
前端传文件java路径url文件下载
于 2024-04-26 10:49:26 首次发布