文件上传下载 java 案例_Spring 文件上传/下载 案例代码

/**

* @author zhiwei_yang

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

*/

@RestController

@RequestMapping("/file")

@Slf4j

public class FileController {

/**

* 文件下载

*

* @param downPath

* @return

*/

@PostMapping("/down")

public ResponseEntity 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 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 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;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值