Insurance 项目——上传下载
这是一个java初学者在独立开发一个项目时做的笔记,很多内容仅仅是为了解决当前需求,并未很深入的研究。
项目需求将excel表中的数据导入到数据库中。参考了不少资料,说的不是很清楚,让自己多走了些弯路。不过在此期间,误打误撞的学习了AWS 的S3,也算是一件美事。
先说思路:
上传:
1、本地上传excel到服务器的XX目录下。
2、服务器端解析excel表内数据。
3、遍历每一行数据,存入数据库。
4、考虑到存储空间的问题,存入数据库后删除上传至服务器的文件。
参考资料时,看到说linux系统删除某些文件(特别大?或者其他,英文太多没有深入研究)不会释放这部分存储空间,以后遇到在处理把。
下载:
1、数据库中取得数据。
2、服务器XX目录下建立xls文件,存入数据库中取得的数据。
3、下载到本地。
代码:
所需jar包:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!-- 文件上传 --> 2 <dependency> 3 <groupId>commons-fileupload</groupId> 4 <artifactId>commons-fileupload</artifactId> 5 <version>1.2.2</version> 6 </dependency> 7 <dependency> 8 <groupId>commons-io</groupId> 9 <artifactId>commons-io</artifactId> 10 <version>2.4</version> 11 </dependency>
上传至服务器:
jsp:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <form action="/" method="post" enctype="multipart/form-data"> 2 <input type = "file" name = "mfile"> 3 <input type="submit" value="ok"> 4 </form>
缺少 enctype="multipart/form-data" 会报异常
Controller层就不写了。
service:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 private final String backslash = "/"; 2 private final String temp = "TEMP/Excel/upload"; 3 private final Logger log = Logger.getLogger(FileService.class); 4 5 6 /*上传 7 * */ 8 public String upload (MultipartFile mfile , String uid) { 9 try { 10 String path = request.getSession().getServletContext().getRealPath(temp)+backslash+uid; 11 String fileName = mfile.getOriginalFilename(); 12 File dir = new File(path,fileName); 13 //判断文件是否存在 14 if(!dir.exists()){ 15 dir.mkdirs(); 16 } 17 //MultipartFile自带的解析方法 18 mfile.transferTo(dir); 19 return path+backslash+fileName; 20 } catch (IllegalStateException e) { 21 e.printStackTrace(); 22 log.error(e); 23 return ""; 24 } catch (IOException e) { 25 e.printStackTrace(); 26 log.error(e); 27 return ""; 28 } 29 30 31 }
这里上传就完成了,注意linux系统和windows系统路径的斜线是不同的
下载:
jsp:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <a href="/" >Down</a>
Controller:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 @RequestMapping(value = "/xx.action" ,method ={ RequestMethod.GET, RequestMethod.POST }) 2 public void exportExcel (HttpServletRequest request , HttpServletResponse response ) { 3 4 try { 5 //获取输入流 6 InputStream bis = new BufferedInputStream(new FileInputStream(new File("下载文件路径"))); 7 //设置下载文件名 8 String filename = "MyExcel.xls"; 9 //假如以中文名下载的话 ,转码,免得文件名中文乱码 10 filename = URLEncoder.encode(filename,"UTF-8"); 11 //设置文件下载头 12 response.addHeader("Content-Disposition", "attachment;filename=" + filename); 13 //1.设置文件ContentType类型,这样设置,会自动判断下载文件类型 14 response.setContentType("multipart/form-data"); 15 BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream()); 16 int len = 0; 17 while((len = bis.read()) != -1){ 18 out.write(len); 19 out.flush(); 20 } 21 bis.close(); 22 response.getOutputStream().flush(); 23 response.getOutputStream().close(); 24 out.close(); 25 } catch (IOException e) { 26 e.printStackTrace(); 27 log.error(e); 28 } 29 30 }
不需要有返回值,否则会报异常