準(zhǔn)備工作:要到http://commons.apache.org/fileupload/download_fileupload.cgi下載上傳文件需要的組件類(lèi)庫(kù)并且到
http://commons.apache.org/io/download_io.cgi上下載其IO組件,再有了這兩個(gè)組件之后我們就可以編寫(xiě)一個(gè)上傳的Servlet程序了。
1.建立一個(gè)jsp文件里面有文件域,能夠提交給要處理數(shù)據(jù)的servlet程序的映射路徑,form標(biāo)簽中有enctype="multipart/form-data"這條屬性。
2.然后我們新建一個(gè)處理該P(yáng)ost方法的servlet程序,設(shè)置映射路徑為jsp文件中對(duì)應(yīng)的映射路徑,讓后,我們修改配置文件,在servlet啟動(dòng)時(shí),添加一條屬性,是存放文件的目錄。
3.在servlet init()方法里讀取項(xiàng)目路徑和上傳文件存放目錄,然后編寫(xiě)doPost()方法。具體如下:
第一、在init()方法中利用config取得保存路徑和工程文件位置。
第二、在doPost方法中實(shí)現(xiàn)上傳的數(shù)據(jù)處理。首先需要設(shè)置一下數(shù)據(jù)的編碼格式response.setCharacterEncoding("UTF-8");,以防下面出現(xiàn)亂碼現(xiàn)象。然后,創(chuàng)建一個(gè)DiskFileItemFactory對(duì)象factory,再用ServletFileUpload類(lèi)創(chuàng)建一個(gè)對(duì)象file,將factory對(duì)象包裝起來(lái),再用file的parseRequest()方法接收request的數(shù)據(jù),放入List對(duì)象lst中,用迭代器it接收遍歷file內(nèi)容,用FileItem對(duì)象fileItem循環(huán)接收,讀取數(shù)據(jù),用fileItem的isFormField()方法判斷是否是上傳文件還是表單數(shù)據(jù)項(xiàng),如果是上傳文件,那么我們先需要?jiǎng)?chuàng)建一個(gè)tempFile對(duì)象,利用其getName()方法獲得文件除了路徑外的真實(shí)名稱(chēng),再用init()方法獲得的ServletContent對(duì)象獲得工程的路徑(sc.getRealPath("/")),加上獲得的保存目錄,構(gòu)成上傳文件的具體目錄,在新建一個(gè)File對(duì)象,利用已得到的文件完全路徑建立新對(duì)象realFile最后永fileItem的write()方法包裝realFile將其寫(xiě)入硬盤(pán),完成上傳的功能
init()方法代碼
public void init(ServletConfig config) throws ServletException {
savePath=config.getInitParameter("savePath");
sc=config.getServletContext();
super.init();
}
doPost()方法代碼
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
DiskFileItemFactory factory=new DiskFileItemFactory();
ServletFileUpload file=new ServletFileUpload(factory);
try {
List lst=file.parseRequest(request);
Iterator it=lst.iterator();
while(it.hasNext()){
FileItem fileItem=(FileItem)it.next();
if(fileItem.isFormField()){
System.out.println("表單數(shù)據(jù)的 名稱(chēng):"+fileItem.getFieldName()+
" 表單數(shù)據(jù)的內(nèi)容"+fileItem.getString("UTF-8"));
}
else{
if(fileItem.getName()!=null&&!fileItem.getName().equals("")){
System.out.println("上傳文件的名稱(chēng):"+fileItem.getName());
System.out.println("上傳文件的大小:"+fileItem.getSize());
System.out.println("上傳文件的類(lèi)型:"+fileItem.getContentType());
File tempFile=new File(fileItem.getName());
File realFile=new File(sc.getRealPath("/")+savePath,tempFile.getName());
try {
fileItem.write(realFile);
request.setAttribute("upLoad message","上傳成功");
} catch (Exception e) {
request.setAttribute("upLoad message","上傳失敗");
e.printStackTrace();
}
}
}
}
} catch (FileUploadException e) {
request.setAttribute("upLoad message","上傳失敗");
e.printStackTrace();
}
}
來(lái)自: http://hi.baidu.com/yaoweinan/blog/item/710f29f8c2206c2f4e4aea5f.html
如果遇到下面錯(cuò)誤,那么解決辦法是:
java.lang.ClassNotFoundException: org.apache.commons.io.output.DeferredFileOutputStream
簡(jiǎn)而言之,是因?yàn)楫?dāng)commons-fileupload包從版本1.0升到1.1時(shí),DeferredFileOutputStream.class被移走了。如果Tomcat使用1.1及其以上版本,你得為它找到這個(gè)類(lèi)。解決方法: 進(jìn)入目錄:$CATALINA/server/webapps/manager/WEB-INF/lib,檢查是否存在三個(gè)包: commons-io catalina-manager.jar commons-fileupload.jar 如果缺少commons-io,拷一個(gè)過(guò)來(lái)。 或者直接使用老版本的commons-fileupload.jar