Spring文件上传/下载案例代码

编程

/**

* @author zhiwei_yang

* @time 2020-6-18-8:29

*/

@RestController

@RequestMapping("/file")

@Slf4j

public class FileController {

/**

* 文件下载

*

* @param downPath

* @return

*/

@PostMapping("/down")

public ResponseEntity<byte[]> down(@RequestParam("downPath") String downPath) {

if (StringUtils.isEmpty(downPath)) {

return ResponseEntity.notFound().build();

}

String fileName = downPath.substring(downPath.lastIndexOf(File.separator) + 1);

InputStream inputStream = null;

try {

if (StringUtils.startsWithIgnoreCase(downPath, ResourceUtils.FILE_URL_PREFIX)) {

inputStream = new FileInputStream(ResourceUtils.getFile(downPath));

} else if (StringUtils.startsWithIgnoreCase(downPath, "http")) {

inputStream = ResourceUtils.getURL(downPath).openStream();

} else {

inputStream = this.getClass().getClassLoader().getResourceAsStream(downPath);

}

if (null == inputStream) {

return ResponseEntity.notFound().build();

}

byte[] data = new byte[inputStream.available()];

IOUtils.read(inputStream, data);

HttpHeaders headers = new HttpHeaders();

headers.add(HttpHeaders.CONTENT_TYPE, "application/octet-stream");

headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + fileName);

log.info("文件下载成功 filePath:{}, 大小:{}", downPath, data.length);

return ResponseEntity.ok().headers(headers).body(data);

} catch (IOException ioException) {

log.error("文件下载故障 path:{}", downPath);

return ResponseEntity.unprocessableEntity().build();

} finally {

IOUtils.closeQuietly(inputStream);

}

}

/**

* 文件上传: MultipartResolver解析

* @param httpServletRequest 请求

* @return

*/

@PostMapping("/upload/simple")

public Boolean uploadFile(HttpServletRequest httpServletRequest, @RequestParam("uploadPath") String uploadPath) {

//多文件头请求: 文件上传

if (httpServletRequest instanceof MultipartHttpServletRequest) {

MultiValueMap<String, MultipartFile> multipartFileMultiValueMap = ((MultipartHttpServletRequest) httpServletRequest).getMultiFileMap();

if (CollectionUtils.isEmpty(multipartFileMultiValueMap)) {

return false;

}

File uploadDir = new File(uploadPath);

if (!uploadDir.exists()) {

uploadDir.mkdirs();

}

if (uploadDir.isFile()) {

uploadDir.delete();

uploadDir.mkdirs();

}

for (Map.Entry<String, MultipartFile> entry : multipartFileMultiValueMap.toSingleValueMap().entrySet()) {

MultipartFile multipartFile = entry.getValue();

FileOutputStream fileOutputStream = null;

try {

fileOutputStream = new FileOutputStream(uploadPath + File.separator + multipartFile.getOriginalFilename());

fileOutputStream.write(multipartFile.getBytes());

fileOutputStream.flush();

log.info("表单字段名:{},文件【{}】上传成功", entry.getKey(), multipartFile.getOriginalFilename());

} catch (IOException exception) {

log.error("文件[{}]上传失败", multipartFile.getOriginalFilename(), exception);

return false;

} finally {

IOUtils.closeQuietly(fileOutputStream);

}

}

return true;

}

return false;

}

/**

* 文件上传: MultipartResolver解析

* @param multipartFile 上传文件

* @return

*/

@PostMapping("/upload/advance")

public Boolean uploadFile(@RequestParam("multipartFile") MultipartFile multipartFile, @RequestParam("uploadPath") String uploadPath) {

File uploadDir = new File(uploadPath);

if (!uploadDir.exists()) {

uploadDir.mkdirs();

}

if (uploadDir.isFile()) {

uploadDir.delete();

uploadDir.mkdirs();

}

try {

//multipartFile 默认文件数据持久化API

multipartFile.transferTo(new File(uploadPath + File.separator + multipartFile.getOriginalFilename()));

log.info("表单字段名:{},文件【{}】上传成功", multipartFile.getName(), multipartFile.getOriginalFilename());

return true;

} catch (IOException exception) {

log.error("文件[{}]上传失败", multipartFile.getOriginalFilename(), exception);

return false;

}

}

}

以上是 Spring文件上传/下载案例代码 的全部内容, 来源链接: utcz.com/z/517566.html

回到顶部