前台
参考:漂亮的文件上传控件input file css样式整理;
后台
代码如下:
package com.aitlp.dataArchives.controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
/**
* Spring Boot 文件上传下载
* @author 四个空格-https://www.4spaces.org
*/
@RestController
@RequestMapping(value = "/file")
public class FileController {
/**
* 多文件上传
*
* @param file
* @return
* @throws Exception
*/
@PostMapping("/bacthFileUpload")
public Map bacthFileUpload(@RequestParam("file") MultipartFile[] file) {
Map resultMap = new HashMap();
resultMap.put("code", "0");
resultMap.put("msg", "success");
for (MultipartFile multipartFile : file) {
singleFileUpload(multipartFile);
}
return resultMap;
}
/**
* 单个文件上传
*
* @param file
* @return
* @throws Exception
*/
@PostMapping("/singleFileUpload")
public Map singleFileUpload(MultipartFile file) {
Map resultMap = new HashMap();
resultMap.put("code", "200");
resultMap.put("msg", "success");
// 获取上传文件路径
String originalFilename = file.getOriginalFilename();
// 获取上传文件的后缀
String fileSuffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
// 上传路径
String uploadPath = "F:\\upload\\";
// 上传文件名
String fileName = new Date().getTime() + new Random().nextInt(100) + "." + fileSuffix;
File savefile = new File(uploadPath + fileName);
if (!savefile.getParentFile().exists()) {
savefile.getParentFile().mkdirs();
}
try {
file.transferTo(savefile);
} catch (IOException e) {
resultMap.put("code", "500");
resultMap.put("msg", "error");
e.printStackTrace();
}
return resultMap;
}
/**
* 文件下载
*
* @param request
* @param response
* @return
*/
@GetMapping("/downloadFile")
public String downloadFile(HttpServletRequest request, HttpServletResponse response) {
String fileName = request.getParameter("fileName");
//设置文件路径,这里写死,下载F盘的1557803382139.jpg
File file = new File("F://upload//" + fileName);
//File file = new File(realPath , fileName);
if (file.exists()) {
response.setContentType("application/force-download");// 设置强制下载不打开
response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
byte[] buffer = new byte[1024];
FileInputStream fileInputStream = null;
BufferedInputStream bufferedInputStream = null;
try {
fileInputStream = new FileInputStream(file);
bufferedInputStream = new BufferedInputStream(fileInputStream);
OutputStream os = response.getOutputStream();
int i = bufferedInputStream.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bufferedInputStream.read(buffer);
}
return "下载成功";
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bufferedInputStream != null) {
try {
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return "下载失败";
}
}
参考文章:
最新评论
网飞没问题, 迪士尼+有解决方案么?
pp助手是安卓手机用的,根本下载用不来苹果
已解决
这样的话数据库里的结构为{"attachment":{"content":"xxx"}}, 要怎么才能变成{"content":"xxx"},从而使结构保持一致?
赞! make test不过的坑都写到的,谢谢楼主~
谢谢你
用了root用户还是一直502是怎么回事呢
student id 是空的