source : http://www.javaworld.com.tw/jute/post/view?bid=38&id=182676
web.xml
<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>File Upload Servlet</servlet-name>
<servlet-class>catalog.view.util.FileUploadServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- File Upload Servlet Mapping -->
<servlet-mapping>
<servlet-name>File Upload Servlet</servlet-name>
<url-pattern>*.file</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
faces-config.xml
<?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN" "http://java.sun.com/dtd/web-facesconfig_1_0.dtd">
<faces-config>
<navigation-rule>
<from-view-id>/uploadResult1.jsp</from-view-id>
<navigation-case>
<description>
</description>
<from-outcome>success</from-outcome>
<to-view-id>/index.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
FileUploadServlet.java
package catalog.view.util;
import java.util.List;
import java.util.Iterator;
import java.io.File;
//
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
public class FileUploadServlet extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
this.doPost(req, res);
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
//this.logger.debug("upload image");
try {
String temp=getServletContext().getRealPath("/")+"temp";
String loadpath=getServletContext().getRealPath("/")+"dir";
DiskFileUpload fu = new DiskFileUpload();
fu.setSizeMax(5*1024*1024);
fu.setSizeThreshold(4096);
fu.setRepositoryPath(temp);
List fileItems = fu.parseRequest(req);
Iterator iter = fileItems.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
String name = item.getName();
name=name.substring(name.lastIndexOf("\\")+1);
// out.println(name);
long size = item.getSize();
if((name==null||name.equals("")) && size==0)
continue;
// out.println(item.getName()+" Size="+item.getSize()+"<br>");
File fNew= new File("C:\\"+name);
item.write(fNew);
}
}
req.getRequestDispatcher("/uploadResult.jsp").forward(req, res);
} catch (Exception e) {
//this.logger.error("Could not upload file.", e);
throw new ServletException("Could not upload file." + e.toString());
}
}
}
index.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
<html>
<head>
<title>Page of upload</title>
<link rel="stylesheet" type="text/css" href="styles.css"/>
</head>
<body>
<f:view>
<form enctype="multipart/form-data" method="POST" action="upload.file">
<table align="center" class="box">
<tr><td style="padding:20">
<table>
<tr>
<td align="left">
<h:outputText value="choose the file you want to upload" />
</td>
</tr>
<tr>
<td align="center">
<input type="file" name="uploadFile"/>
</td>
</tr>
<tr>
<td align="center">
<input type="submit" value="Upload"/>
</td>
</tr>
</table>
</td></tr>
</table>
</form>
</f:view>
</body>
</html>
upLoadResult.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
<html>
<head>
<title>page of upload Success</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css"/>
</head>
<body>
<f:view>
<table align="center" class="box">
<tr><td style="padding:20">
<table>
<tr>
<td>
<h:outputText value="Success!"/>
<h:outputText value="File Name:"/>
<h:outputText value="#{FileUploadServlet.fu.name}"/>
</td>
</tr>
<tr>
<td align="center">
<h:form id="uploadImageResultForm">
<h:commandButton value="Back" action="success"/>
</h:form>
</td>
</tr>
</table>
</td></tr>
</table>
</f:view>
</body>
</html>
沒有留言:
張貼留言