JSF: 1.2
Server: weblogic
I am trying to implement multiple file upload. Since I need to provide support for IE7, so I cannot use the HTML5 input file. Therefore, I planned to add a button, when I click it, it will add an input file to the page.
First of all, I started my work with ADF Faces. But that did not work. It behaves unpredictably.
I also tried the Tomahawk file upload component, but the problem was with this component, which after adding a new file upload from the backend of the previously created file upload fields became empty; not an instance UploadedFile. But this does not meet my requirements. Since I need to show the whole path in the file downloaders until the last button Submit is clicked.
Then I took advantage of apache file download.
I tried this with pure JSP and apache fileupload and it worked fine.
But I want to implement it using JSF with file-loading apaches, and when I tried to do this, it started to cause problems.
The jspx page is below:
<?xml version='1.0' encoding='utf-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<jsp:output omit-xml-declaration="true" doctype-root-element="HTML"
doctype-system="http://www.w3.org/TR/html4/loose.dtd"
doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"/>
<jsp:directive.page contentType="text/html;charset=utf-8"/>
<f:view>
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=utf-8"/>
<title>home</title>
</head>
<body>
<h:form enctype="multipart/form-data">
<input type="file" name="file"/>
<h:commandButton value="Upload" action="#{uploadBean.upload}" />
</h:form>
</body>
</html>
</f:view>
</jsp:root>
I created a filter because I cannot get the correct multi-page request from the event action.
Web.xml:
<?xml version = '1.0' encoding = 'windows-1252'?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5" xmlns="http://java.sun.com/xml/ns/javaee">
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>
<filter>
<filter-name>UploadFilter</filter-name>
<filter-class>com.edfx.massupload.filter.UploadFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>UploadFilter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/home.jspx</welcome-file>
</welcome-file-list>
</web-app>
UploadFilter:
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class UploadFilter implements Filter {
private FilterConfig _filterConfig = null;
public void init(FilterConfig filterConfig) throws ServletException {
_filterConfig = filterConfig;
}
public void destroy() {
_filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("Filter");
if (!(request instanceof HttpServletRequest)) {
chain.doFilter(request, response);
return;
}
HttpServletRequest httpRequest = (HttpServletRequest)request;
boolean isMultipartContent = ServletFileUpload.isMultipartContent(httpRequest);
if (!isMultipartContent) {
chain.doFilter(request, response);
return;
}
long maxFileSize = (1024 * 1024 * 1024);
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
upload.setSizeMax(maxFileSize);
upload.setFileSizeMax(maxFileSize);
try {
List<FileItem> items = upload.parseRequest(httpRequest);
System.out.println(items.size());
List<FileItem> files = new ArrayList<FileItem>();
for (FileItem item : items) {
if (!item.isFormField()) {
files.add(item);
}
}
httpRequest.setAttribute("files", files);
} catch (FileUploadException ex) {
ex.printStackTrace();
}
chain.doFilter(request, response);
}
}
, , bean:
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
public class UploadBean {
public UploadBean() {
super();
}
public String upload() {
System.out.println("====JYM");
HttpServletRequest httpRequest = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
System.out.println(httpRequest.getAttribute("files"));
return "";
}
}
- JavaScript jQuery, JSP, JSF.
enctype="multipart/form-data" h:form, , .
.