当前位置:网站首页>About file upload and download
About file upload and download
2022-07-19 06:59:00 【He is a rookie programmer】
If it's just a simple file upload and download, it's still relatively simple . The code is as follows :
Upload files :
@PostMapping("/uploadFile")
@ApiOperation(" Upload report file ")
public ResponseResult uploadAudio(@RequestParam(value = "file") MultipartFile file) {
if (file.isEmpty()) {
return ResponseResult.failed(" The file cannot be empty ");
}
// File address does not exist , Create
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
// file extension
String prefix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
//if (".mp3".equals(prefix) || ".m4a".equals(prefix) || ".wav".equals(prefix)) {
// String fileName = UUID.randomUUID().toString().replaceAll("-", "") + prefix;
String fileName = file.getOriginalFilename();
File newFile = new File(path + fileName);
log.info(" Upload path :" + path);
log.info(" file name :" + fileName);
try {
file.transferTo(newFile);
// request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+
String url = dbPath + fileName;
Map<String,Object> map=new HashMap<>();
map.put("url",url);
map.put("name",fileName);
return ResponseResult.ok(map, " Upload successful ");
} catch (IOException e) {
e.printStackTrace();
return ResponseResult.failed(" Upload failed ");
}
// }
// return ResponseResult.failed(" The file format does not meet the requirements ");
}File download :
@GetMapping("/exportFile")
@ApiOperation(" Download the file ")
public void exportQualityReport(@RequestParam("id") Integer id,HttpServletResponse response)throws IOException{
InputStream inputStream = null;
ServletOutputStream outputStream = null;
if (id!=null){
QualityReportVo vo=reportService.getById(id);
if (vo!=null){
try {
String fileName = vo.getFileName();
String path1 =locationPre+vo.getReportLocation();
Resource resource = new DefaultResourceLoader().getResource("file:" + path1);
FileConvertUtil.responseInit(response, fileName);
inputStream = resource.getInputStream();
outputStream = response.getOutputStream();
IOUtils.copy(inputStream, outputStream);
response.flushBuffer();
} catch (Exception e) {
e.printStackTrace();
throw new IOException(" Download error ");
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
if (inputStream != null) {
inputStream.close();
}
} catch (Exception e) {
throw new CustomException(" Download error ");
}
}
}else {
throw new IOException(" file id non-existent ");
}
}else {
throw new CustomException("id Can't be empty ");
}
}I forgot to make up an important point , Is to set the download rules , That is, coding , What kind of image to download
/**
* Set file download rules
*
* @param response
* @param fileName
*/
public static void responseInit(HttpServletResponse response, String fileName) {
response.reset();
response.setHeader("Content-Disposition", "attachment;filename=" + new
String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
response.setContentType("application/octet-stream; charset=utf-8");
response.setCharacterEncoding("UTF-8");
}
Just use your own path to upload files , The path in the code is because it is stored , Show only relative paths , So when taking a path , It needs to be prefixed for processing . Then I configured the path on the configuration file .

边栏推荐
- mysql的复习总结
- Libevent report undefined reference to `getrandom‘
- 【自动化测试】——robotframework实战(三)编写测试用例
- 破译密码(综合)
- freebsd12 安装gnome3图形界面
- 邮资范围(数组 or +函数)
- Release nohup Out disk space occupied
- [automated testing] - robotframework practice (I) building environment
- 阿里云、腾讯云、华为云、Ucloud(优刻得)、天翼云 的云服务器性能测试和价格对比
- 103.53.124.X IP段BGP线路和普通的专线有什么区别
猜你喜欢

STEAM游戏高主频i9-12900k 搭建CS:GO服务器

搭建一个网站都需要那些东西

关于文件上传下载问题

Slag learning road (2) pure white direction: win Server 2003 server building

ssh远程登录服务

数据保护/磁盘列阵RAID保护 IP段103.103.188.xxx

Temperature acquisition experiment and experimental experience based on I2C

文本三剑客之awk命令--截取

ARM服务器搭建 我的世界(MC) 1.18.2 版私服教程

中国知网论文free下载的方法
随机推荐
Decipher password (comprehensive)
企业数据治理之勒索病毒篇
OLED data display based on spi/iic interface
Release nohup Out disk space occupied
Yuanzi racehorse.
FreeBSD 12 国内源提速 pkg 和 ports 做个笔记
ARM开发环境入门
X11 forwarding
[automated testing] - robotframework practice (I) building environment
怎么知道网络是否需要用高防服务器?怎么选择机房也是很重要的一点以及后期业务的稳定性
opensuse 安装网易云音乐(tumbleweed)(leap)
103.53.124.X IP段BGP线路和普通的专线有什么区别
Évaluation des performances de la machine virtuelle Tianyi Cloud Hangzhou (VPS)
Steam游戏服务器配置选择 IP
上课啦要点名啊
Xiaodi network security - Notes (4)
Good partner of single chip microcomputer - CS Genesis SD NAND flash
Wireshark packet capturing: error analysis
Sed command of text three swordsman -- text replacement; Grep command - text filtering
Minecraft基岩版BDS开服教程