1. 文件上载(upload)
HTTP协议支持文件上载功能,浏览器和Spring MVC按照 RFC1867 标准实现文件上载, 具体步骤
,
- 客户端
- 使用form 标签并且使用 method="post" 和 enctype="multipart/form-data"
- 使用 input type=file 选择文件
- 服务器端
- 导入 commons-fileupload
- 在Spring中配置 multipartResolver 组件
- 在控制器利用 MultipartFile 接收文件
编写客户端 upload.html
<h1>文件上载</h1>
<!-- 文件上载表单必须有 method="post" 和 enctype="multipart/form-data" -->
<form action="upload.do" method="post"
enctype="multipart/form-data">
<div>
<label>选择文件</label>
<!-- input type="file" 用于选择上载文件 -->
<input type="file" name="image">
</div>
<div>
<label>说明</label>
<input type="text" name="memo">
</div>
<input type="submit" value="上传">
</form>
<!-- 提示: 利用浏览器的F12可以检查文件上载的http消息 -->
在项目中导入 commons-fileupload 组件
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${version.commons.fileupload}</version>
</dependency>
在xml 中配置上载解析器
<!-- 配置文件上载解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上载编码, 否则中文文件名会乱码 -->
<property name="defaultEncoding" value="utf-8"/>
<!-- one of the properties available; the maximum file size in bytes -->
<!-- 最大上载总字节数 -->
<property name="maxUploadSize" value="10000000"/>
</bean>
编写Servlet方法
//method=RequestMethod.POST 限定
//用户提交的请求必须是 post 请求,
//如果不是post请求, 则会出现500错误
@RequestMapping(value="/upload.do",
method=RequestMethod.POST)
public String upload(
MultipartFile image,
String memo,
HttpServletRequest request)
throws Exception {
//文件相关信息从image对象中获取
//获取文件名
String filename =
image.getOriginalFilename();
//获取文件长度
long length=image.getSize();
//获取文件中全部的数据
//byte[] data=image.getBytes();
//获取<input name="?"> name 属性值
String name=image.getName();
System.out.println(filename);
System.out.println(name);
System.out.println(length);
System.out.println(memo);
//保存到文件 Linux: /home/soft01/files
//File dir = new File("D:/files");
//获取Tomcat中实际的路径:
//servletContext.getRealPath()
String path="/files";
path=request.getServletContext()
.getRealPath(path);
System.out.println(path);
//将文件保存到Tomcat的实际路径中:
File dir=new File(path);
if(!dir.exists()) dir.mkdir();
File file = new File(dir, filename);
//保存到文件
//image.transferTo(file);
//利用流保存文件
InputStream in=image.getInputStream();
FileOutputStream out=
new FileOutputStream(file);
int b;
while((b=in.read())!=-1){
out.write(b);
}
in.close();
out.close();
return "ok";
}
2. 文件下载(download)
直接下载图片
Spring MVC 对文件下载做了支持, 原理为:
编写控制器:
/**
* 利用Spring 下载显示一张照片
*/
@RequestMapping("/image.do")
public void image(
HttpServletResponse response)
throws Exception{
//读取照片数据 到 bytes数组中
String path="xx/xx/xx";
byte[] bytes = readFile(path);
//设置响应头
response.setContentType("image/png");
response.setContentLength(
bytes.length);
//设置响应正文(Message Body)
OutputStream out=
response.getOutputStream();
out.write(bytes);
out.close();
}
private byte[] readFile(String path) throws FileNotFoundException, IOException {
File file=new File(path);
byte[] bytes=
new byte[(int)file.length()];
FileInputStream in=
new FileInputStream(file);
in.read(bytes);
in.close();
return bytes;
}
@ResponseBody注解
Spring MVC 提供了@ResponseBody注解, 这个是一个自动处理响应Body的注解.
编写控制器:
/**
* 利用Spring 下载显示一张照片
*/
@RequestMapping(value="/image2.do",
produces="image/png")
@ResponseBody
public byte[] image2() throws Exception{
//读取照片数据 到 bytes数组中
String path="D:/1.png";
byte[] bytes = readFile(path);
//自动设置响应头
//自动设置响应正文(Message Body)
return bytes;
}
编写网页 download.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
.png{width: 200px}
</style>
</head>
<body>
<h1>下载演示</h1>
<h2>显示照片</h2>
<p>
<img class="png" alt="" src="image.do">
<img class="png" alt="" src="image2.do">
</p>
</body>