Apache commons fileupload文件上传实例讲解
程序员文章站
2024-03-12 11:13:20
文件上传的方法主要目前有两个常用的,一个是smartupload,一个是apache的commons fileupload.
我们这里主要介绍下第二个的用法,首先要上传文...
文件上传的方法主要目前有两个常用的,一个是smartupload,一个是apache的commons fileupload.
我们这里主要介绍下第二个的用法,首先要上传文件,注意几个问题:
1 form表单内,要添加空间<input type="file" name="myfile">
2 form表单的内容格式要定义成multipart/form-data格式
3 需要类库:1 commons-io.jar 2commons-fileupload-1.3.1.jar
接下来我们看下用法。
首先阅读apache commons fileupload的官方文档可以发现下面几个常用的函数:
1 创建文件解析对象
复制代码 代码如下:
diskfileupload diskfileupload = new diskfileupload();
2 进行文件解析后放在list中,因为这个类库支持多个文件上传,因此把结果会存在list中。
复制代码 代码如下:
list<fileitem> list = diskfileupload.parserequest(request);
3 获取上传文件,进行分析(不是必须)
复制代码 代码如下:
file remotefile = new file(new string(fileitem.getname().getbytes(),"utf-8"));
4 创建新对象,进行流拷贝
file1 = new file(this.getservletcontext().getrealpath("attachment"),remotefile.getname()); file1.getparentfile().mkdirs(); file1.createnewfile(); inputstream ins = fileitem.getinputstream(); outputstream ous = new fileoutputstream(file1); try{ byte[] buffer = new byte[1024]; int len = 0; while((len = ins.read(buffer)) > -1) ous.write(buffer,0,len); out.println("以保存文件"+file1.getabsolutepath()+"<br/>"); }finally{ ous.close(); ins.close(); }
这样我们就完成了文件的上传。
fileupload.html
<form action="servlet/uploadservlet" method="post" enctype="multipart/form-data"> <div align="center"> <fieldset style="width:80%"> <legend>上传文件</legend><br/> <div align="left">上传文件1</div> <div align="left"> <input type="file" name="file1"/> </div> <div align="left">上传文件2</div> <div align="left"> <input type="file" name="file2"/> </div> <div> <div align='left'>上传文件说明1</div> <div align='left'><input type="text" name="description1"/></div> </div> <div> <div align='left'>上传文件说明2</div> <div align='left'><input type="text" name="description2"/></div> </div> <div> <div align='left'> <input type='submit' value="上传文件"/> </div> </div> </fieldset> </div> </form>
web.xml
<servlet> <servlet-name>uploadservlet</servlet-name> <servlet-class>com.test.hello.uploadservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>uploadservlet</servlet-name> <url-pattern>/servlet/uploadservlet</url-pattern> </servlet-mapping>
uploadservlet.java
package com.test.hello; import java.io.file; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import java.io.printwriter; 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.diskfileupload; import org.apache.commons.fileupload.fileitem; import org.apache.commons.fileupload.fileuploadexception; public class uploadservlet extends httpservlet { /** * constructor of the object. */ public uploadservlet() { super(); } /** * destruction of the servlet. <br> */ public void destroy() { super.destroy(); // just puts "destroy" string in log // put your code here } /** * the doget method of the servlet. <br> * * this method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws servletexception if an error occurred * @throws ioexception if an error occurred */ public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcharacterencoding("utf-8"); response.getwriter().println("请以post方式上传文件"); } /** * the dopost method of the servlet. <br> * * this method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws servletexception if an error occurred * @throws ioexception if an error occurred */ @suppresswarnings({ "unchecked", "deprecation" }) public void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { file file1 = null,file2=null; string description1 = null,description2 = null; response.setcharacterencoding("utf-8"); request.setcharacterencoding("utf-8"); response.setcontenttype("text/html"); printwriter out = response.getwriter(); diskfileupload diskfileupload = new diskfileupload(); try{ list<fileitem> list = diskfileupload.parserequest(request); out.println("遍历所有的fileitem...<br/>"); for(fileitem fileitem : list){ if(fileitem.isformfield()){ if("description1".equals(fileitem.getfieldname())){ out.println("遍历到description1 ... <br/>"); description1 = new string(fileitem.getstring().getbytes(),"utf-8"); } if("description2".equals(fileitem.getfieldname())){ out.println("遍历到description2 ... <br/>"); description2 = new string(fileitem.getstring().getbytes(),"utf-8"); } }else{ if("file1".equals(fileitem.getfieldname())){ file remotefile = new file(new string(fileitem.getname().getbytes(),"utf-8")); out.println("遍历到file1...<br/>"); out.println("客户端文件位置:"+remotefile.getabsolutepath()+"<br/>"); file1 = new file(this.getservletcontext().getrealpath("attachment"),remotefile.getname()); file1.getparentfile().mkdirs(); file1.createnewfile(); inputstream ins = fileitem.getinputstream(); outputstream ous = new fileoutputstream(file1); try{ byte[] buffer = new byte[1024]; int len = 0; while((len = ins.read(buffer)) > -1) ous.write(buffer,0,len); out.println("以保存文件"+file1.getabsolutepath()+"<br/>"); }finally{ ous.close(); ins.close(); } } if("file2".equals(fileitem.getfieldname())){ file remotefile = new file(new string(fileitem.getname().getbytes(),"utf-8")); out.println("遍历到file2...<br/>"); out.println("客户端文件位置:"+remotefile.getabsolutepath()+"<br/>"); file2 = new file(this.getservletcontext().getrealpath("attachment"),remotefile.getname()); file2.getparentfile().mkdirs(); file2.createnewfile(); inputstream ins = fileitem.getinputstream(); outputstream ous = new fileoutputstream(file2); try{ byte[] buffer = new byte[1024]; int len = 0; while((len = ins.read(buffer)) > -1) ous.write(buffer,0,len); out.println("以保存文件"+file2.getabsolutepath()+"<br/>"); }finally{ ous.close(); ins.close(); } } } out.println("request 解析完毕<br/><br/>"); } }catch(fileuploadexception e){} out.println("<!doctype html public \"-//w3c//dtd html 4.01 transitional//en\">"); out.println("<html>"); out.println(" <head><title>a servlet</title></head>"); out.println(" <body>"); if(file1 != null){ out.println("<div>"); out.println(" <div align='left'>file1;</div>"); out.println(" <div align='left'><a href='"+request.getcontextpath()+"/attachment/"+ file1.getname()+"'target=_blank>"+file1.getname()+"</a>"); out.println("</div>"); out.println("</div>"); } if(file2 != null){ out.println("<div>"); out.println(" <div align='left'>file2;</div>"); out.println(" <div align='left'><a href='"+request.getcontextpath()+"/attachment/"+ file2.getname()+"'target=_blank>"+file2.getname()+"</a>"); out.println("</div>"); out.println("</div>"); } out.println("<div>"); out.println(" <div align='left'>description1:</div>"); out.println(" <div align='left'>"); out.println(description1); out.println("</div>"); out.println("</div>"); out.println("<div>"); out.println(" <div align='left'>description2:</div>"); out.println(" <div align='left'>"); out.println(description2); out.println("</div>"); out.println("</div>"); out.println(" </body>"); out.println("</html>"); out.flush(); out.close(); } /** * initialization of the servlet. <br> * * @throws servletexception if an error occurs */ public void init() throws servletexception { // put your code here } }
运行示例
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Java图片上传实现代码
下一篇: apache-superset环境搭建
推荐阅读
-
java组件commons-fileupload文件上传示例
-
Apache commons fileupload文件上传实例讲解
-
JavaWeb文件上传下载实例讲解(酷炫的文件上传技术)
-
php表单文件iframe异步上传实例讲解
-
php表单文件iframe异步上传实例讲解
-
JavaWeb文件上传下载实例讲解(酷炫的文件上传技术)
-
Apache Commons fileUpload实现文件上传之一
-
JAVA使用commos-fileupload实现文件上传与下载实例解析
-
Apache Commons fileUpload实现文件上传之一
-
JAVA使用commos-fileupload实现文件上传与下载实例解析