jsp+mysql文件上傳下載方法比較(解決中文編碼問題)

 1:smartUpload這個組件被普遍的應用,可是由於沒有繼續更新而面臨着被淘汰的地步,另外因爲該組件對於中文不是很友好。可是它的使用者仍然不少,網上的資源也比較多,筆者作的實驗就是基於smartupload組件的上傳下載方法,源碼見附件。html

對於其中文處理:頁面編碼爲utf-8,數據庫字段設置爲utf-8,my.ini文件中java

default-character-set=GBK設置默認類型爲GBK,採用的smartupload.jar包爲支持UTF-8的包。web

源碼見附件,包含數據庫設計語句。數據庫

2:commons-fileupload是如今應用比較多的組件,方法與smartupload相似,應用較多,上傳文件大小比smartupload稍大,可是也超不過100m。中文處理時須要轉換編碼類型。服務器

3:java的uploadbean,較好的解決方案,網上有詳細的demojsp

在此轉載一個:原做者爲fbysss數據庫設計

Demo: 
1.表單頁面sssformdata.jsp 
<%@ page contentType="text/html;charset=gb2312" %> 
<head> 
<title>fbysss UploadBean 示例</title> 
<!--meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"--> 
<!--meta http-equiv="Content-Type" content="text/html; charset=gb2312"--> 
</head> 
<FORM name="form1" METHOD="POST" ACTION="sssupload.jsp" ENCTYPE="multipart/form-data"> 
<input name="title" type= "text" value="中文字"> 
<td class="bodystyle">附件</td> 
<td class="bodystyle"> <input name="attach" type="FILE" id="attach" size="50" > </td> 
<input name="ok" type= "submit" value="提交"> 
</form> 
2.讀取表單頁面sssgetdata.jsp 
<!-- 
//========================================================================== 
//文件:UploadBean上傳實例 
//功能:解決中文亂碼,完成文件上傳,並提供上傳更名解決方案 
//做者:fbysss 
//msn:jameslastchina@hotmail.com 
//========================================================================== 
--> 
<%@ page contentType="text/html;charset=GBK" %> 
<%@ page language="java" import="com.jspsmart.upload.*"%> 
<%@ page import="java.text.SimpleDateFormat"%> 
<%@ page import="java.io.File"%> 
<%@ page import="java.util.*"%> 
<%@ page import="javazoom.upload.*"%> 
<%@ page import="uploadutilities.FileMover"%> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
</head> 
<% 
request.setCharacterEncoding("GBK");//設置編碼格式,就不用一個個轉碼了。 
FileMover fileMover = new FileMover();//你也能夠使用自帶的實例中jsp:useBean的形式。 
UploadBean upBean = new UploadBean(); 
MultipartFormDataRequest mrequest = null; 
Hashtable files = null;ide

if (MultipartFormDataRequest.isMultipartFormData(request)) 
{測試

mrequest = new MultipartFormDataRequest(request,null,100*1024*1024,MultipartFormDataRequest.COSPARSER,"GBK");//注意這裏也要設置編碼參數 
String sTt0 = mrequest.getParameter("title"); 
out.println("<br>Title0是:"+sTt0+"<br>"); 
String sTt1 = new String(sTt0.getBytes("ISO-8859-1"),"GBK"); 
out.println("<br>Title1是:"+sTt1+"<br>"); 
//這裏用來測試title參數是否正確。調試的時候,加一句if (true)return;便可。 
files = mrequest.getFiles(); 
}ui


//獲取修改前的文件名 
String sOldFileName =mrequest.getParameter("oldfilename"); 
out.println("sOldFileName:"+sOldFileName); 
String sWebRootPath = request.getRealPath("/");//獲得你的web應用的根。 
String sPath=sWebRootPath+"attach"; 
int iFileCount = 0; 
String sServerFileName=""; 
String sLocalFileName = ""; 
//文件獲取 
if ( (files != null) || (!files.isEmpty()) ) {

iFileCount = files.size(); 
UploadFile file = (UploadFile) files.get("attach"); 
sLocalFileName=file.getFileName(); 
out.println("sLocalFileName:"+sLocalFileName); 
int ii= sLocalFileName.indexOf("."); //取文件名的後綴 
String sExt = sLocalFileName.substring(ii,sLocalFileName.length()); 
//獲得不重複的文件名 
java.util.Date dt = new java.util.Date(System.currentTimeMillis()); 
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS"); 
sServerFileName= fmt.format(dt); 
sServerFileName =sServerFileName + sExt; 
//若是不存在該目錄,則新建一個 
File dir =new File(sPath); 
if (!dir.exists()){ 
dir.mkdirs(); 

upBean.setFolderstore(sPath);//設置要上傳的目錄 
upBean.addUploadListener(fileMover);//增長filMover監聽 
fileMover.setNewfilename(sServerFileName);//設置服務器上的文件名 
upBean.store(mrequest, "attach");//上傳 
out.println("file path is "+sPath+"/"+sServerFileName); 
}

%>

Demo注意事項:cos.jar,uploadbean.jar,filemover.jar這幾個包必須有。 
必備下載地址: 
UploadBean1.5:http://wcarchive.cdrom.com/pub/simtelnet/winnt/java/uploadbean1_5.zip 
FileMover1.3:http://www.javazoom.net/jzservlets/uploadbean/addons/filemover1.3.zip 
參考下載地址: 
ChinesUpload例子:http://www.javazoom.net/jzservlets/uploadbean/addons/ChineseUpload.zip

相關文章
相關標籤/搜索