开发学院软件开发Java 从SmartUpload到FileUpload的无缝转移 阅读

从SmartUpload到FileUpload的无缝转移

 2009-10-30 00:00:00 来源:WEB开发网   
核心提示:在修改项目时,发现以前的jsp项目,从SmartUpload到FileUpload的无缝转移,附件上传用的是SmartUpload,系统中多处都用的是这这种方式上传,比如doc,xml,这种上传附件的机制大小只能传十兆左右, 而客户现在要求

在修改项目时,发现以前的jsp项目,附件上传用的是SmartUpload,系统中多处都用的是这这种方式上传,这种上传附件的机制大小只能传十兆左右,

而客户现在要求,至少50M,所以原有的SmartUpload不能满足需求,所以打算用Fileupload来实现附件上传功能。但如果换FileUpload,则系统代码改动量很大,大概有50于处地方都需要修改,遂放弃,直接修改代码的想法。

于是,看了一些代码后,自己写了一个从SmartUload到FileUpload转接的中间件程序,可实现不用修改原有SmartUpload上传的代码。

原上传的主要代码见下面:

Java代码

SmartUpload objUpload = new SmartUpload(pageContext);  
///主要代码  
if(objUpload.getCount()>0)  
      {  
        for(int i=1;i<=objUpload.getCount();i++){  
          ps.setString(9,objUpload.getUgetContentType(i));  
          ps.setString(10,objUpload.getUFileName(i));  
          ps.setBinaryStream(11,objUpload.getUFileInputStream(i),  objUpload.getFLength(i));//Content  
        }  
      }  

我写的中间件类,类名也叫SmartUpload,但用的是Fileupload上传的机制:

Java代码

package gui;  
import java.io.*;  
import java.sql.ResultSet;  
import java.sql.SQLException;  
import java.util.*;  
import javax.servlet.ServletConfig;  
import javax.servlet.ServletContext;  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import javax.servlet.http.HttpSession;  
import javax.servlet.jsp.JspWriter;  
import javax.servlet.jsp.PageContext;  
import org.apache.commons.fileupload.*;  
import org.apache.commons.fileupload.servlet.*;  
import org.apache.commons.fileupload.disk.*;  
  
  
// Referenced classes of package gui:  
// SmartFiles, SmartRequest, SmartUploadException, SmartFile  
  
public class SmartUpload  
{  
  
    
   protected Hashtable parameters;//保存普通form表单域  
   protected Hashtable files;//保存上传的文件    
   private int sizeThreshold = 4096;  
   public int maxflag=0;  
  private long filemaxsize=100*1024*1024;  //默认100MB   
  protected HttpServletRequest m_request;  
  protected HttpServletResponse m_response;  
  protected ServletContext m_application;  
  private PageContext page;  
  private String pencoding;  
    
  public SmartUpload()  
  {  
  } 
     //构造方法 参数一 pagecontex 参数二 一般为GBK 参数三 上传的最大文件 单位MB  
  public SmartUpload(PageContext pagecontext,String encoding,long filesize)  
  throws ServletException, IOException,FileUploadException  
  {  
    page=null;  
   page=pagecontext;  
   m_request=(HttpServletRequest)page.getRequest();  
   m_response=(HttpServletResponse)page.getResponse();  
   if(encoding==null||"".equals(encoding)) encoding="GBK";  
   this.pencoding=encoding;     
   if(filesize<3) filesize=100;  
   System.out.println("fileupload版本号:sun2009101303. 最大上传:"+filesize+"Mb");  
   this.filemaxsize=filesize*1024*1024;     
   pageinit(m_request);   
      
  }   
// 构造方法 参数一 pagecontex 参数二 上传的最大文件 单位MB  
  public SmartUpload(PageContext pagecontext,long filesize)  
  throws ServletException, IOException,FileUploadException  
  {  
  this(pagecontext,"GBK",filesize);  
  }  
    
// 构造方法 参数一 pagecontex 参数二  编码格式  
  public SmartUpload(PageContext pagecontext,String encode)  
  throws ServletException, IOException,FileUploadException  
  {  
  this(pagecontext,encode,100);  
  }    
    
// 构造方法 参数一 pagecontex 默认GBK 默认大小100MB   
  public SmartUpload(PageContext pagecontext)  
     throws ServletException, IOException,FileUploadException  
  {  
    this(pagecontext,"GBK",100);  
  }   
    
