SpringMVC下文件的上传与下载以及文件列表的显示
1、配置好SpringMVC环境-----SpringMVC的HelloWorld快速入门!
导入jar包:commons-fileupload-1.3.1.jar和commons-io-2.4.jar
文件上传和下载的jar包(百度云) ---> 资源目录--->jar包资源--->文件上传和下载的jar包
2、在SpringMVC配置文件springmvc.xml中配置CommonsMultipartResovler
<!-- 配置CommonsMultipartResolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<!-- 以字节为单位 -->
<property name="maxUploadSize" value="10485760"></property><!-- 1M=1024x1024 -->
</bean>
3、controller方法
@Controller
@RequestMapping("File")
public class FileController {
//文件上传:表单:POST请求,file类型,enctype="multipart/form-data"
@RequestMapping(value = "fileUpload", method = RequestMethod.POST)
public String testUpload(HttpServletRequest request, @RequestParam(value = "desc", required = false) String desc,
@RequestParam("photo") CommonsMultipartFile fileList[]) throws Exception {
ServletContext servletContext = request.getServletContext();
//获取服务器下的upload目录
String realPath = servletContext.getRealPath("/upload");
File filePath = new File(realPath);
//如果目录不存在,则创建该目录
if (!filePath.exists()) {
filePath.mkdir();
}
OutputStream out;
InputStream in;
for (CommonsMultipartFile file : fileList) {
if (file.getSize() == 0) {
continue;
}
// 防止重命名uuid_name.jpg
String prefix = UUID.randomUUID().toString();
prefix = prefix.replace("-", "");
String fileName = prefix + "_" + file.getOriginalFilename();
out = new FileOutputStream(new File(realPath + "\\" + fileName));
in = file.getInputStream();
byte[] b = new byte[1024];
int c = 0;
while ((c = in.read(b)) != -1) {
out.write(b, 0, c);
out.flush();
}
out.close();
in.close();
}
return "redirect:/File/showFile";
}
//用ResponseEntity<byte[]> 返回值完成文件下载
@RequestMapping(value = "fileDownload")
public ResponseEntity<byte[]> fileDownload(HttpServletRequest request, @RequestParam(value = "path") String path)
throws Exception {
byte[] body = null;
// ServletContext servletContext = request.getServletContext();
String fileName = path.substring(path.lastIndexOf("_") + 1); //从uuid_name.jpg中截取文件名
// String path = servletContext.getRealPath("/WEB-INF/res/" + fileName);
File file = new File(path);
InputStream in = new FileInputStream(file);
body = new byte[in.available()];
in.read(body);
HttpHeaders headers = new HttpHeaders();
fileName = new String(fileName.getBytes("gbk"), "iso8859-1");
headers.add("Content-Disposition", "attachment;filename=" + fileName);
HttpStatus statusCode = HttpStatus.OK;
ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(body, headers, statusCode);
in.close();
return response;
}
//文件列表的显示
@RequestMapping(value = "/showFile")
public String showFile(HttpServletRequest request,Model m){
ServletContext servletContext = request.getServletContext();
String path=servletContext.getRealPath("/upload");
File[] fileList = new File(path).listFiles();
m.addAttribute("fileList", fileList);
return "showFile";
}
}
文件列表的显示没有对其进行处理,应该按时间排序:获取一个目录下的所有文件(按时间排序)
4、前台页面fileUpload.jsp和showFile.jsp
fileUpload.jsp
<form action="${pageContext.request.contextPath}/File/fileUpload"method="post" enctype="multipart/form-data">
<input type="file" name="photo"><br>
<input type="file" name="photo"><br>
<input type="file" name="photo"><br>
描述:<input type="text" name="desc"><br>
<input type="submit" value="上传">
</form>
showFile.jsp
<c:choose>
<c:when test="${not empty fileList }">
<!--索引-->
<c:set var="index" value='1'></c:set>
<c:forEach items="${fileList }" var="file">
<!-- filename:文件的名字,不带UUID -->
<c:set var="filename" value='${fn:substring(file.name,fn:indexOf(file.name,"_")+1,fn:length(file.name)) }'/>
<!-- filefullname:文件的名字,带UUID -->
<c:set var="filefullname" value='${fn:split(file.path,"\\\\")[fn:length(fn:split(file.path,"\\\\"))-1] }'></c:set>
<!-- rootdir:文件的目录 -->
<c:set var="rootdir" value='${pageContext.request.contextPath}/upload/'></c:set>
<div>
<img alt='${fileanme }' src='${rootdir.concat(filefullname) }'><!-- 文件的全路径 -->
<a href="${pageContext.request.contextPath}/File/fileDownload?path=${file.path}">下载</a>
</div>
<!-- 每行显示5张图片 -->
<c:if test="${index%5==0 }">
<br>
</c:if>
<!--索引+1-->
<c:set var="index" value='${index+1 }'></c:set>
</c:forEach>
</c:when>
<c:otherwise>
暂无数据
</c:otherwise>
</c:choose>
<c:set>中使用“\\”会报错,要使用“\\\\”,其他地方使用“\\”即可
SpringMVC下文件的上传与下载以及文件列表的显示的更多相关文章
- php 上传文件实例 上传并下载word文件
上传界面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
- 在SpringMVC框架下实现文件的 上传和 下载
在eclipse中的javaEE环境下:导入必要的架包 web.xml的配置文件: <?xml version="1.0" encoding="UTF-8" ...
- 文件的上传和下载--SpringMVC
文件的上传和下载是项目开发中最常用的功能,例如图片的上传和下载.邮件附件的上传和下载等. 接下来,将对Spring MVC环境中文件的上传和下载进行详细的讲解. 一.文件上传 多数文件上传都是通过表单 ...
- SpringMVC+Ajax实现文件批量上传和下载功能实例代码
需求: 文件批量上传,支持断点续传. 文件批量下载,支持断点续传. 使用JS能够实现批量下载,能够提供接口从指定url中下载文件并保存在本地指定路径中. 服务器不需要打包. 支持大文件断点下载.比如下 ...
- SpringMVC学习09(文件的上传和下载)
文件上传和下载 准备工作 文件上传是项目开发中最常见的功能之一 ,springMVC 可以很好的支持文件上传,但是SpringMVC上下文中默认没有装配MultipartResolver,因此默认情况 ...
- linux下使用rzsz实现文件的上传和下载
新搞的云服务器用SecureCRT不支持上传和下载,没有找到rz命令.记录一下如何安装rz/sz命令的方法. 一.工具说明 在SecureCRT这样的ssh登录软件里, 通过在Linux界面里输入rz ...
- springMVC实现文件的上传和下载
文件的下载功能 @RequestMapping("/testDown")public ResponseEntity<byte[]> testResponseEntity ...
- Spring MVC 实现文件的上传和下载
前些天一位江苏经贸的学弟跟我留言问了我这样一个问题:“用什么技术来实现一般网页上文件的上传和下载?是框架还是Java中的IO流”.我回复他说:“使用Spring MVC框架可以做到这一点,因为Spri ...
- java实现ftp文件的上传与下载
最近在做ftp文件的上传与下载,基于此,整理了一下资料.本来想采用java自带的方法,可是看了一下jdk1.6与1.7的实现方法有点区别,于是采用了Apache下的框架实现的... 1.首先引用3个包 ...
随机推荐
- 《Sqlserver》通过端口 8080 连接到主机 localhost 的 TCP/IP 连接失败。错误:“驱动程序收到意外的登录前响应。请验证连接属性,并检查 SQL Server 的实例正在主机上运行,且在此端口接受
1. 点击 开始 --> 所有程序 --> Microsoft SQL Server2005 --> 配置工具-->SQL Server configuration Manag ...
- iOS 计算时间差
/** * 计算指定时间与当前的时间差 * @param compareDate 某一指定时间 * @return 多少(秒or分or天or月or年)+前 (比如,3天前.10分钟前) */ +(NS ...
- activeMQ "HelloWorld"实现
本文主要介绍activeMQ在应用程序中是如何使用的,同个两个实例进行说明,这两个实例分别针对P2P模式和Pub/Sub模式. 开发环境 操作系统:Ubuntu 16.10 开发平台:Eclipse ...
- Vsftpd匿名登录设置
修改配置文件 # vi /etc/vsftpd/vsftpd.conf local_enable=NO connect_from_port_20=YES listen=YES listen_port= ...
- delphi 遇到问题、报错等
解决方法:using Windows
- swift UITextField
var textField = UITextField(frame: CGRectMake(10,160,200,30)) //设置边框样式为圆角矩形 textField.borderStyle = ...
- office 2013 activiate---(run as admin)
win7 office 2013 activiate---(run as admin) empty the garbage in osx rm -rf ~/.Trash
- Spring 框架的JDBC模板技术
1. 概述 Spring 框架提供了很多持久层的模板类来简化编程; Spring 框架提供的JDBC模板类: JdbcTemplate 类; Spring 框架提供的整合 Hibernate 框架的模 ...
- docker 构建镜像 centos7 nginx php
#docker 构建镜像(Dockerfile) centos 7.4.1078镜像制作 nginx镜像制作(以前面centos7镜像为基础) Nginx+php镜像制作 更多操作实例,查看git里的 ...
- Python元组组成的列表转化为字典
虽然元组.列表不可以直接转化为字典,但下面的确是可行的,因为经常用python从数据库中读出的是元组形式的数据. # 原始数据 rows = (('apollo', 'male', '164.jpeg ...