国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

java基礎(chǔ)篇---文件上傳(commons-FileUpload組件)

2019-11-14 22:39:10
字體:
供稿:網(wǎng)友
java基礎(chǔ)篇---文件上傳(commons-FileUpload組件

上一篇講解了smartupload組件上傳,那么這一篇我們講解commons-FileUpload組件上傳

FileUpload是Apache組織(www.apache.org)提供的免費(fèi)的上傳組件,可以直接從Apache站點(diǎn)上下載(下載地址:http://commons.apache.org/fileupload/),本文使用的版本是1.2.1,但是FileUpload組件本身還依賴于commons組件,所以從Apache下載此組件的時(shí)候還需要連同commons組件的IO包一起下載(下載地址:http://commons.apache.org/io/)commons-fileUpload上傳組件對(duì)中文進(jìn)行了良好的處理,對(duì)上傳文件不會(huì)出現(xiàn)中文亂碼問題,是目前最廣泛的組件,將commons-fileupload-1.2.1.jar和commons-io-1.4.jar配置到TOMCAT_HOME/lib/目錄中³FileUpload的具體上傳操作與SmartUpload相比有著很高的復(fù)雜度,下面來看一看FileUpload上傳的基本步驟:
  1. 1創(chuàng)建磁盤工廠:DiskFileItemFactory factory = new DiskFileItemFactory();
  2. 創(chuàng)建處理工具:ServletFileUpload upload = new ServletFileUpload(factory);
  3. 設(shè)置上傳文件大小:upload.setFileSizeMax(3145728);
  4. 接收全部?jī)?nèi)容:List<FileItem> items = upload.parseRequest(request);
上傳原理使用fileupload組件接收完全部的數(shù)據(jù)之后,所有的數(shù)據(jù)都保存在了List集合之中,則就需要使用Iterator輸出每一個(gè),但是由于其中既有普通的文本數(shù)據(jù)又有上傳的文件,每一個(gè)上傳內(nèi)容都使用一個(gè)FileItem類對(duì)象表示。所以當(dāng)使用Iterator依次取出每一個(gè)FileItem對(duì)象的時(shí)候,就可以使用FileItem類中的isFormField()方法來判斷當(dāng)前操作的內(nèi)容是普通的文本還是上傳文件,如果是上傳文件,則將文件的內(nèi)容依次取出;如果是普通的文本,則直接通過getString()方法取得具體的信息。組件上傳代碼實(shí)例一html代碼
<html><head><title>commons-FileUpload組件上傳</title></head><body><form action="fileupload_demo01.jsp" method="post" enctype="mult

jsp代碼

<%@ page contentType="text/html" pageEncoding="GBK"%><%@ page import="java.util.*"%><%@ page import="org.apache.commons.fileupload.*"%><%@ page import="org.apache.commons.fileupload.disk.*"%><%@ page import="org.apache.commons.fileupload.servlet.*"%><html><head><title>commons-FileUpload組件上傳</title></head><body><%    DiskFileItemFactory factory = new DiskFileItemFactory() ;    ServletFileUpload upload= new ServletFileUpload(factory) ;    upload.setFileSizeMax(3 * 1024 * 1024) ;    // 只能上傳3M    List<FileItem> items = upload.parseRequest(request) ; // 接收全部?jī)?nèi)容    Iterator<FileItem> iter = items.iterator() ;    while(iter.hasNext()){        FileItem item = iter.next() ;        String fieldName = item.getFieldName() ;    // 取得表單控件的名稱%>        <ul><h4><%=fieldName%> --> <%=item.isFormField()%></h4><%        if(!item.isFormField()){        // 不是普通文本            String fileName = item.getName() ;    // 取得文件的名稱            String contentType = item.getContentType() ;    // 文件類型            long sizeInBytes = item.getSize() ;%>            <li>上傳文件名稱:<%=fileName%>            <li>上傳文件類型:<%=contentType%>            <li>上傳文件大小:<%=sizeInBytes%><%        } else {            String value = item.getString() ;%>            <li>普通參數(shù):<%=value%><%        }%>        </ul><%    }%></body></html>

