https://github.com/ruqinhu/updown
本次ssm框架的搭建都是正常的配置,除了下面说的包,只需要引入增删改查所需要的包即可。
<!-- Map工具类 -->
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2</version>
</dependency>
<!-- 文件上传下载 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!-- 使用poi进行导入导出excel -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.10-FINAL</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10-FINAL</version>
</dependency>
commons-collections包中有一些增强了的数据结构,可以避免在jdk中数据结构不好用时重复造轮子。
比如Iterator – java.util.Iterator接口定义了标准的Collection遍历方法,但是如果不做改变的使用它,我们得到的是从头到尾一次性的遍历。假如我们需要循环遍历,假如我们需要遍历某一段,假如我们需要遍历满足某些条件的元素,等等等等,我们就不能完全依赖于这个Iterator的标准实现了。
其他两个,commons-fileupload是上传文件时需要导入的包,poi-ooxml是使用新版本的excel需要导入的包,poi老版本excel需要的包。
在使用springMVC获取文件时,需要在配置文件引入如下的bean。
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置默认编码 -->
<property name="defaultEncoding" value="utf-8"></property>
<!-- 上传图片最大大小5M-->
<property name="maxUploadSize" value="5242440"></property>
</bean>
引入commons-fileupload也就是为了使用这个bean。
response.setHeader("content-disposition", "attachment;filename="
+ URLEncoder.encode(realname, "UTF-8"));
只要设置了这个请求头,浏览器自动为你下载。
在上传文件的时候,可以在对应路径的Controller中使用commons-fileupload提供的MultipartHttpServletRequest.getFile(“前端input的文件名”)进行文件的获取。在前台使用form表单提交的时候。
<form action="http://localhost:8080/ssm/upload" method="post" enctype="multipart/form-data">
选择文件:<input type="file" name="upfile" width="120px">
<input type="submit" value="上传">
</form>
下面是主要的代码,大概的逻辑是这样的。
String filePath=request.getServletContext().getRealPath("/");
String fileName=request.getOriginalFileName();
File dir=new File(filePath,fileName);
if(!dir.exists()){
dir.mkdirs();
}
file.transferTo(dir);//MultipartFile自带的解析方法
return "redidrect:/file/listFile";
ServletContext sc=request.getSession.getServletContext;
File fileLists=sc.getRealPath("/upload");
Map<String,Object> listFile=new HashMap<String,Object>();
File[] files=fileLists.listFile;
for(File file:files){
map.put(file.getName,file);
}
request.setAttribute("listFile",listFile);
try{
String fileName=request.getParameter("filename");
fileName =new String(fileName.getBytes("iso8859-1"),"UTF-8");
ServletContext sc=request.getSession.getServletContext();
String fileSaveRootPath=sc.getRealPath("/upload");
File file=new File(fileSaveRootPath+"\\"+fileName);
if(!file.exists()){
request.setAttribute("message","您要下载的资源已删除");
System.out.println("您要下载的资源已经被删除")
return;
}
String realname=fileName.subString(fileName.indexOf("_")+1);
response.setHeader("content-disposition","attachment;filename="+URLEncoder.encode(realname,"UTF-8"));
FileInputStream in=new FileInputStream(fileSaveRootPath+"\\"+fileName);
OutputStream out=reponse.getOutputStream();
byte buffer[]=new byte[1024];
int len=0;
while((len=in.read(buffer))>0){
out.write(buffer,0,len);
}
in.close();
out.close();
}catch(Exception e){
}
详细解释,代码中有,地址:
https://github.com/ruqinhu/updown