JSP組件commons-fileupload實(shí)現(xiàn)文件上傳
本文實(shí)例為大家分享了JSP使用commons-fileupload實(shí)現(xiàn)文件上傳代碼,供大家參考,具體內(nèi)容如下
1、準(zhǔn)備:
將commons-fileupload-1.1.zip和commons-io-1.1.zip復(fù)制到"\WEB-INF\lib"目錄下
2、首先是Servlet: FileUpload.java
package servlet;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class FileUpload extends HttpServlet {
private String uploadPath="E:\\addnetFile\\";//要上傳文件的目錄
private File tempPath=new File("E:\\tempFile\\");//存放上傳的文件的目錄
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=GB2312");
response.setCharacterEncoding("gb2312");
PrintWriter out=response.getWriter();
out.println("請(qǐng)求內(nèi)容的長(zhǎng)度為:"+request.getContentLength());
out.println("請(qǐng)求內(nèi)容的類型為:"+request.getContentType());
DiskFileItemFactory factory=new DiskFileItemFactory();
factory.setRepository(tempPath);
factory.setSizeThreshold(4096);
ServletFileUpload upload=new ServletFileUpload(factory);
upload.setSizeMax(1000000);
List<?> fileitems=null;
try{
fileitems=upload.parseRequest(request);
Iterator<?> iterator=fileitems.iterator();
String regex=".+\\\\(.+)$";
String[] errortype={".exe",".com",".cgi",".asp"};
Pattern p=Pattern.compile(regex);
while(iterator.hasNext()){
FileItem item=(FileItem) iterator.next();
if(!item.isFormField()){
String name=item.getName();
long size=item.getSize();
if(name==null||name.equals("")&&size==0)
continue;
Matcher m=p.matcher(name);
if(m.find()){
for(int temp=0;temp<errortype.length;temp++){
if(m.group(1).endsWith(errortype[temp]))
throw new IOException(name+":wrong type");
}
try{
item.write(new File(tempPath,m.group(1)));
out.println(name+" "+size+"<br/>");
out.println("上傳成功");
}catch(Exception e){
out.println("333"+e);
}
}
else{
throw new IOException("fail to upload");
}
}
}
}catch(IOException e){
out.println("222"+e);
}
catch(FileUploadException e1){
e1.printStackTrace();
out.println("111"+e1);
}
}
public void init() throws ServletException {
if(!new File(uploadPath).isDirectory())
new File(uploadPath).mkdir();
if(!tempPath.isDirectory())
tempPath.mkdir();
}
public void destroy(){
super.destroy();
}
}
3、其次是html:Uploadfile.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Uploadfilel.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <form action="/Firstjsp/servlet/FileUpload" method="post" enctype="multipart/form-data" name="form1"> <input type="file" name="file"/> <input type="submit" name="submit" value="upload"/> </form> <form action="/Firstjsp/servlet/FileUpload" method="post" enctype="multipart/form-data" name="uploadform"> <table> <tr> <td> 文件1:<input type="file" name="X" size="40"/> </td> </tr> <tr> <td> 文件2:<input type="file" name="Y" size="40"/> </td> </tr> <tr> <td> 文件3:<input type="file" name="Z" size="40"/> </td> </tr> </table> <input type="submit" name="upload" value="開(kāi)始上傳"/> </form> </body> </html>
4、最后是配置web.xml
<servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>FileUpload</servlet-name> <servlet-class>servlet.FileUpload</servlet-class> </servlet> <servlet-mapping> <servlet-name>FileUpload</servlet-name> <url-pattern>/servlet/FileUpload</url-pattern>
首先運(yùn)行html,servlet處理上傳請(qǐng)求
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Apache commons fileupload文件上傳實(shí)例講解
- java組件commons-fileupload實(shí)現(xiàn)文件上傳、下載、在線打開(kāi)
- Java組件commons fileupload實(shí)現(xiàn)文件上傳功能
- JavaEE組件commons-fileupload實(shí)現(xiàn)文件上傳、下載
- java組件commons-fileupload文件上傳示例
- Apache Commons fileUpload文件上傳多個(gè)示例分享
- java組件commons-fileupload實(shí)現(xiàn)文件上傳
- commons fileupload實(shí)現(xiàn)文件上傳的實(shí)例代碼
- java組件fileupload文件上傳demo
- java使用common-fileupload實(shí)現(xiàn)文件上傳
相關(guān)文章
在JSP中訪問(wèn)Oracle數(shù)據(jù)庫(kù)
在JSP中訪問(wèn)Oracle數(shù)據(jù)庫(kù)...2006-10-10
JSP下動(dòng)態(tài)INCLUDE與靜態(tài)INCLUDE的區(qū)別分析
這篇文章給大家介紹了JSP下動(dòng)態(tài)INCLUDE與靜態(tài)INCLUDE的區(qū)別分析,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2007-12-12
JSP使用Servlet過(guò)濾器進(jìn)行身份驗(yàn)證的方法
這篇文章主要介紹了JSP使用Servlet過(guò)濾器進(jìn)行身份驗(yàn)證的方法,結(jié)合實(shí)例形式分析了Servlet過(guò)濾器的實(shí)現(xiàn)方法及jsp身份驗(yàn)證的具體使用技巧,需要的朋友可以參考下2015-12-12
Windows下JSP開(kāi)發(fā)環(huán)境的配置
Windows下JSP開(kāi)發(fā)環(huán)境的配置...2006-10-10
JDBCTM 指南:入門(mén)4 - Statement
JDBCTM 指南:入門(mén)4 - Statement...2006-10-10

