基于SpringMVC的文件上传和下载
2017-04-06 23:03
706 查看
一 文件上传和下载需要的jar包
二 文件的上传:
jsp页面:<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.get
4000
ContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>陈二狗的妖孽人生</title>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.9.1.js"></script>
</head>
<body>
<h1>SpringMVCDemo:${message}</h1>
<!-- <script type="text/javascript">
$(document).ready(function() {
alert("陈二狗的妖孽人生");
});
</script> -->
<form action="${pageContext.request.contextPath}/HelloWorld/uploadFile" method="POST" enctype="multipart/form-data">
<h2>文件上传</h2>
文件:<input type="file" name="uploadFile"/><br><br>
<input type="submit" value="上传">
<a href="${pageContext.request.contextPath }/HelloWorld/fileDownLoad?123.jpg">下载</a>
</form>
</body>
</html>
Controller里面的文件上传的方法:
/**
* 文件上传
* @param uploadFile
* @param session
* @return
* @throws IOException
* @throws IllegalStateException
*/
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public String uploadFile(MultipartFile uploadFile, HttpSession session) throws Exception{
// 获取文件名称作为保存在服务器的文件的名称
String filename = uploadFile.getOriginalFilename();
// 前半部分路径, 目录,将WebRoot下一个名称为images文件夹 转换成绝对路径
String leftPath = session.getServletContext().getRealPath("/images");
// 进行路径拼接 = 前半部分路径 + 文件名称
File file = new File(leftPath, filename);
// 如果上传文件目录不存在 自动创建该文件夹
if (!file.exists()){
file.mkdirs();
}
uploadFile.transferTo(file);
return "loadFileSuccess";
} spingMVC配置文件配置如下:
<!-- 配置文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="50000000"></property>
<!-- <property name="maxUploadSizePerFile" value="5000000"></property> -->
<property name="defaultEncoding" value="utf-8"></property>
</bean>
三 文件的下载
Controller里面的方法:
/**
* 文件下载
* @return
* @throws Exception
*/
@RequestMapping(value = "/fileDownLoad", method = RequestMethod.GET)
public ResponseEntity<byte[]> fileDownLoad()throws Exception{
File file = new File("E:\\相册\\微博\\123.jpg");
HttpHeaders headers = new HttpHeaders();
String fileName = new String("123.jpg".getBytes("utf-8"), "iso-8859-1"); // 解决中文乱码问题
headers.setContentDispositionFormData("attachment", fileName);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
}
二 文件的上传:
jsp页面:<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.get
4000
ContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>陈二狗的妖孽人生</title>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.9.1.js"></script>
</head>
<body>
<h1>SpringMVCDemo:${message}</h1>
<!-- <script type="text/javascript">
$(document).ready(function() {
alert("陈二狗的妖孽人生");
});
</script> -->
<form action="${pageContext.request.contextPath}/HelloWorld/uploadFile" method="POST" enctype="multipart/form-data">
<h2>文件上传</h2>
文件:<input type="file" name="uploadFile"/><br><br>
<input type="submit" value="上传">
<a href="${pageContext.request.contextPath }/HelloWorld/fileDownLoad?123.jpg">下载</a>
</form>
</body>
</html>
Controller里面的文件上传的方法:
/**
* 文件上传
* @param uploadFile
* @param session
* @return
* @throws IOException
* @throws IllegalStateException
*/
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public String uploadFile(MultipartFile uploadFile, HttpSession session) throws Exception{
// 获取文件名称作为保存在服务器的文件的名称
String filename = uploadFile.getOriginalFilename();
// 前半部分路径, 目录,将WebRoot下一个名称为images文件夹 转换成绝对路径
String leftPath = session.getServletContext().getRealPath("/images");
// 进行路径拼接 = 前半部分路径 + 文件名称
File file = new File(leftPath, filename);
// 如果上传文件目录不存在 自动创建该文件夹
if (!file.exists()){
file.mkdirs();
}
uploadFile.transferTo(file);
return "loadFileSuccess";
} spingMVC配置文件配置如下:
<!-- 配置文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="50000000"></property>
<!-- <property name="maxUploadSizePerFile" value="5000000"></property> -->
<property name="defaultEncoding" value="utf-8"></property>
</bean>
三 文件的下载
Controller里面的方法:
/**
* 文件下载
* @return
* @throws Exception
*/
@RequestMapping(value = "/fileDownLoad", method = RequestMethod.GET)
public ResponseEntity<byte[]> fileDownLoad()throws Exception{
File file = new File("E:\\相册\\微博\\123.jpg");
HttpHeaders headers = new HttpHeaders();
String fileName = new String("123.jpg".getBytes("utf-8"), "iso-8859-1"); // 解决中文乱码问题
headers.setContentDispositionFormData("attachment", fileName);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
}
相关文章推荐
- 基于SpringMVC的文件上传下载功能
- 基于SpringMVC的文件(增删改查)上传、下载、更新、删除
- 基于springmvc的文件上传与下载
- 基于struts2的文件上传与下载
- SpringMVC中文件上传下载
- 基于Apache FTP点断续传的文件上传和下载
- 基于asp.net的文件上传和下载
- 基于Apache FTP实现的文件上传下载工具
- 【jUploader】1.0版 基于jQuery文件无刷新上传插件下载及介绍
- springMVC教程(九)springMVC的文件上传于下载
- springMVC的文件上传和基于ajax的文件上传(学习记录)
- 基于java的文件上传和下载
- 04springMVC结构,mvc模式,spring-mvc流程,spring-mvc的第一个例子,三种handlerMapping,几种控制器,springmvc基于注解的开发,文件上传,拦截器,s
- 基于Hadoop的云盘系统上传和下载效率优化及处理大量小文件的解决方法
- SpringMVC经典系列-12基于SpringMVC的文件上传---【LinusZhu】
- 基于asp.net的文件上传和下载
- myBatis + SpringMVC上传、下载文件
- JSF 2.0(基于Primefaces 3.4.2)文件上传与下载的总结
- 基于 apache 的 commons-net-3.3.jar 的 ftp java代码上传下载文件
- 基于Hadoop的云盘系统上传和下载效率优化及处理大量小文件的解决方法