服务器中的文件上传下载是基本操作,图片、用户头像的上传下载、文件的传输和资源分享等已经成为每个应用必不可少的功能。在我的认知中,服务器的文件存储下载简单流程是这样的:上传文件->在数据库中记录该文件的相关信息(文件名、大小、路径等)->提供文件列表->文件下载。
由于项目中只有App增量更新需要上传和下载文件,那么这里就将文件的路径固定。
上传下载页面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"></meta>
<title>文件上传</title>
</head>
<body>
<p>单文件上传</p>
<form action="upload" method="POST" enctype="multipart/form-data">
文件:<input type="file" name="file"/>
<input type="submit"/>
</form>
<hr/>
<p>文件下载</p>
<a href="download">下载最新安装包</a>
<hr/>
<p>多文件上传</p>
<form method="POST" enctype="multipart/form-data" action="batch">
<p>文件1:<input type="file" name="file"/></p>
<p>文件2:<input type="file" name="file"/></p>
<p><input type="submit" value="上传"/></p>
</form>
</body>
</html>
上传和下载控制器:
@RestController
public class FileController {
private static final Logger log = LoggerFactory.getLogger(FileController.class);
@Autowired
private VersionService versionService;
@RequestMapping(value = "/upload")
public String upload(@RequestParam("file") MultipartFile file) {
try {
if (file.isEmpty()) {
return "文件为空";
}
//获取文件名
String fileName = file.getOriginalFilename();
log.debug("上传的文件名字:" + fileName);
//获取文件的后缀名
String suffixName = fileName.substring(fileName.lastIndexOf("."));
log.debug("文件的后缀名为:" + suffixName);
// 设置文件存储路径
String filePath = "/usr/local/download/";
String path = filePath + fileName;
File dest = new File(path);
// 检测是否存在目录
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();// 新建文件夹
}
file.transferTo(dest);// 文件写入
return "上传成功";
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "上传失败";
}
@PostMapping("/batch")
public String handleFileUpload(HttpServletRequest request) {
List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
MultipartFile file = null;
BufferedOutputStream stream = null;
for (int i = 0; i < files.size(); ++i) {
file = files.get(i);
String filePath = "/usr/local/download/";
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
stream = new BufferedOutputStream(new FileOutputStream(
new File(filePath + file.getOriginalFilename())));//设置文件路径及名字
stream.write(bytes);// 写入
stream.close();
} catch (Exception e) {
stream = null;
return "第 " + i + " 个文件上传失败 ==> "
+ e.getMessage();
}
} else {
return "第 " + i
+ " 个文件上传失败因为文件为空";
}
}
return "上传成功";
}
@GetMapping("download")
public String downloadFile(HttpServletRequest request, HttpServletResponse response) {
String fileName = "app_new.apk";
if (fileName != null) {
//设置文件路径
File file = new File("/usr/local/live/download/" + fileName);
if (file.exists()) {
response.setContentType("application/force-download"); //设置强制下载不打开
//设置文件名
response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
byte[] buffer = new byte[1024];
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
OutputStream os = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
return "下载成功";
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
return "下载失败";
}
}
网友评论