正文

HTML标记库 - 文件上传2007-03-13 10:34:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/hurongliang/23910.html

分享到:

html:file

参考”struts学习笔记.doc”

自编的一个的文件上传类

com.cn.rolia.util.struts.upload .UploadUtil.java

package com.cn.rolia.util.struts.upload;

 

import org.apache.struts.upload.FormFile;

 

import java.io.BufferedOutputStream;

import java.io.BufferedInputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.HashSet;

import java.util.Iterator;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

 

/**

 *

 * @author rolia

 *

 */

public class UploadUtil {

         /*

          * FormFile,表示要上传的文件

          */

         FormFile file;

         /**

          * 保存目录

          */

         String savePath;

         /**

          * 文件大小

          */

         int fileSize;

         /**

          * 上传文件的最大值,单位为 byte

          */

         int sizeLimited;

         /**

          * 文件名

          */

         String fileName;

         /**

          * 文件类型

          */

         String fileType;

         /**

          * 允许的文件类型

          */

         HashSet allowedExt;

        

         public UploadUtil(FormFile file){

                   this.file = file;

                   this.fileSize=file.getFileSize();

                   this.fileName=file.getFileName();

                   this.fileType=file.getContentType();

                   this.savePath = null;

                   allowedExt = null;

                   this.sizeLimited = 0;

         }

 

         public UploadUtil(FormFile file,String savePath){

                   this.file = file;

                   this.fileSize=file.getFileSize();

                   this.fileName=file.getFileName();

                   this.fileType=file.getContentType();

                   this.savePath = savePath;

                   allowedExt = null;

                   this.sizeLimited = 0;

         }

         public UploadUtil(FormFile file,String savePath,HashSet allowedExt){

                   this.file = file;

                   this.fileSize=file.getFileSize();

                   this.fileName=file.getFileName();

                   this.fileType=file.getContentType();

                   this.savePath = savePath;

                   this.allowedExt = allowedExt;

                   this.sizeLimited = 0;

         }

         public UploadUtil(FormFile file,String savePath,HashSet allowedExt,int sizeLimited){

                   this.file = file;

                   this.fileSize=file.getFileSize();

                   this.fileName=file.getFileName();

                   this.fileType=file.getContentType();

                   this.savePath = savePath;

                   this.allowedExt = allowedExt;

                   this.sizeLimited = sizeLimited;

         }

 

        

         public UploadUtil(FormFile file,String savePath,HashSet allowedExt,int sizeLimited,String fileName){

                   this.file = file;

                   this.fileSize=file.getFileSize();

                   this.fileName=fileName;

                   this.fileType=file.getContentType();

                   this.savePath = savePath;

                   this.sizeLimited = sizeLimited;

                   this.allowedExt = allowedExt;

         }

        

         /**

          * 保存文件操作。

          * 以下情况会抛出异常:

          * 1      上传文件的类型不允许

          * 2      上传文件的大小超出允许的最大值

          * 3      没有指定文件的保存路径

          * 4      指定的文件保存路径不存在

          * 5      服务器上已有与上传文件同名的文件

          * 6     IO错误

          * @throws Exception 文件保存过程中出现的异常。

          */

         public void writeFile()throws Exception{

                   Log log = LogFactory.getLog(UploadUtil.class);

                  

                   //过滤上传文件的类型

                   if(allowedExt==null){

                            log.info("Allow all types of file being uploaded!");

                   }else{

                            boolean flag = false;

                            Iterator iterator = allowedExt.iterator();

                            while(iterator.hasNext() && flag==false){

                                     flag = this.fileName.endsWith(iterator.next().toString());

                            }

                            if(flag==false){

                                     log.warn("The type of file \""+this.fileName+"\" is firbided!");

                                     throw new UploadException("The type of file \""+this.fileName+"\" is firbided!");

                            }

                   }

                  

                   //设置允许上传文件的最大值

                   if((sizeLimited!=0) && (this.fileSize > this.sizeLimited)){

                            log.warn("The file is greater than "+this.sizeLimited+"bytes");

                            throw new UploadException("The file is greater than "+this.sizeLimited+"bytes");

                   }

                  

                   //检查是否指定了保存路径

                   if(savePath==null){

                            log.error("The destination file being saved is not assigned!");

                            throw new UploadException("The destination file being saved is not assigned!");

                   }

                  

                   //检查指定的路径是否存在

                   File saveFile = new File(savePath+"\\"+this.fileName);

                   if(!saveFile.getParentFile().exists()){

                            log.error("The Directory: "+savePath+" is not exists!");

                            throw new UploadException("The Directory: \""+savePath+"\" is not exists!");

                   }

                  

                   //检查服务器里是否已经存在此上传的文件

                   if(saveFile.exists()){

                            log.error("The file: "+saveFile.getPath()+" has existed!");

                            throw new UploadException("The file: \""+saveFile.getPath()+"\" has existed!");

                   }

                  

                   //开始上传

                   InputStream in = null;

                   OutputStream out = null;

                   try{

                            in = new BufferedInputStream(file.getInputStream());

                            out = new BufferedOutputStream(new FileOutputStream(saveFile));

                            int b;

                            while((b=in.read())!=-1){

                                     out.write((char)b);

                            }

                   }catch(IOException e){

                            log.error(e.getMessage());

                            throw new IOException(e.getMessage());

                   }

                   finally{

                            try{

                                     if(out!=null)out.close();

                                     if(in!=null)in.close();

                            }catch(IOException e){

                                     log.error(e.getMessage());

                                     throw new IOException(e.getMessage());

                            }

                   }

                   file.destroy();

                   log.info("File \""+saveFile.getPath()+"\" has been saved successfully!");

         }

         public HashSet getAllowedExt() {

                   return allowedExt;

         }

         public FormFile getFile() {

                   return file;

         }

         public String getFileName() {

                   return fileName;

         }

         public int getFileSize() {

                   return fileSize;

         }

         public String getFileType() {

                   return fileType;

         }

         public int getSizeLimited() {

                   return sizeLimited;

         }

         public String getSavePath() {

                   return savePath;

         }

         public void setAllowedExt(HashSet allowedExt) {

                   this.allowedExt = allowedExt;

         }

         public void setFileName(String fileName) {

                   this.fileName = fileName;

         }

         public void setSizeLimited(int sizeLimited) {

                   this.sizeLimited = sizeLimited;

         }

         public void setSavePath(String savePath) {

                   this.savePath = savePath;

         }

}

阅读(4097) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册