未分类
2021-03-29 15:08:00
1822677238@qq.com
手机扫码查看
控制层:
@Controller
@RequestMapping("/")
public class UploadController {
@RequestMapping("/")
public String index(){return "upload";}
// 得到一个新的文件路径
public String realPath(String path){
SimpleDateFormat y=new SimpleDateFormat("yyyy");
SimpleDateFormat m=new SimpleDateFormat("MM");
SimpleDateFormat d=new SimpleDateFormat("dd");
Date date=new Date(System.currentTimeMillis());
String year = y.format(date);
String month = m.format(date);
String day = d.format(date);
String realPath=path+ File.separator+year+File.separator+month+File.separator+day;
File file=new File(realPath);
if(!file.exists()) file.mkdirs();
return realPath;
}
// 得到一个新的带有用户名的文件名
public String newFilename(String username,String filename){
Date date=new Date();
SimpleDateFormat time=new SimpleDateFormat("yyyyMMddHHmmss");
String times = time.format(date);
return times+"_"+username+"_"+filename;
}
// 文件上传
@RequestMapping("/upload")
public void upload(String username, MultipartFile file, HttpServletRequest request, HttpServletResponse response)throws Exception{
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
// 获得文件名称
String filename = file.getOriginalFilename();
System.out.println(filename);
// 判断是否上传文件
if (file != null && filename != null) {
// 获取上传目录
String realPath = request.getServletContext().getRealPath("/WEB-INF/upload");
String newFilePath = realPath(realPath);
String newFilename = newFilename(username,filename);
// 判断上传目录是否存在,如果不存在则创建反之直接存
File file1=new File(newFilePath);
if(!file1.exists())file1.mkdirs();
file.transferTo(new File(newFilePath+File.separator+newFilename));
}
}
// 文件下载
@RequestMapping("/download")
public void download(String filename,HttpServletRequest request,HttpServletResponse response) throws IOException {
String realPath = request.getServletContext().getRealPath("/WEB-INF/upload");
String newFilePath = realPath(realPath);
response.setHeader("content-disposition","attachment;filename="+filename);
IOUtils.copy(new FileInputStream(newFilePath+File.separator+filename),response.getOutputStream());
}
// 获取文件列表
public void getFileList(String path, HashMap<String,String> map){
File file=new File(path);
File[] files = file.listFiles();
if (files != null) {
for (File file1 : files) {
if (file1.isDirectory()) {
getFileList(file1.getPath(),map);
}else{
String name = file1.getName();
String sub=name.substring(name.lastIndexOf("_")+1);
map.put(name,sub);
}
}
}
}
// 展示文件列表
@RequestMapping("/fileList")
public String fileList(HttpServletRequest request){
String realPath = request.getServletContext().getRealPath("/WEB-INF/upload");
HashMap<String,String> map=new HashMap<>();
getFileList(realPath,map);
request.setAttribute("map",map);
return "showFileLists";
}
}
文件列表的jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>下载页面</title>
</head>
<body>
<h3>下载列表</h3>
<table border="1">
<tr>
<th>文件名</th>
<th>操作</th>
</tr>
<c:forEach var="files" items="${requestScope.map}">
<tr>
<td>文件名:${files.value}</td>
<td>
<a href="${pageContext.request.contextPath}/download?filename=${files.key}">下载</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
只展示登录用户的文件列表,修改文件列表的jsp,可以把王五换成session的用户作用域里的用户名
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>下载页面</title>
</head>
<body>
<h3>下载列表</h3>
<table border="1">
<tr>
<th>文件名</th>
<th>操作</th>
</tr>
<c:forEach var="files" items="${requestScope.map}">
<c:if test="${files.key.contains('王五')}">
<tr>
<td>文件名:${files.value}</td>
<td>
<a href="${pageContext.request.contextPath}/download?filename=${files.key}">下载</a>
</td>
</tr>
</c:if>
</c:forEach>
</table>
</body>
</html>
- 本页地址 https://www.9713job.com/?p=3079
- 上一篇 <<Linux配置固定IP
- 下一篇 >>Linux运行SpringBoot项目出现404解决方案



发表回复