在JSP环境中利用Commons-fileupload组件实现文件上传
一.在JSP环境中利用Commons-fileupload组件实现文件上传
1.页面upload.jsp清单如下:
<%@
page language= "java" import = "java.util.*" pageEncoding= "UTF-8" %>
<!DOCTYPE
HTML PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN" >
<html>
<head>
<title>The
FileUpload Demo</title> </head>
<body>
<form
action= "UploadFile" method= "post" enctype= "multipart/form-data" >
<p><input
type= "text" name= "fileinfo" value= "" >文件介绍</p>
<p><input
type= "file" name= "myfile" value= "浏览文件" ></p>
<p><input
type= "submit" value= "上
传" ></p>
</form>
</body>
</html>
|
注意:在上传表单中,既有普通文本域也有文件上传域
2.FileUplaodServlet.java清单如下:
package org.chris.fileupload;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class FileUplaodServlet
extends HttpServlet
{ protected void doGet(HttpServletRequest
request, HttpServletResponse response) throws ServletException,
IOException { doPost(request,
response); }
protected void doPost(HttpServletRequest
request, HttpServletResponse response) throws ServletException,
IOException { request.setCharacterEncoding( "UTF-8" );
//文件的上传部分
boolean isMultipart
= ServletFileUpload.isMultipartContent(request); if (isMultipart)
{
try {
FileItemFactory
factory = new DiskFileItemFactory();
ServletFileUpload
fileload = new ServletFileUpload(factory);
//
设置最大文件尺寸,这里是4MB fileload.setSizeMax( 4194304 );
List<FileItem>
files = fileload.parseRequest(request); Iterator<FileItem>
iterator = files.iterator(); while (iterator.hasNext())
{
FileItem
item = iterator.next(); if (item.isFormField())
{
String
name = item.getFieldName(); String
value = item.getString(); System.out.println( "表单域名为:
" +
name + "值为:
" +
value); } else {
//获得获得文件名,此文件名包括路径
String
filename = item.getName(); if (filename
!= null )
{
File
file = new File(filename);
//如果此文件存在
if (file.exists()){
File
filetoserver = new File( "d:\\upload\\" ,file.getName());
item.write(filetoserver);
System.out.println( "文件
" +
filetoserver.getName() + "
上传成功!!" );
}
}
}
}
}
catch (Exception
e) { System.out.println(e.getStackTrace());
}
}
}
}
|
3.web.xml清单如下:
Java代码 <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>UploadFileServlet</servlet-name> <servlet-class> org.chris.fileupload.FileUplaodServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>UploadFileServlet</servlet-name> <url-pattern>/UploadFile</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/Index.jsp</welcome-file> </welcome-file-list> </web-app>
版权声明:本文为博主原创文章,未经博主允许不得转载。