1. 单文件上传

配置jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>文件上传</title>
</head>
<body>
<form action="/file/fileUpload" method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
作者:<input type="text" name="author"/>
<input type="submit" value="提交">
</form>
</body>
</html>

配置文件上传的解析器

在WEB-INF里创建upload文件夹

效果展示

<!--CommonsMultipartResolver文件上传解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--编码-->
<property name="defaultEncoding" value="UTF-8"/>
<property name="maxInMemorySize" value="500000"/>
</bean>

2. 多文件上传

  1. 编写jsp页面

Form表单加上enctype="multipart/form-data"

input 属性的name值必须保持一致

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>多文件上传</title>
</head>
<body>
<form action="/file/fileUploads" method="post" enctype="multipart/form-data">
<input type="file" name="uploadFiles"/>
<input type="file" name="uploadFiles"/>
<input type="file" name="uploadFiles"/>
作者:<input type="text" name="author"/>
<input type="submit" value="提交">
</form>
</body>
</html>
@RequestMapping("/fileUploads")
public String fileMothers(HttpSession session, @RequestParam MultipartFile[] uploadFiles,String author) throws IOException {
System.out.println("作者:"+author);
System.out.println(uploadFiles);
/*如何处理文件*/
for (MultipartFile file:uploadFiles) {
if (!file.isEmpty()) {
//获取文件名称
String fileName = file.getOriginalFilename();
//获取需要上传的路径
String realPath = session.getServletContext().getRealPath("/WEB-INF/upload");
//创建文件对象
File uploadfile = new File(realPath + "\\" + fileName);
//如何上传文件
file.transferTo(uploadfile);
}
}
return "main";
}

实现handler文件

@RequestMapping("/springmvc")
@Controller
public class SpringMVCTest {
/**
*来一波上传文件 ,用@RequestParam注解来指定表单上的file为MultipartFile
*/
@RequestMapping("/fileUpload")
public void fileUpload(@RequestParam("file") MultipartFile file){
// 判断文件是否为空
if (!file.isEmpty()) {
try {
// 文件保存路径
String filePath = "E:\\MySQL\\springmvc_test\\"
+ file.getOriginalFilename();
// 转存文件
System.out.println(filePath);
file.transferTo(new File(filePath));
File uploadDest = new File(filePath);
String[] fileNames = uploadDest.list();
for (int i = 0; i < fileNames.length; i++) {
//打印出文件名
System.out.println(fileNames[i]);
}
} catch (Exception e) {
e.printStackTrace();
}
} }
}

注意:我上面没有返回字符串或者是ModelAndView。。虽然能上传文件,但是跳转页面会是404,如下图:

这里进行修改

/**
*来一波上传文件 ,用@RequestParam注解来指定表单上的file为MultipartFile
*/
@RequestMapping("/fileUpload")
public String fileUpload(@RequestParam("file") MultipartFile file){
// 判断文件是否为空
if (!file.isEmpty()) {
try {
// 文件保存路径
String filePath = "E:\\MySQL\\springmvc_test\\"
+ file.getOriginalFilename();
// 转存文件
System.out.println(filePath);
file.transferTo(new File(filePath));
File uploadDest = new File(filePath);
String[] fileNames = uploadDest.list();
for (int i = 0; i < fileNames.length; i++) {
//打印出文件名
System.out.println(fileNames[i]);
}
} catch (Exception e) {
e.printStackTrace();
}
}
// 重定向
return SUCCESS;
}

3.文件下载

修改index.jsp,加入下载文件的超链接:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UYF-8">
<title>Insert title here</title>
</head>
<body> <form action="springmvc/fileUpload" method="post" enctype="multipart/form-data">
choose file:<input type="file" name="file">
<input type="submit" value="submit">
</form> <br><br>
<a href="springmvc/testResponseEntity">Test ResponseEntity</a </body>
</html>

在Controller中添加对应 handler:

 @RequestMapping("/testResponseEntity")
public ResponseEntity<byte[]> testResponseEntity(HttpSession session) throws IOException {
byte[] body =null;
ServletContext servletContext = session.getServletContext();
InputStream in = servletContext.getResourceAsStream("/files/abc.txt");
body = new byte[in.available()];
in.read(body); HttpHeaders headers = new HttpHeaders();
//添加头部信息
headers.add("Content-Disposition", "attachment;filename=abc.txt"); HttpStatus statusCode = HttpStatus.OK; ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(body, headers, statusCode);
return response;
}

点击超链接,左下角看到