  public void pageinit(HttpServletRequest request) throws FileUploadException  
  {  
    int filecount=0; //附件个数  
    parameters = new Hashtable();  
    files = new Hashtable();  
    DiskFileItemFactory factory = new DiskFileItemFactory();  
    factory.setSizeThreshold(sizeThreshold);  
    ServletFileUpload upload = new ServletFileUpload(factory);  
    upload.setHeaderEncoding(this.pencoding);  
      
    upload.setFileSizeMax(filemaxsize);  
    try {  
      List items = upload.parseRequest(request);  
      Iterator iterator = items.iterator();  
      while(iterator.hasNext()){  
        FileItem item = (FileItem)iterator.next();  
        if(item.isFormField()){            
          String fieldName = item.getFieldName();  
          String value = item.getString();  
          //----------  
          if(parameters.containsKey(fieldName))  
          {  
            Hashtable hashtable = (Hashtable)parameters.get(fieldName);  
            hashtable.put(new Integer(hashtable.size()), value);  
          } else  
          {  
            Hashtable hashtable1 = new Hashtable();  
            hashtable1.put(new Integer(0), value);  
            parameters.put(fieldName, hashtable1);  
          }  
          //------------  
        }else{  
          //去除掉空的。没选择文件的file。  
          if(item.getSize()>filemaxsize)  
          {  
            maxflag=1;  
            System.out.println("文件过大="+item.getSize()+";最大值为="+filemaxsize/1024/1024+"MB");  
          }  
          if(item.getName()!=null&&!"".equals(item.getName()))  
          {  
          filecount=filecount+1;                  
          files.put(filecount+"", item);  
          }  
           
        }  
      }  
    } catch (FileUploadException e) {  
      e.printStackTrace();  
    }  
  }  
    
    
  /**取得表单元素值。**/  
  public String getParameter(String s)  
  {  
    return this.getParameter(s,this.pencoding);      
      
  }  
    
  /**取得表单元素值。 可以选择编码方式,根据传递过来的来设定**/  
  public String getParameter(String s,String encode)  
  {  
      
    if(s == null)  
      throw new IllegalArgumentException("表单名字无效!!!");  
    Hashtable one = (Hashtable)parameters.get(s);  
    if(one==null)  
      return null;  
    String returnvalue=(String)one.get(new Integer(0));  
    if(returnvalue==null) returnvalue="";  
    try {  
      returnvalue=new String(returnvalue.getBytes("ISO-8859-1"),encode);  
    } catch (UnsupportedEncodingException e) {  
      e.printStackTrace();  
      return null;  
    }       
    if(returnvalue == null)  
      return null;  
    else  
      return returnvalue;  
  }  
    
  public String[] getParameterValues(String s)  
  {  
    if(s == null)  
      throw new IllegalArgumentException("Form's name is invalid or does not exist (1305).");  
    Hashtable hashtable = (Hashtable)this.parameters.get(s);  
    if(hashtable == null)  
      return null;  
    String as[] = new String[hashtable.size()];  
    for(int i = 0; i < hashtable.size(); i++)  
      as[i] = (String)hashtable.get(new Integer(i));  
    return as;  
  }  
  public SmartUpload getRequest()  
  {    
    return this;  
  }  
  public Enumeration getParameterNames()  
  {  
    return this.parameters.keys();  
  }  
  public SmartUpload getFiles()  
  {  
      return this;  
  }  
    
  public int getCount() //返回附件个数  
  {  
     return files.size();  
  }  
  public int getFCount() //返回附件个数  
  {  
    return getCount();  
  }  
  public String getUFileName(int aa)  
  {  
      //aa表示第几个附件,返回上传附件的名字  
      
     FileItem item=(FileItem)files.get(aa+"");     
     String fileName = item.getName();       
     fileName = fileName.substring(fileName.lastIndexOf("\\")+1);       
     return fileName;  
  }  
  public String getFieldNames(int aa) //返回附件表单域的name  
  {  
     FileItem item=(FileItem)files.get(aa+"");  
     return item.getFieldName();  
  }  
  public String getUgetContentType(int aa)  
  {  
//   aa表示第几个附件,返回附件的格式,比如doc,xml,mp3  
     FileItem item=(FileItem)files.get(aa+"");     
   return item.getContentType();  
  }  
    
  public InputStream getUFileInputStream(int aa) throws IOException//返回输入流  
  {  
    FileItem item=(FileItem)files.get(aa+"");    
     return item.getInputStream();      
  }  
    
    
  public int getFLength(int aa) //返回附件大小  
  {  
     FileItem item=(FileItem)files.get(aa+"");     
     return (int)item.getSize();  
  }  
  public boolean isUfile(int aa) //判断该附件是否为空。  
  {  
    if(getUFileName(aa).equals(""))  
      return false;  
    else  
      return true;  
  }  
  public void clean()  
  {  
    //System.out.println("g关闭流文件");  
    //关闭流文件等资源性  
  }  
}  

1 2  下一页

Tags:SmartUpload FileUpload 无缝

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接