多文件上传

  1. /多文件上传  
  2.     public ActionForward multipartFileUpload(ActionMapping mapping, ActionForm form,  
  3.             HttpServletRequest request, HttpServletResponse response)  
  4.             throws Exception {  
  5.         //转换为自己的表单,这里只要经过了一个表单就可以 ,不管里面的内容  
  6.         MulForm form2 = (MulForm)form;  
  7.         //多文件上传,通过form来获得页面的input = file的元素  
  8.         //用一个Hashtable来接收,<String,FormFile> ----键是页面的<input type="file" name="key">里面的name属性  
  9.         Hashtable<String, FormFile> hashtable = form2.getMultipartRequestHandler().getFileElements();  
  10.         //因为我在struts-config.xml里面控制了上传文件的大小  
  11.         //当文件超过了大小的时候,hashtable里面是空的,所以在这里判断一下  
  12.         if(hashtable.size()==0){  
  13.             request.setAttribute("info""文件太大了!");  
  14.         }else {  
  15.             //循环里面的file  
  16.             for (Entry<String, FormFile> entry : hashtable.entrySet()) {  
  17.                 //如果是没有上传文件,是空的要判断一下,用名字的长度来判断  
  18.                 if(entry.getValue().getFileName().length()!=0){  
  19.                     //获得保存路径  
  20.                     String fileName = entry.getValue().getFileName();  
  21.                     String webPath = ConfigPath.getProp().getProperty(ConfigPath.propKey);  
  22.                     String realPath = this.getServlet().getServletContext().getRealPath(webPath);  
  23.                     String savePath = ConfigPath.getSavePath(fileName, realPath);  
  24.                     //建立流的管道  
  25.                     InputStream inStream = entry.getValue().getInputStream();  
  26.                     OutputStream outStream = new FileOutputStream(savePath);  
  27.                     //开始传  
  28.                     int len = 0 ;  
  29.                     byte[] b = new byte[1024];  
  30.                     while ((len = inStream.read(b))!=-1) {  
  31.                         outStream.write(b, 0, len);  
  32.                     }  
  33.                     outStream.close();  
  34.                     inStream.close();  
  35.                 }  
  36.             }  
  37.             request.setAttribute("info""上传成功!");  
  38.         }  
  39.           
  40.         return mapping.findForward("showInfo");  
  41.     }  
  42.   
  43. ================================  
  44.   
  45. public class ConfigPath {  
  46.     public static final String propKey = "hwt.MultipartUpload.relativePath";  
  47.     private static Properties prop = null ;  
  48.     private ConfigPath(){}  
  49.       
  50.     static{  
  51.         prop = new Properties();  
  52.         //配置文件要放在类文件目录下面  
  53.         InputStream inStream = ConfigPath.class.getResourceAsStream("/configPath.properties");  
  54.         try {  
  55.             prop.load(inStream);  
  56.         } catch (IOException e) {  
  57.             e.printStackTrace();  
  58.         }  
  59.     }  
  60.       
  61.     //处理文件上传的目录和文件名  
  62.     public static String getSavePath(String fileName,String realPath){  
  63.         //把文件名改成唯一的文件名 用UUID  
  64.         UUID uuid = UUID.randomUUID();  
  65.         String extraName = fileName.substring(fileName.lastIndexOf("."));  
  66.         String uniqueFileName = uuid+extraName;  
  67.           
  68.         //文件的存储目录  
  69.         Calendar calendar = new GregorianCalendar();  
  70.         //===注意这里的盘符啊,在java中有盘符的绝对路径的时候要\\,因为编译器不认识\ 要转移\\  
  71.         //==相对路径就用这个/  
  72.         String path = realPath+"\\"+calendar.get(Calendar.YEAR)+"\\"+calendar.get(Calendar.MONTH)+  
  73.                         "\\"+calendar.get(Calendar.DATE);  
  74.         //不要忘了要创建不存在目录的时候  
  75.         File file = new File(path);  
  76.         if(!file.exists()){  
  77.             file.mkdirs();  
  78.         }  
  79.         return path+"\\"+uniqueFileName;  
  80.     }  
  81.       
  82.     //返回配置文件  
  83.     public static Properties getProp(){  
  84.         return prop;  
  85.     }  
  86. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值