关于SpringMVC框架实现简单的文件上传下载(ssm)
【关于ssm框架整合文件的上传和下载】
1、创建前端页面
首先创建一个用来测试的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=UTF-8"> <title>文件上传下载</title> </head> <body> <form action="${pageContext.request.contextPath }/file/upload.chao" method="post" enctype="multipart/form-data"> 选择文件:<input type="file" name="file" width="120px"> <input type="submit" value="上传"> </form> <hr> <form action="${pageContext.request.contextPath }/file/down.chao" method="get"> <input type="submit" value="下载"> </form> </body> </html>
2、构建项目
在自己已经构建好的maven web项目下的pom.xml配置文件中添加上传下载所需要的jar包
<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency>
3、修改配置文件
在spring的applicationContext.xml配置文件中添加文件上传解析器。
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8"></property> <property name="maxUploadSize" value="5242440"></property> </bean>
4、代码实现
在controller层实现上传下载的代码如下。
package com.chao.controller;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
/**
* 下载 测试
* @author 国家一级退堂鼓表演艺术家
*
*/
@Controller
@RequestMapping("file")
public class UoloadController {
@RequestMapping(value="/upload",method=RequestMethod.POST)
@ResponseBody
public String upload(MultipartFile file,HttpServletRequest request) throws IOException{
String path = request.getSession().getServletContext().getRealPath("upload");
String fileName = file.getOriginalFilename();
File dir = new File(path,fileName);
if(!dir.exists()){
dir.mkdirs();
}
file.transferTo(dir);
return fileName;
}
@RequestMapping("/down")
public void down(HttpServletRequest request,HttpServletResponse response) throws Exception{
String fileName = request.getSession().getServletContext().getRealPath("upload")+"/101.jpg";
InputStream bis = new BufferedInputStream(new FileInputStream(new File(fileName)));
String filename = "下载文件.jpg";
filename = URLEncoder.encode(filename,"UTF-8");
response.addHeader("Content-Disposition", "attachment;filename=" + filename);
response.setContentType("multipart/form-data");
BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
int len = 0;
while((len = bis.read()) != -1){
out.write(len);
out.flush();
}
out.close();
}
}