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下文件的上传与下载以及文件列表的显示的更多相关文章

  1. php 上传文件实例 上传并下载word文件

    上传界面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...

  2. 在SpringMVC框架下实现文件的 上传和 下载

    在eclipse中的javaEE环境下:导入必要的架包 web.xml的配置文件: <?xml version="1.0" encoding="UTF-8" ...

  3. 文件的上传和下载--SpringMVC

    文件的上传和下载是项目开发中最常用的功能,例如图片的上传和下载.邮件附件的上传和下载等. 接下来,将对Spring MVC环境中文件的上传和下载进行详细的讲解. 一.文件上传 多数文件上传都是通过表单 ...

  4. SpringMVC+Ajax实现文件批量上传和下载功能实例代码

    需求: 文件批量上传,支持断点续传. 文件批量下载,支持断点续传. 使用JS能够实现批量下载,能够提供接口从指定url中下载文件并保存在本地指定路径中. 服务器不需要打包. 支持大文件断点下载.比如下 ...

  5. SpringMVC学习09(文件的上传和下载)

    文件上传和下载 准备工作 文件上传是项目开发中最常见的功能之一 ,springMVC 可以很好的支持文件上传,但是SpringMVC上下文中默认没有装配MultipartResolver,因此默认情况 ...

  6. linux下使用rzsz实现文件的上传和下载

    新搞的云服务器用SecureCRT不支持上传和下载,没有找到rz命令.记录一下如何安装rz/sz命令的方法. 一.工具说明 在SecureCRT这样的ssh登录软件里, 通过在Linux界面里输入rz ...

  7. springMVC实现文件的上传和下载

    文件的下载功能 @RequestMapping("/testDown")public ResponseEntity<byte[]> testResponseEntity ...

  8. Spring MVC 实现文件的上传和下载

    前些天一位江苏经贸的学弟跟我留言问了我这样一个问题:“用什么技术来实现一般网页上文件的上传和下载?是框架还是Java中的IO流”.我回复他说:“使用Spring MVC框架可以做到这一点,因为Spri ...

  9. java实现ftp文件的上传与下载

    最近在做ftp文件的上传与下载,基于此,整理了一下资料.本来想采用java自带的方法,可是看了一下jdk1.6与1.7的实现方法有点区别,于是采用了Apache下的框架实现的... 1.首先引用3个包 ...

随机推荐

  1. Angular ui-route的用法

    ui-router和同属AngularJS框架一部分的ng-route一样强大. ui-router提供了让我们可以做路由嵌套和视图命名的特性,嵌套路由功能主要是依赖$stateProvider服务, ...

  2. Java核心技术1

    Java方法参数的使用情况: 一个方法不能修改一个基本数据 对象析构与finalize方法 Java有自动的垃圾回收器,不需要人工回收内存,例如,文件或使用了系统资源的另一个对象的句柄.在这种情况下, ...

  3. 通过手机浏览器打开APP或者跳转到下载页面.md

    目录 通过手机浏览器打开APP或者跳转到下载页面 添加 schemes 网页设置 参考链接 通过手机浏览器打开APP或者跳转到下载页面 以下仅展示最简单的例子及关键代码 由于硬件条件有限,仅测试了 A ...

  4. finereport---FineReport入门常见疑难点

    一.入门介绍 二.入门需知 注意:开发人员可以设置DEBUG级别,有助于测试 三.数据准备 数据集sql中可以使用参数宏${}动态地生成过滤条件,${}中的语句在FineReport报表中执行,将${ ...

  5. 【Python之路】第十七篇--Ajax全套

    概述 1.传统的Web应用 一个简单操作需要重新加载全局数据 2.AJAX AJAX,Asynchronous JavaScript and XML (异步的JavaScript和XML),一种创建交 ...

  6. SQL case when else

    先占个坑,sql 版本的swith case SELECT Oldvote, (CASE THEN (SELECT NOW() from dual) END) as "number" ...

  7. FW: git internal

    Git 内部原理 不管你是从前面的章节直接跳到了本章,还是读完了其余各章一直到这,你都将在本章见识 Git 的内部工作原理和实现方式.我个人发现学习这些内容对于理解 Git 的用处和强大是非常重要的, ...

  8. Storm-源码分析- spout (backtype.storm.spout)

    1. ISpout接口 ISpout作为实现spout的核心interface, spout负责feeding message, 并且track这些message. 如果需要Spout track发出 ...

  9. sql语句(mysql中json_contains、json_array的使用)

    https://blog.csdn.net/qq_35952946/article/details/79131488 https://www.jianshu.com/p/455d3d4922e1 1. ...

  10. 采集baidu搜索信息的java源代码实现(大部分转发,少量自己修改)(使用了htmlunit和Jsoup)(转发:https://blog.csdn.net/zhaohang_1/article/details/44731039)

    1.maven依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...