单文件上传
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>单上传文件</title>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<form action="/demo/upload" method="post" enctype="multipart/form-data">
<input type="file" name="multipartFile" id="file" value="上传文件">
<input type="button" value="ajax形式上传" id="btn" onclick="onUpload()">
<input type="submit" value="form表单的submit上传">
</form>
<script>
function onUpload() {
var file = $("#file")[0].files[0];
var formData = new FormData();
formData.append("multipartFile", file);
formData.append("isOnline", 1);
$.ajax({
type: "POST",
url: "/demo/upload",
data: formData,
processData: false,
contentType: false,
success: function (msg) {
console.log(msg)
alert("上传文件成功");
}
})
}
</script>
</body>
</html>
@PostMapping("/demo/upload")
@ResponseBody
public String upload(MultipartFile multipartFile, HttpServletRequest request, HttpServletResponse response) throws IOException {
String originalFilename = multipartFile.getOriginalFilename();
String realPath = ResourceUtils.getURL(ResourceUtils.CLASSPATH_URL_PREFIX).getPath();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String format = simpleDateFormat.format(new Date());
String path = realPath + "static/" + format;
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
multipartFile.transferTo(new File(file, originalFilename));
String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/" + format + "/" + originalFilename;
return url;
}
多文件上传
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>多上传文件</title>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<form action="/demo/upload2" method="post" enctype="multipart/form-data">
<input type="file" name="files" multiple value="多文件上传">
<input type="submit" value="多文件上传">
</form>
</body>
</html>
@PostMapping("/demo/upload2")
@ResponseBody
public String upload2(MultipartFile[] files, HttpServletRequest request, HttpServletResponse response) throws IOException {
List<String> list = new ArrayList<>();
for (MultipartFile multipartFile : files) {
String originalFilename = multipartFile.getOriginalFilename();
String realPath = ResourceUtils.getURL(ResourceUtils.CLASSPATH_URL_PREFIX).getPath();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String format = simpleDateFormat.format(new Date());
String path = realPath + "static/" + format;
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
multipartFile.transferTo(new File(file, originalFilename));
String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/" + format + "/" + originalFilename;
list.add(url);
}
return new ObjectMapper().writeValueAsString(list);
}
下载、在线查看
@RequestMapping("/demo/download")
public void download(HttpServletRequest request, HttpServletResponse response, String name, @RequestParam(defaultValue = "0") int isOnline) throws IOException {
try (ServletOutputStream outputStream = response.getOutputStream();) {
String path = ResourceUtils.getURL(ResourceUtils.CLASSPATH_URL_PREFIX).getPath();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String format = simpleDateFormat.format(new Date());
String realPath = path + "static/" + format + "/" + name;
File file = new File(realPath);
if (!file.exists()) {
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("下载的文件不存在");
return;
}
InputStream in = new FileInputStream(realPath);
int read;
byte[] b = new byte[1024];
if (isOnline == 0) {
response.addHeader("Content-Disposition", "inline;filename=" + URLEncoder.encode(name, "utf-8"));
} else {
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(name, "utf-8"));
response.setContentType("application/octet-stream");
}
response.addHeader("Content-Length", "" + file.length());
while ((read = in.read(b)) > 0) {
outputStream.write(b);
}
} catch (Exception e) {
System.out.println(e.getMessage());
throw e;
}
}
参考文章