标签:
上一篇讲解了smartupload组件上传,那么这一篇我们讲解commons-FileUpload组件上传
<html> <head><title>commons-FileUpload组件上传</title></head> <body> <form action="fileupload_demo01.jsp" method="post" enctype="multipart/form-data"> 姓名:<input type="text" name="uname"><br> 照片:<input type="file" name="pic"><br> <input type="submit" value="上传"> <input type="reset" value="重置"> </form> </body> </html>
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) ; // 接收全部内容 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>普通参数:<%=value%> <% } %> </ul> <% } %> </body> </html>
组件上传代码实例二
<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组件上传实例二</title></head> <body> <% DiskFileItemFactory factory = new DiskFileItemFactory() ; factory.setRepository(new File(this.getServletContext().getRealPath("/") + "uploadtemp")) ; // 更准确的说是一个临时文件 ServletFileUpload upload = new ServletFileUpload(factory) ; upload.setFileSizeMax(3 * 1024 * 1024) ; // 只能上传3M List<FileItem> items = upload.parseRequest(request) ; // 接收全部内容 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("\\.")[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>普通参数:<%=value%> <% } %> </ul> <% } %> </body> </html>
注意:本代码在建立项目时需要在项目名称下创建upload文件夹才能正常运行。
FileUpload组件的不便之处:
java基础篇---文件上传(commons-FileUpload组件)
标签:
原文地址:http://www.cnblogs.com/oumyye/p/4237674.html