SpringMVC的文件上传与下载的更多相关文章

  1. SpringMVC 实现文件上传与下载,并配置异常页面

    目录 上传文件的表单要求 Spring MVC实现上传文件 需要导入的jar包 配置MultipartResolver解析器 编写接收上传文件的控制器 Spring MVC实现文件下载 下载文件时的h ...

  2. 使用springMVC实现文件上传和下载之环境配置与上传

    最近的项目中用到了文件的上传和下载功能,任务分配给了其他的同时完成.如今项目结束告一段落,我觉着这个功能比较重要,因此特意把它提取出来自己进行了尝试. 一. 基础配置: maven导包及配置pom.x ...

  3. springMvc之文件上传与下载

    我们经常会使用的一个功能是文件下载,既然有文件下载就会有文件上传,下面我们来看一下文件上传是如何实现的 首先准备好一个页面 <style type="text/css"> ...

  4. springmvc之文件上传、下载

    1.接收到的是图片的流时 //上传头像 @RequestMapping(value = "/uploadHeadSculpture", method = RequestMethod ...

  5. 使用SpringMVC实现文件上传和下载

    文件上传 第一步,加入jar包: commons-fileupload-1.3.1.jar commons-io-2.4.jar 第二步,在SpringMVC配置文件中配置CommonsMultipa ...

  6. 【SpringMVC】文件上传与下载、拦截器、异常处理器

    文件下载 使用ResponseEntity实现下载文件的功能 index.html <!DOCTYPE html> <html lang="en" xmlns:t ...

  7. 使用springMVC实现文件上传和下载之文件下载

    接上一篇,文件下载需要获取下载文件的存储路径,这里只是手动填入,如果是在具体项目中,可以把文件名和上传后的存储路径保存在数据库中.然后增加一个文件列表的页面展示文件名和文件路径,然后点击下载的时候把相 ...

  8. 文件上传和下载(可批量上传)——Spring(二)

    针对SpringMVC的文件上传和下载.下载用之前“文件上传和下载——基础(一)”的依然可以,但是上传功能要修改,这是因为springMVC 都为我们封装好成自己的文件对象了,转换的过程就在我们所配置 ...

  9. 使用Spring MVC实现文件上传与下载

    前段时间做毕业设计的时候,想要完成一个上传文件的功能,后来,虽然在自己本地搭建了一个ftp服务器,然后使用公司的工具完成了一个文档管理系统:但是还是没有找到自己想要的文件上传与下载的方式. 今天看到一 ...

随机推荐

  1. wpf 对控件进行截图,获取快照

    有时候我们项目,在执行某个操作后,会生成一些数据结果,如报表一类的东西,我们需要对结果进行保存,甚至是生成word文档. 那么首先获取到控件快照就最基本的条件. 生成快照的静态方法类 using Sy ...

  2. IdentityServer4:发布环境的数字签名证书

    一,jwt的三个组成部件 先来看一个由IdentityServer颁发的一个标准令牌 eyJhbGciOiJSUzI1NiIsImtpZCI6IjBiNTE3ZjIzYWY0OGM4ZjkyZjExM ...

  3. Python3 压缩与解压缩(zlib / gzip / bz2 / lzma / zipfile / tarfile)

    本文由 Luzhuo 编写,转发请保留该信息. 原文: http://blog.csdn.net/Rozol/article/details/72672703 以下代码以Python3.6.1为例 L ...

  4. screen命令下,自启动设置

    建立autostart.sh文件 #!/bin/bash screen_name=$"coffeetest:9100" screen -dmS $screen_name cmd=$ ...

  5. 关于 JS this

    关于 JS this 1. this 与 普通函数 2. this 与 bind.call.apply 3. this 与 箭头函数 4. this 与 return 4.1 返回 引用对象 4.2 ...

  6. input里面的提示文字修改(placeholder里的文字修改,el-input也适用)

    input::-webkit-input-placeholder { /* WebKit browsers */ color: red; } input:-moz-placeholder { /* M ...

  7. vue动态加载图片

    如果是直接动态获取完整的图片地址可以使用以下方法 <template> <img :src="url"> </template> <scr ...

  8. linux运行级

    Linux有0到6个级别,分别对应/etc/rcN.d,N对应7个级别 各运行级详解 0.关机 1.单用户模式,类似于Windows安全模式 2.多用户模式 3.完整的多用户模式.标准运行级 4.不用 ...

  9. python 操作excel实现替换特定内容

    本文介绍使用python语言,借助openyxl库来实现操作excel(xlsx)文件,实现替换特定内容的需求. 目前实现了3个小功能: 1. 全字匹配替换(mode1):(如:全字匹配 yocich ...

  10. [LeetCode] 448. 找到所有数组中消失的数字 ☆

    描述 给定一个范围在  1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次. 找到所有在 [1, n] 范围之间没有出现在数组中的数字. 您 ...