标签:file-upload jpa jsf java java-ee
我目前正在使用Tomahawk MyFaces将文件上传到我的服务器中.我一步一步地遵循了一些说明,一切似乎都很好,但是我没有将文件持久保存到数据库中.我没有看到任何错误,只是在我的Eclipse控制台中看到了一些警告.可以看看吗?这就是我所做的:
1-下载了用于JSF 2.0的战斧并将所有.jars添加到我的WEB-INF / lib文件夹中
2-我检查了我的web.xml是否已正确配置为使用Faces Servlet.我还为战斧扩展添加了一个过滤器,它看起来像这样:
<web-app version="3.0" 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">
<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>/pages/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>pages/index.jsp</welcome-file>
</welcome-file-list>
<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>
</web-app>
3-我还检查了我的faces-config.xml是否正确:
<?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">
4-我为上传小工具创建了一个非常简单的复合页面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:t="http://myfaces.apache.org/tomahawk">
<ui:composition template="WEB-INF/templates/BasicTemplate.xhtml">
<ui:define name="uploadForm">
<h:form>
<t:inputFileUpload value="#{uploadController.uploadedFile}" />
<h:commandButton value="submit" action="#{uploadController.submit}" />
<h:messages />
</h:form>
</ui:define>
</ui:composition>
</html>
5-我有一个托管bean与页面通讯并获取输入的文件:
@ManagedBean
@RequestScoped
public class UploadController {
@EJB
private IFileUploaderEJB fileUploaderEJB;
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?)
Garbage garbage = new Garbage();
garbage.setFilename(fileName);
garbage.setFile(bytes);
garbage.setDescription("info about the file");
garbage.setFileType("File extension");
fileUploaderEJB.uploadGarbage(garbage);
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;
}}
注意:托管bean调用了一个EJB,该EJB应该持久保存到文件
6-允许访问数据库的EJB:
@Stateless(name = "ejbs/FileUploaderEJB")
public class FileUploaderEJB implements IFileUploaderEJB {
@PersistenceContext
private EntityManager em;
public Garbage uploadGarbage(Garbage garbage) {
Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
garbage.setUploadDate(dateFormat.format(date));
//...
em.persist(garbage);
return garbage;
}
7-最后,我有一个使用JPA批注的Entity,它使用@Lob将文件存储在数据库中:
@Entity
public class Garbage {
@Id
@GeneratedValue
@Column(nullable = false)
private Long id;
@Column(nullable = false)
private String filename;
@Column(nullable = false)
private String fileType;
@Column(nullable = false)
private String uploadDate;
@Column(nullable = false)
private String destroyDate;
@Lob
@Column(nullable = false)
private byte[] file;
@Column(nullable = false)
private String description;
//Getters and Setters...
问题是3:
P1-当我使用浏览按钮选择一个文件,然后单击“提交”时,没有任何反应.我在控制台中看不到任何输入查询,没有新行添加到数据库.我想念什么?
P2-每当我对源代码进行更改并再次发布控制台时,其构建时间将比平时更长(将近40秒),并且它向我显示许多类似这样的警告(或多或少20条):
WARNING: JSF1029: Application is versioned at 2.0 (either explicitly by the version of /WEB-INF/faces-config.xml or the lack of a /WEB-INF/faces-confg.xml), however class ‘org.ajax4jsf.taglib.html.facelets.AjaxSupportHandler’ depends on a legacy facelet class. The facelet artifact represented by this class will not be registered.
jar文件或faces-config.xml中的配置是否有问题?
P3-当我使用浏览器导航到项目中的任何页面时,将出现如下警告:
WARNING: PWC4011: Unable to set request character encoding to UTF-8 from context /Datapool, because request parameters have already been read, or ServletRequest.getReader() has already been called
我认为这与web.xml中的过滤器有关
解决方法:
When i select a file with the browse button and then i click submit nothing happens.
您的< h:form>缺少enctype =“ multipart / form-data”属性.在这里查看迷你教程:JSF 2.0 file upload with Tomahawk’s <t:inputFileUpload>
.
WARNING: JSF1029: Application is versioned at 2.0 (either explicitly by the version of /WEB-INF/faces-config.xml or the lack of a /WEB-INF/faces-confg.xml), however class ‘org.ajax4jsf.taglib.html.facelets.AjaxSupportHandler’ depends on a legacy facelet class. The facelet artifact represented by this class will not be registered.
您的Web项目中仍然有一些针对JSF 1.2的RichFaces / A4J库.如果不需要,请摆脱它们.
WARNING: PWC4011: Unable to set request character encoding to UTF-8 from context /Datapool, because request parameters have already been read, or ServletRequest.getReader() has already been called
这是特定于Glassfish的. Glassfish的默认值为ISO-8859-1,需要重新配置为使用UTF-8.另请参见How to get rid of WARNING: PWC4011: Unable to set request character encoding to UTF-8
标签:file-upload,jpa,jsf,java,java-ee 来源: https://codeday.me/bug/20191102/1994101.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。