在修改項目時,發現以前的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關閉流文件");
//關閉流文件等資源性
}
}
這樣,就可以實現從SmartUload到FileUpload的無縫轉換了。原來項目的代 碼不用改動。自動轉化成fileUPlaod上傳機制。