apache commons-fileupload-1.2 组件下载地址:
http://jakarta.apache.org/commons/fileupload/,同时该组件需要Commons IO的支持,下载地址:
http://jakarta.apache.org/commons/io/
--------------------------------------------------------------------
用来处理上传文件的Upload.java(Servlet )
--------------------------------------------------------------------
package common;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.File;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
public final class Upload extends HttpServlet {
private final String savepath = new String ("E://upload//");
public Upload() {
super();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{
try {
request.setCharacterEncoding("gb2312");
if(ServletFileUpload.isMultipartContent(request)){
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
if (item.isFormField()) {
System.out.println("Form field " + name + " with value "
+ Streams.asString(stream) + " detected.");
} else {
String filename = new File(item.getName()).getName();
System.out.println("File field " + name + " with file name "
+ filename + " detected.");
BufferedInputStream bis = new BufferedInputStream(stream);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(savepath + filename)));
Streams.copy(bis, bos, true);
}
}
response.getWriter().write("<p>succeed</p>");
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
--------------------------------------------------------------------
测试用的HTML网页
--------------------------------------------------------------------
<%@ page language="java" contentType="text/html; charset=gb2312"%>
<html>
<head>
<title>upload Test</title>
</head>
<body>
<form name="form1" enctype="multipart/form-data" method="post" action="upload.html">
<label>Test
<input type="text" name="text1">
</label>
<br>
<label>File
<input type="file" name="file">
</label>
<input type="submit" name="Submit" value="upload">
</form>
</body>
</html>
--------------------------------------------------------------------
配置web.xml
--------------------------------------------------------------------
<?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>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>Upload</servlet-name>
<servlet-class>common.Upload</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Upload</servlet-name>
<url-pattern>/upload.html</url-pattern>
</servlet-mapping>
</web-app>
--------------------------------------------------------------------
用来处理上传文件的Upload.java(Servlet )
--------------------------------------------------------------------
package common;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.File;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
public final class Upload extends HttpServlet {
private final String savepath = new String ("E://upload//");
public Upload() {
super();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{
try {
request.setCharacterEncoding("gb2312");
if(ServletFileUpload.isMultipartContent(request)){
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
if (item.isFormField()) {
System.out.println("Form field " + name + " with value "
+ Streams.asString(stream) + " detected.");
} else {
String filename = new File(item.getName()).getName();
System.out.println("File field " + name + " with file name "
+ filename + " detected.");
BufferedInputStream bis = new BufferedInputStream(stream);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(savepath + filename)));
Streams.copy(bis, bos, true);
}
}
response.getWriter().write("<p>succeed</p>");
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
--------------------------------------------------------------------
测试用的HTML网页
--------------------------------------------------------------------
<%@ page language="java" contentType="text/html; charset=gb2312"%>
<html>
<head>
<title>upload Test</title>
</head>
<body>
<form name="form1" enctype="multipart/form-data" method="post" action="upload.html">
<label>Test
<input type="text" name="text1">
</label>
<br>
<label>File
<input type="file" name="file">
</label>
<input type="submit" name="Submit" value="upload">
</form>
</body>
</html>
--------------------------------------------------------------------
配置web.xml
--------------------------------------------------------------------
<?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>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>Upload</servlet-name>
<servlet-class>common.Upload</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Upload</servlet-name>
<url-pattern>/upload.html</url-pattern>
</servlet-mapping>
</web-app>