組件上傳代碼實(shí)例二

<html><head><title>commons-FileUpload組件上傳</title></head><body><form action="fileupload_demo02.jsp" method="post" enctype="multipart/form-data">     姓名:<input type="text" name="uname"><br>    照片:<input type="file" name="pic1"><br>    照片:<input type="file" name="pic2"><br>    照片:<input type="file" name="pic3"><br>    <input type="submit" value="上傳">    <input type="reset" value="重置"></form></body></html>

JSP代碼

<%@ page contentType="text/html" pageEncoding="GBK"%><%@ page import="java.util.*,java.io.*"%><%@ page import="org.apache.commons.fileupload.*"%><%@ page import="org.apache.commons.fileupload.disk.*"%><%@ page import="org.apache.commons.fileupload.servlet.*"%><%@ page import="cn.mldn.lxh.util.*"%><html><head><title>commons-fileUpload組件上傳實(shí)例二</title></head><body><%    DiskFileItemFactory factory = new DiskFileItemFactory() ;    factory.setRepository(new File(this.getServletContext().getRealPath("/") + "uploadtemp")) ;        // 更準(zhǔn)確的說是一個(gè)臨時(shí)文件    ServletFileUpload upload = new ServletFileUpload(factory) ;    upload.setFileSizeMax(3 * 1024 * 1024) ;    // 只能上傳3M    List<FileItem> items = upload.parseRequest(request) ; // 接收全部?jī)?nèi)容    Iterator<FileItem> iter = items.iterator() ;    IPTimeStamp its = new IPTimeStamp(/* request.getRemoteAddr() */) ;    while(iter.hasNext()){        FileItem item = iter.next() ;        String fieldName = item.getFieldName() ;    // 取得表單控件的名稱%>        <ul><h4><%=fieldName%> --> <%=item.isFormField()%></h4><%        if(!item.isFormField()){        // 不是普通文本            File saveFile = null ;            InputStream input = null ;            OutputStream output = null ;            input = item.getInputStream() ;             output = new FileOutputStream(new File(this.getServletContext().getRealPath("/")+"upload"+File.separator+its.getIPTimeRand()+"."+item.getName().split("http://.")[1])) ;             int temp = 0 ;            byte data[] = new byte[512] ;            while((temp=input.read(data,0,512))!=-1){                output.write(data) ;    // 分塊保存            }            input.close() ;            output.close() ;          } else {            String value = item.getString() ;%>            <li>普通參數(shù):<%=value%><%        }%>        </ul><%    }%></body></html>

注意:本代碼在建立項(xiàng)目時(shí)需要在項(xiàng)目名稱下創(chuàng)建upload文件夾才能正常運(yùn)行。

FileUpload組件的不便之處:

  1. 無法像使用request.getParameter()方法那樣準(zhǔn)確的取得提交的參數(shù);
  2. 無法像使用request.getParameterValues()那樣準(zhǔn)確的取得一組提交參數(shù);
  3. 所有的上傳文件都需要進(jìn)行依次的判斷,才能夠分別保存,不能一次性批量保存。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 白水县| 义乌市| 江门市| 来宾市| 蓬莱市| 德江县| 扎赉特旗| 化州市| 宜章县| 依安县| 西宁市| 延安市| 治多县| 澜沧| 宜川县| 广水市| 长汀县| 巢湖市| 华安县| 宽甸| 分宜县| 怀柔区| 五华县| 绥江县| 读书| 黎川县| 昭平县| 桐柏县| 华宁县| 自治县| 额敏县| 开封市| 吴堡县| 西乌| 叶城县| 聂荣县| 察哈| 察哈| 河间市| 诸暨市| 平南县|