码迷,mamicode.com
首页 > 编程语言 > 详细

Java Web开发中的文件上传与下载

时间:2015-08-16 13:44:58      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

 

一、借助Struts2框架


1.环境搭建
  1.1 导包:conmmons-fileupload-1.2.1.jar conmmons-io-1.4.jar 以及其他框架的jar包

2.页面:略
3.文件上传:
  3.1 设计一个文件上传工具类:FileUploadUtils

    

 1 public class FileUploadUtils{
 2                 
 3                 //文件要上传到的路径,可配置
 4                 @Resource
 5                 private String filePath;
 6 
 7                 private String createNewFilePath(String fileName){
 8 
 9                     //原始路径为配置的filePath
10                     String basePath=filePath;
11                     //子路径为格式化的/年/月/日形式
12                     SimpleDateFormat sdf=new SimpleDateFormat("/yyyy/MM/dd");
13                     String subPath=sdf.format(new Date());
14 
15                     //如果没有目录文件夹就创建
16                     File dir=new File(basePath+subPath);
17                     if(!dir.exists()){
18                         //递归地创建目录文件夹
19                         dir.mkdirs();
20                     }
21                     
22                     //新文件名为UUID+文件扩展名
23                     String newFileName=UUID.randomUUID().toString()//
24                         +fileName.substring(fileName.lastIndexOf("."));
25                     //这是新生成的文件路径
26                     String newFilePath=basePath+subPath+newFileName;
27 
28                     return newFilePath;
29                 }
30 
31                 public String saveUploadFile(File uploadFile,String fileName){
32                     //新文件路径
33                     String newFilePath=createNewFilePath(filePath,fileName);
34                     try{
35                         //获取文件输入输出流
36                         InputStream in=new FileInputStream(uploadFile);
37                         OutputStream out=new FileOutputStream(newFilePath);
38                         
39                         //往目标路径文件中写字节流
40                         byte[] buff=new byte[1024];
41                         int len=0;
42                         while((len=in.read(buff))!=-1){
43                             out.write(buff,0,len);
44                         }
45 
46 
47                     }catch(Exception e){
48                     
49                         throw new RuntimeException();
50                     }finally{
51                         try{
52                             in.close();
53                         }catch(Exception e){
54                             throw new RuntimeException();
55                         }finally{
56                             try{
57                                 out.close();
58                             }catch(Exception e){
59                                 throw new RuntimeException();
60                             }
61                             
62                         }
63                         
64                     }
65                     
66                     return newFilePath;
67                 }
68             }
69             

 

 

4.文件下载:Struts2中封装之后就很简单

 1 public class XXXAction extends ActionSupport implements ModelDriven{
 2             
 3             //供下砸用的输入流
 4             private InputStream inputStream;
 5             //需写setter,getter;
 6 
 7             //模型驱动,简化传参,替代了Strtus1的ActionForm
 8             private XXX model=new XXX();
 9             public XXX getModel(){
10                 returm model;
11             }
12 
13             public String download(){
14                 XXX xxx=xxxService.getById(model.getId());
15                 inputStream=new FileInputStream(new File(xxx.getPath()));
16                 
17                 //解决中文乱码问题
18                 String fileName=URLEncoder.encode(xxx.getFileName(),"utf-8");
19                 ActionContext.getContext.put("fileName",fileName);
20             }
21         }

 


剩下的就是struts.xml的配置了

 

二、用纯Servlet(续)

Java Web开发中的文件上传与下载

标签:

原文地址:http://www.cnblogs.com/liaowanzhong/p/4733991.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!