很多人都接触过文件上传和下载,比如JavaWeb。
在这里我记录一下在Spring框架中的此类基本操作,水平有限,不喜勿喷。
首先编写好前端ftl格式的界面
<p>单文件上传</p>
<form action="upload" method="POST" enctype="multipart/form-data">
文件:<input type="file" name="file">
<input type="submit">
</form>
<p>文件下载</p>
<p><a href="download">下载文件</a></p>
<p>多文件上传</p>
<form action="batch" method="POST" enctype="multipart/form-data">
<p>文件1:<input type="file" name="file"></p>
<p>文件2:<input type="file" name="file"></p>
<p><input type="submit" value="上传"></p>
</form>
</body>
配置对外访问路径fileUpload
@GetMapping("/fileUpload")
public String fileUpload(ModelMap modelMap){
modelMap.addAttribute("msg", "文件上传下载");
return "fileUpload";
}
对于单文件上传操作,主要通过流的方式,把前端上传的文件写入到服务端指定位置。对于File中的transferTo方法,我是试了一下发现上传的文件内容为空,不知道问题出在什么地方
@RequestMapping(value = "/upload")
public String upload(@RequestParam("file")MultipartFile file){
if (file.isEmpty()){
return "file is null";
}
String fileName = file.getOriginalFilename();
log.info("upload filename: "+ fileName);
File dest = new File(filePath);
if (!dest.exists()){
dest.mkdirs();
}
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(dest,fileName)));
byte[] bytes = file.getBytes();
bos.write(bytes);
bos.close();
return "upload ok";
} catch (IOException e) {
e.printStackTrace();
}
return "upload wrong";
}
多文件上传操作类似于单文件上传操作,是单文件上传的叠加,也是通过流的方式。
@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);
if (!file.isEmpty()){
try {
byte[] bytes = file.getBytes();
stream = new BufferedOutputStream(new FileOutputStream(new File(filePath+file.getOriginalFilename())));
stream.write(bytes);
stream.close();
} catch (IOException e) {
stream = null;
return "number "+i+" upload wrong "+e.getMessage();
}
}else {
return "number "+i+" upload wrong , because file is null";
}
}
return "upload ok";
}
我这边是硬编码指定了一个下载文件,对于下载操作,要设置好响应头中的项,比如文件名,文件长度等
@GetMapping("/download")
public String downloadFile(HttpServletResponse response){
String fileName = "readme.txt";
if (fileName != null){
File file = new File(filePath+fileName);
if (file.exists()){
response.setContentType("application/force-download");//强制下载不打开
response.addHeader("Content-Disposition", "attachment;fileName="+fileName);//文件名
response.setContentLength((int)file.length());
response.setCharacterEncoding("utf-8");
byte[] buffer = new byte[1024];
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream(file));
OutputStream os = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1){
os.write(buffer, 0, i);
i = bis.read(buffer);
}
return "down ok";
} 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 "down wrong";
}