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. iOS 保存异常日志

    // // AppDelegate.m // test // // Created by Chocolate. on 14-4-16. // Copyright (c) 2014年 redasen. ...

  2. 爬虫入门【11】Pyspider框架入门—使用HTML和CSS选择器下载小说

    开始之前 首先我们要安装好pyspider,可以参考上一篇文章. 从一个web页面抓取信息的过程包括: 1.找到页面上包含的URL信息,这个url包含我们想要的信息 2.通过HTTP来获取页面内容 3 ...

  3. 两个input在一行让它们能对齐

    input明明写在同一行,高度也一样,在不同的浏览器或者手机上显示却不一样,经常会出现这样的情况 <input type="text" name="verify&q ...

  4. 【Linux】命令学习笔记和总结

    莫名的想学习一下Linux了,因为对这方面的知识储备为0.对于命令行界面始终是零接触零了解,对一个程序员来说这几乎是致命的,所以简单了解一下. 一.教程参考 参考菜鸟教程即可: Linux 教程 | ...

  5. 【JDF】学习和理解

    一.资源地址 官方GitBub地址: putaoshu/jdf: Jingdong front-end integrated solution https://github.com/putaoshu/ ...

  6. iPhone设备分辨率一览

    地址:https://developer.apple.com/design/human-interface-guidelines/ios/icons-and-images/launch-screen/

  7. 改变label中的某字体颜色

    NSString *allString=@"你家在哪里,需要我送你么"; NSString *subString=@"在哪里"; UILabel *string ...

  8. simplest_ffmpeg_grabdesktop:屏幕录制。 simplest_ffmpeg_readcamera:读取摄像头

    最简单的基于FFmpeg的AVDevice例子(屏幕录制) - 雷霄骅(leixiaohua1020)的专栏 - CSDN博客 https://blog.csdn.net/leixiaohua1020 ...

  9. XSS CSS Cross SiteScript 跨站脚本攻击

    XSS攻击及防御 - 高爽|Coder - CSDN博客 https://blog.csdn.net/ghsau/article/details/17027893 XSS又称CSS,全称Cross S ...

  10. FW: Nginx模块开发入门

    前言 Nginx是当前最流行的HTTP Server之一,根据W3Techs的统计,目前世界排名(根据Alexa)前100万的网站中,Nginx的占有率为6.8%.与Apache相比,Nginx在高并 ...