JSF 2.0file upload

我正在浏览一些博客,试图find如何使用JSF 2.0上传文件,但所有的解决scheme都让我困惑。 我想知道什么是我需要能够成功上传文件(MP3,PDF,video…什么types),并将其作为@Lob存储在数据库中。 这是我迄今为止所做的:

  • 我创build了一个具有types为byte []的属性的实体,并且还使用@Lob注释进行了注释。

  • 我创build了一个EJB,它将引入带有一个byte []作为参数的方法的实体,并使用EntityManager类(persist方法)将其插入到数据库中。

  • 我创build了一个types为“file”的input标签和一个提交button的JSF页面

  • 我准备了一个托pipebean来交换有关该文件与JSF页面的信息。

现在我被卡住了,我有很多怀疑:

  • 我应该怎么做才能将文件从JSF传递到托pipebean,然后将其转换为字节[](为了能够处理它到EJB)?

  • servlet如何帮助我?

  • 我需要一个servlet来做到这一点吗?

  • 另外我发现在一些博客中提到了关于servlet 3.0的一些问题,但我不知道我的工作环境是否正在使用它,如何使用servlets 3.0(我正在使用JEE6)呢?

我从来没有做过file upload,我也不是很熟悉servlets。 我很困惑,有人可以给我一些启动提示,请好吗?

首先,这个(旧)问题和答案假设JSF 2.0 / 2.1。 由于JSF 2.2有一个本地的<h:inputFile>组件,不需要第三方组件库。 另请参见如何使用JSF 2.2 <h:inputFile>上载文件? 保存的文件在哪里?


最简单的方法是使用战斧为JSF 2.0 。 它提供了一个<t:inputFileUpload>组件。

