Apache Commons fileUpload文件上传多个示例分享
本文通过实例来介绍如何使用commons-fileupload.jar,apache的commons-fileupload.jar可方便的实现文件的上传功能,具体内容如下
将apache的commons-fileupload.jar放在应用程序的web-inf\lib下,即可使用。下面举例介绍如何使用它的文件上传功能。
所使用的fileupload版本为1.2,环境为eclipse3.3+myeclipse6.0。fileupload 是基于 commons io的,所以在进入项目前先确定commons io的jar包(本文使用commons-io-1.3.2.jar)在web-inf\lib下。
此文作示例工程可在文章最后的附件中下载。
示例1
最简单的例子,通过servletfileupload静态类来解析request,工厂类fileitemfactory会对mulipart类的表单中的所有字段进行处理,不只是file字段。getname()得到文件名,getstring()得到表单数据内容,isformfield()可判断是否为普通的表单项。
demo1.html
<html> <head> <meta http-equiv="content-type" content="text/html; charset=gb18030"> <title>file upload</title> </head> <body> //必须是multipart的表单数据。 <form name="myform" action="demo1.jsp" method="post" enctype="multipart/form-data"> your name: <br> <input type="text" name="name" size="15"><br> file:<br> <input type="file" name="myfile"><br> <br> <input type="submit" name="submit" value="commit"> </form> </body> </html>
demo1.jsp
<%@ page language="java" contenttype="text/html; charset=gb18030" pageencoding="gb18030"%> <%@ page import="org.apache.commons.fileupload.*"%> <%@ page import="org.apache.commons.fileupload.servlet.*"%> <%@ page import="org.apache.commons.fileupload.disk.*"%> <%@ page import="java.util.*"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <% boolean ismultipart = servletfileupload.ismultipartcontent(request);//检查输入请求是否为multipart表单数据。 if (ismultipart == true) { fileitemfactory factory = new diskfileitemfactory();//为该请求创建一个diskfileitemfactory对象,通过它来解析请求。执行解析后,所有的表单项目都保存在一个list中。 servletfileupload upload = new servletfileupload(factory); list<fileitem> items = upload.parserequest(request); iterator<fileitem> itr = items.iterator(); while (itr.hasnext()) { fileitem item = (fileitem) itr.next(); //检查当前项目是普通表单项目还是上传文件。 if (item.isformfield()) {//如果是普通表单项目,显示表单内容。 string fieldname = item.getfieldname(); if (fieldname.equals("name")) //对应demo1.html中type="text" name="name" out.print("the field name is" + item.getstring());//显示表单内容。 out.print("<br>"); } else {//如果是上传文件,显示文件名。 out.print("the upload file name is" + item.getname()); out.print("<br>"); } } } else { out.print("the enctype must be multipart/form-data"); } %> <html> <head> <meta http-equiv="content-type" content="text/html; charset=gb18030"> <title>file upload</title> </head> <body> </body> </html>
结果:
the field name isjeff
the upload file name isd:\c语言考试样题\作业题.rar
示例2
上传两个文件到指定的目录。
demo2.html
<html> <head> <meta http-equiv="content-type" content="text/html; charset=gb18030"> <title>file upload</title> </head> <body> <form name="myform" action="demo2.jsp" method="post" enctype="multipart/form-data"> file1:<br> <input type="file" name="myfile"><br> file2:<br> <input type="file" name="myfile"><br> <br> <input type="submit" name="submit" value="commit"> </form> </body> </html>
demo2.jsp
<%@ page language="java" contenttype="text/html; charset=gb18030" pageencoding="gb18030"%> <%@ page import="org.apache.commons.fileupload.*"%> <%@ page import="org.apache.commons.fileupload.servlet.*"%> <%@ page import="org.apache.commons.fileupload.disk.*"%> <%@ page import="java.util.*"%> <%@ page import="java.io.*"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <%string uploadpath="d:\\temp"; boolean ismultipart = servletfileupload.ismultipartcontent(request); if(ismultipart==true){ try{ fileitemfactory factory = new diskfileitemfactory(); servletfileupload upload = new servletfileupload(factory); list<fileitem> items = upload.parserequest(request);//得到所有的文件 iterator<fileitem> itr = items.iterator(); while(itr.hasnext()){//依次处理每个文件 fileitem item=(fileitem)itr.next(); string filename=item.getname();//获得文件名,包括路径 if(filename!=null){ file fullfile=new file(item.getname()); file savedfile=new file(uploadpath,fullfile.getname()); item.write(savedfile); } } out.print("upload succeed"); } catch(exception e){ e.printstacktrace(); } } else{ out.println("the enctype must be multipart/form-data"); } %> <html> <head> <meta http-equiv="content-type" content="text/html; charset=gb18030"> <title>file upload</title> </head> <body> </body> </html>
结果:
upload succeed
此时,在"d:\temp"下可以看到你上传的两个文件。
示例3
上传一个文件到指定的目录,并限定文件大小。
demo3.html
<html> <head> <meta http-equiv="content-type" content="text/html; charset=gb18030"> <title>file upload</title> </head> <body> <form name="myform" action="demo3.jsp" method="post" enctype="multipart/form-data"> file:<br> <input type="file" name="myfile"><br> <br> <input type="submit" name="submit" value="commit"> </form> </body> </html>
demo3.jsp
<%@ page language="java" contenttype="text/html; charset=gb18030" pageencoding="gb18030"%> <%@ page import="org.apache.commons.fileupload.*"%> <%@ page import="org.apache.commons.fileupload.servlet.*"%> <%@ page import="org.apache.commons.fileupload.disk.*"%> <%@ page import="java.util.*"%> <%@ page import="java.io.*"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <% file uploadpath = new file("d:\\temp");//上传文件目录 if (!uploadpath.exists()) { uploadpath.mkdirs(); } // 临时文件目录 file temppathfile = new file("d:\\temp\\buffer\\"); if (!temppathfile.exists()) { temppathfile.mkdirs(); } try { // create a factory for disk-based file items diskfileitemfactory factory = new diskfileitemfactory(); // set factory constraints factory.setsizethreshold(4096); // 设置缓冲区大小,这里是4kb factory.setrepository(temppathfile);//设置缓冲区目录 // create a new file upload handler servletfileupload upload = new servletfileupload(factory); // set overall request size constraint upload.setsizemax(4194304); // 设置最大文件尺寸,这里是4mb list<fileitem> items = upload.parserequest(request);//得到所有的文件 iterator<fileitem> i = items.iterator(); while (i.hasnext()) { fileitem fi = (fileitem) i.next(); string filename = fi.getname(); if (filename != null) { file fullfile = new file(fi.getname()); file savedfile = new file(uploadpath, fullfile .getname()); fi.write(savedfile); } } out.print("upload succeed"); } catch (exception e) { e.printstacktrace(); } %> <html> <head> <meta http-equiv="content-type" content="text/html; charset=gb18030"> <title>file upload</title> </head> <body> </body> </html>
示例4
利用servlet来实现文件上传。
upload.java
package com.zj.sample; import java.io.file; import java.io.ioexception; import java.util.iterator; import java.util.list; 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.disk.diskfileitemfactory; import org.apache.commons.fileupload.servlet.servletfileupload; @suppresswarnings("serial") public class upload extends httpservlet { private string uploadpath = "d:\\temp"; // 上传文件的目录 private string temppath = "d:\\temp\\buffer\\"; // 临时文件目录 file temppathfile; @suppresswarnings("unchecked") public void dopost(httpservletrequest request, httpservletresponse response) throws ioexception, servletexception { try { // create a factory for disk-based file items diskfileitemfactory factory = new diskfileitemfactory(); // set factory constraints factory.setsizethreshold(4096); // 设置缓冲区大小,这里是4kb factory.setrepository(temppathfile);// 设置缓冲区目录 // create a new file upload handler servletfileupload upload = new servletfileupload(factory); // set overall request size constraint upload.setsizemax(4194304); // 设置最大文件尺寸,这里是4mb list<fileitem> items = upload.parserequest(request);// 得到所有的文件 iterator<fileitem> i = items.iterator(); while (i.hasnext()) { fileitem fi = (fileitem) i.next(); string filename = fi.getname(); if (filename != null) { file fullfile = new file(fi.getname()); file savedfile = new file(uploadpath, fullfile.getname()); fi.write(savedfile); } } system.out.print("upload succeed"); } catch (exception e) { // 可以跳转出错页面 e.printstacktrace(); } } public void init() throws servletexception { file uploadfile = new file(uploadpath); if (!uploadfile.exists()) { uploadfile.mkdirs(); } file temppathfile = new file(temppath); if (!temppathfile.exists()) { temppathfile.mkdirs(); } } }
demo4.html
<html> <head> <meta http-equiv="content-type" content="text/html; charset=gb18030"> <title>file upload</title> </head> <body> // action="fileupload"对应web.xml中<servlet-mapping>中<url-pattern>的设置. <form name="myform" action="fileupload" method="post" enctype="multipart/form-data"> file:<br> <input type="file" name="myfile"><br> <br> <input type="submit" name="submit" value="commit"> </form> </body> </html>
web.xml
<servlet> <servlet-name>upload</servlet-name> <servlet-class>com.zj.sample.upload</servlet-class> </servlet> <servlet-mapping> <servlet-name>upload</servlet-name> <url-pattern>/fileupload</url-pattern> </servlet-mapping>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Object.assign() 函数与...state函数
下一篇: php 猴子摘桃的算法
推荐阅读
-
Apache Commons fileUpload文件上传多个示例分享
-
java组件commons-fileupload文件上传示例
-
Apache commons fileupload文件上传实例讲解
-
Apache Commons fileUpload实现文件上传之一
-
Apache Commons fileUpload实现文件上传之一
-
jquery uploadify和apache Fileupload实现异步上传文件示例
-
jquery uploadify和apache Fileupload实现异步上传文件示例
-
使用apache.commons.fileupload等进行文件上传
-
使用apache.commons.fileupload组件来进行文件上传
-
apache commons fileupload 组件上传文件