标签:append trace rest bind origin try dom submit alt
单、多文件上传:单文件上传使用upload.html ,多文件上传使用uploads.html
创建一个Springboot application, POM 中加入 spring-boot-starter-web 依赖
upload.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="uploadFile" value="请选择文件" /> <input
type="submit" value="上传" />
</form>
</body>
</html>
uploads.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="uploads" method="post" enctype="multipart/form-data">
<input type="file" name="uploadFiles" value="请选择文件" multiple /> <input
type="submit" value="上传" />
</form>
</body>
</html>
创建Controller 用于处理提交之后的Aciton
package com.app; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.UUID; import javax.servlet.http.HttpServletRequest; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; @RestController public class FileUploadController { SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/"); @PostMapping("/upload") public String upload(MultipartFile uploadFile, HttpServletRequest req) { String realPath = req.getSession().getServletContext().getRealPath("/uploadFile/"); String format = sdf.format(new Date()); File folder = new File(realPath + format); System.out.println(folder.getAbsolutePath()); if(!folder.isDirectory()) { folder.mkdirs(); } String oldName = uploadFile.getOriginalFilename(); System.out.println("oldName" + oldName); String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."), oldName.length()); System.out.println("new Name" + newName); try { uploadFile.transferTo(new File(folder,newName)); String filePath= req.getScheme() +"://" + req.getServerName() + ":" + req.getServerPort() + "/uploadFile/" + format + newName; return filePath; } catch (IllegalStateException | IOException e) { e.printStackTrace(); } return "上传失败!"; } @PostMapping("/uploads") public String upload(MultipartFile[] uploadFiles, HttpServletRequest req) { String realPath = req.getSession().getServletContext().getRealPath("/uploadFiles/"); String format = sdf.format(new Date()); File folder = new File(realPath + format); System.out.println(folder.getAbsolutePath()); if(!folder.isDirectory()) { folder.mkdirs(); } StringBuffer result = new StringBuffer(); for(MultipartFile uploadFile: uploadFiles) { String oldName = uploadFile.getOriginalFilename(); System.out.println("oldName" + oldName); String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."), oldName.length()); System.out.println("new Name" + newName); try { uploadFile.transferTo(new File(folder,newName)); String filePath= req.getScheme() +"://" + req.getServerName() + ":" + req.getServerPort() + "/uploadFiles/" + format + newName; result.append(filePath); result.append("\r\n"); } catch (IllegalStateException | IOException e) { e.printStackTrace(); return "上传失败!"; } } return result.toString(); } }
标签:append trace rest bind origin try dom submit alt
原文地址:https://www.cnblogs.com/luffystory/p/12010409.html