下面是一个一步一步的教程:

  • 为Servlet 3.0和JSF 2.0创build一个空白的dynamicWeb项目。 web.xml必须符合Servlet 3.0规范,并且已经包含JSF servlet:

     <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" 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_3_0.xsd" id="YourProjectName" version="3.0"> <display-name>Your Project Name</display-name> <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>*.xhtml</url-pattern> </servlet-mapping> </web-app> 

    faces-config.xml必须符合JSF 2.0规范:

     <?xml version="1.0" encoding="UTF-8"?> <faces-config xmlns="http://java.sun.com/xml/ns/javaee" 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-facesconfig_2_0.xsd" version="2.0"> </faces-config> 

  • 为JSF 2.0下载Tomahawk 1.1.10 。 解压zip文件,进入/lib文件夹,将所有*.jar文件复制到/WEB-INF/lib

    它是18个文件,其中单独使用t:inputFileUpload组件不需要batik*.jarxml*.jar 。 你可以离开他们。


  • web.xmlconfigurationTomahawk扩展filter。 这是负责处理需要能够通过HTTP发送文件的multipart/form-data请求的人。

     <filter> <filter-name>MyFacesExtensionsFilter</filter-name> <filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class> </filter> <filter-mapping> <filter-name>MyFacesExtensionsFilter</filter-name> <servlet-name>Faces Servlet</servlet-name> </filter-mapping> 

    请注意, <servlet-name>必须与您在web.xml中定义的FacesServlet的确切<servlet-name>匹配。


  • 创build一个简单的Facelet, upload.xhtml

     <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:t="http://myfaces.apache.org/tomahawk" xmlns:ui="http://java.sun.com/jsf/facelets"> <h:head> <title>Tomahawk file upload demo</title> </h:head> <h:body> <h:form enctype="multipart/form-data"> <t:inputFileUpload value="#{bean.uploadedFile}" /> <h:commandButton value="submit" action="#{bean.submit}" /> <h:messages /> </h:form> </h:body> </html> 

    请注意<h:form>上的enctype="multipart/form-data"属性,这对于能够使用HTTP发送文件非常重要。


  • 创build一个简单的托pipebean, com.example.Bean

     package com.example; import java.io.IOException; import javax.faces.application.FacesMessage; import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; import javax.faces.context.FacesContext; import org.apache.commons.io.FilenameUtils; import org.apache.myfaces.custom.fileupload.UploadedFile; @ManagedBean @RequestScoped public class Bean { private UploadedFile uploadedFile; public void submit() throws IOException { String fileName = FilenameUtils.getName(uploadedFile.getName()); String contentType = uploadedFile.getContentType(); byte[] bytes = uploadedFile.getBytes(); // Now you can save bytes in DB (and also content type?) FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(String.format("File '%s' of type '%s' successfully uploaded!", fileName, contentType))); } public UploadedFile getUploadedFile() { return uploadedFile; } public void setUploadedFile(UploadedFile uploadedFile) { this.uploadedFile = uploadedFile; } } 

应该是这样的。 通过http:// localhost:8080 / projectname / upload.xhtml打开它。

至于你的具体问题:

我应该怎么做才能将文件从JSF传递到托pipebean,然后将其转换为字节[](为了能够把它处理到EJB)?

这在上面得到了回答。

servlet如何帮助我?

它能够处理和控制HTTP请求/响应。 在JSF环境中, FacesServlet已经完成了所有的工作。

我需要一个servlet来做到这一点?

在JSF环境中, FacesServlet是强制性的。 但它已经由API提供了,你不需要自己写一个。 但是,为了能够从数据库中下载文件,另一个servlet是绝对有用的。 你可以在这里find一个基本的例子: 服务静态内容的Servlet 。

另外我发现在一些博客中提到了关于servlet 3.0的一些问题,但是我不知道我的工作环境是否正在使用它,如果我正在使用servlets 3.0(我正在使用JEE6)呢?

如果您使用的是Glassfish 3,JBoss AS 6,Tomcat 7等Servlet 3.0容器,并且web.xml声明为Servlet 3.0,那么您肯定使用Servlet 3.0。 Servlet 3.0是Java EE 6的一部分。

为了完整起见,我只想提供一个function完整的自包含的示例,说明如何使用JSF 2.2完成此操作,无论是使用非Ajax请求还是使用Ajax请求 。 请记住,JSF 2.2使用不同的名称空间,您需要使用Servlet 3.0容器 (如Tomcat 7.0.x,JBoss AS 6.x和7.x以及GlassFish 3.x)。

fileUpload.xhtml

 <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:f="http://xmlns.jcp.org/jsf/core"> <h:head /> <h:body> <h:form enctype="multipart/form-data"> <h:inputFile value="#{uploadBean.file}" /> <h:commandButton value="Post Upload" action="#{uploadBean.upload}" /> </h:form> <h:form enctype="multipart/form-data"> <h:inputFile value="#{uploadBean.file}" /> <h:commandButton value="Ajax Upload"> <f:ajax listener="#{uploadBean.upload}" execute="@form" render="countOutput" /> </h:commandButton> <!-- Counts the uploaded items --> <h:outputText id="countOutput" value="Files uploaded #{uploadBean.filesUploaded}" /> </h:form> </h:body> </html> 

UploadBean.java:

 @ManagedBean @ViewScoped public class UploadBean { private int filesUploaded = 0; //javax.servlet.http.Part (Servlet 3.0 API) private Part file; private String fileContent; /** * Just prints out file content */ public void upload() { try { fileContent = new Scanner(file.getInputStream()) .useDelimiter("\\A").next(); System.out.println(fileContent + " uploaded"); filesUploaded++; } catch (IOException e) { e.printStackTrace(); } } public int getFilesUploaded() { return filesUploaded; } public Part getFile() { return file; } public void setFile(Part file) { this.file = file; } } 

也可以看看:

  • JSF 2.2:使用h:inputFile进行file upload

我build议使用像Tomahawk的<t:inputFileUpload>或PrimeFaces <p:fileUpload>这样的压缩库。

BalusC还有一个关于使用JSF 2.0和Servlet 3.0上传文件的好博客文章。

BalusC的博客文章: 使用JSF 2.0和Servlet 3.0上传文件是救了我的,因为我用Spring WebFlow运行RichFaces 4 fileUpload标记时遇到了问题。

修改BalusC的代码以使用Spring的MultipartResolver是值得的 – 您不需要从另一篇博客文章中获得他的MultipartMap

我通过像这样修改FileRendererdecode方法来实现它:

  UploadedFile ret = null; Object req = context.getExternalContext().getRequest(); if (req instanceof MultipartHttpServletRequest) { MultipartFile file = ((MultipartHttpServletRequest)req).getFile(clientId); File temp = null; try { temp = File.createTempFile("_UPLOAD_", null); file.transferTo(temp); String name = new File(file.getOriginalFilename()).getName(); ret = new UploadedFile(temp, name); } catch (IOException e) { throw new RuntimeException("Could not create temp file.", e); } } else { throw new IllegalStateException("Request is not multipart. Use spring's multipart resolver."); } // If no file is specified, set empty String to trigger validators. ((UIInput) component).setSubmittedValue( ret == null ? EMPTY_STRING : ret); 

UploadedFile是一个简单的可序列化的POJO,用于将结果返回到后台bean。

在JSF 2.2中,您可以使用标签轻松上传文件,而无需使用commons-io或filter。 这个标签支持normal和ajax进程。

正常:

  <h:inputFile id="file" value="#{fileUploadBean.uploadedFile}"/> <h:commandButton id="button" action="#{fileUploadBean.sumbit()}" value="Upload"/> 

阿贾克斯:

  <h:inputFile id="file" value="#{fileUploadBean.uploadedFile}"/> <h:commandButton id="button" value="submit"> <f:ajax execute="@all" render="@all" onevent="statusUpdate"/> </h:commandButton> 

devise你的托pipebean如下:

  @Named @RequestScoped public class FileUploadBean { private Part uploadedFile; } 

最简单的方法可能是使用在MyFaces中可以find的inputFileUpload标签:

http://myfaces.apache.org/

IceFaces2.0有一个, http: //wiki.icefaces.org/display/ICE/FileEntry还没有尝试实现它,但下载了示例应用程序,它在Tomcat 6(servlet 2.5,所以不是JEE6)下工作,

您必须在我们的项目构buildpath中添加commons-fileupload-1.2.1.jar

1.configurationweb.xml文件

 Web.xml <filter> <filter-name>PrimeFaces FileUpload Filter</filter-name> <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class> </filter> <filter-mapping> <filter-name>PrimeFaces FileUpload Filter</filter-name> <servlet-name>Faces Servlet</servlet-name> </filter-mapping> <mime-mapping> <extension>png</extension> <mime-type>image/png</mime-type> </mime-mapping> 

2.创buildManagedBean

  @ManagedBean @SessionScoped public class FileUploadBean implements Serializable{ public FileUpload (){ } private StreamedContent file; public void loadFile(FileUploadEvent event) throws IOException, InterruptedException { InputStream input = new ByteArrayInputStream(event.getFile().getContents()); file= new DefaultStreamedContent(input, "image/jpg"); } } 

3.jsf文件(xhtml)

  <h:form enctype="multipart/form-data"> <p:fileUpload fileUploadListener="#{fileUploadBean.file}" sizeLimit="100000" allowTypes="/(\.|\/)(gif|jpe?g|png|bmp)$/"/> </h:form>