当前位置:天才代写 > tutorial > JAVA 教程 > 从SmartUpload到FileUpload的无缝转移

从SmartUpload到FileUpload的无缝转移

2017-11-11 08:00 星期六 所属: JAVA 教程 浏览:276

副标题#e#

在修改项目时,发明以前的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
         }
       }


#p#副标题#e#

我写的中间件类,类名也叫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封锁流文件");
     //封锁流文件等资源性
   }
}

这样,就可以实现从SmartUload到FileUpload的无缝转换了。本来项目标代 码不消窜改。自动转化成fileUPlaod上传机制。

 

    关键字:

天才代写-代写联系方式