导入jar包
配置上传文件解析器(在SpringmvcConfig.xml里)
<!--配置上传文件解析器 5242880=1024*1024*5 ;不写乘法表达式的原因是:配置文件中*有特殊意义。-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="5242880"/>
</bean>
配置静态资源放行路径(在SpringmvcConfig.xml里)
<!--配置静态资源放行-->
<mvc:resources location="/lib/" mapping="/lib/**"></mvc:resources>
<mvc:resources location="/imgs/" mapping="/imgs/**"></mvc:resources>
<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
<!--<mvc:default-servlet-handler/>-->
上传:
新建fup.jsp页面 页面代码如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>文件上传</title>
</head>
<body>
<%--文件上传
method:表单提交方式必须是post
enctype:多部件表单提交multipart/form-data
--%>
<form action="emp/fup" method="post" enctype="multipart/form-data">
用户名:<input type="text" name="uname"><br>
上传文件:<input type="file" name="multipartFile"><br>
<img src="http://127.0.0.1:8080/SpringMVCDay2/imgs/${url}"/><br>
<input type="submit" value="上传"><br>
</form>
</body>
</html>
在controller层的 控制器 里添加如下代码:
/**
* 普通文本发送过来的请求信息,是常用类型;
* 附件发送过来的请求信息,是MultipartFile类型;
* multipartFile.transferTo();实现上传功能的核心代码!
* */
@RequestMapping("fup")
public String fup(String uname, MultipartFile multipartFile){
multipartFile.transferTo();
}
在web下新建imgs文件夹:那么在out文件夹里会同步出现img文件夹
在controller层的 控制器 里添加如下代码:
/**
* 普通文本发送过来的请求信息,是常用类型;
* 附件发送过来的请求信息,是MultipartFile类型;
* 获得上传时文件名
* 获得文件发布/上传的服务器地址
* multipartFile.transferTo();实现上传功能的核心代码!
*
* */
@RequestMapping("fup")
public String fup(String uname, MultipartFile multipartFile,HttpSession session) throws IOException {
/*获得上传时文件名*/
String fname=multipartFile.getOriginalFilename();
/*获得文件发布/上传的服务器地址*/
String url = session.getServletContext().getRealPath("/imgs/");
System.out.println(fname+url);
File file = new File(url+fname);
multipartFile.transferTo(file);
session.setAttribute("url",fname);
return "redirect:/fup.jsp";
}
下载
在fup.jsp添加如下代码
<a href="emp/fdown">点击下载文件</a>
在controller层的 控制器 里添加如下代码:
/**
* file.listFiles();获得当前文件夹下所有子文件和子目录*/
@RequestMapping("fdown")
public String fdown(HttpSession session){
String url = session.getServletContext().getRealPath("/imgs/");
File file = new File(url);
File[] files = file.listFiles();
List<String> list = new ArrayList<>();
for (int i = 0; i < files.length; i++) {
list.add(files[i].getName());
}
session.setAttribute("list",list);
return "redirect:/fdown.jsp";
}
新建fdown.jsp 添加如下代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>所有下载文件</h1>
<c:forEach items="${list}" var="obj">
<a href="emp/down?fname=${obj}">${obj}</a>
</c:forEach>
</body>
</html>
在controller层的 控制器 里添加如下代码:
/**
* Files.copy();下载文件的核心方法
* */
@RequestMapping("down")
public void down(HttpSession session,HttpServletResponse response,String fname) throws IOException {
String url = session.getServletContext().getRealPath("/imgs/");
File file = new File(url+fname);
//下载文件的响应跟普通文本信息的响应有区别
//设置响应信息 格式/类型 ==》 告诉浏览器当前是下载操作,我要下载东西
response.setContentType("application/x-msdownload");
//设置下载时文件的 文件名字和格式--也就是显示类型(即文件名称-后缀) ex:txt为文本类型
response.setHeader("Content-Disposition", "attachment;filename=" + fname);
Files.copy(file.toPath(),response.getOutputStream());
}