我使用jsf 2上传文件,首先我将文件上传到系统目录中,然后尝试将路径与其他信息存储到数据库中,我的卡住是,当提交我上传文件成功时,找到它在正确的位置,找到其他信息如描述,文件名…在数据库中,但找不到路径。这是我管理的bean:
package mbeans;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import daoImpl.DocDAO;
import entities.Document;
@ManagedBean
@RequestScoped
public class DocBean {
public DocDAO docDAO;
private Part file;
private String titreDocument;
private String descriptionDocument;
private String lien;
private String dateMise;
private String Categorie;
private String chemin;
private String sousCategorie;
private Integer idDocument;
public DocBean(){
docDAO=new DocDAO();
}
public String upload() throws IOException{
file.write(getFilename(file));
docDAO.createDoc(idDocument, titreDocument, descriptionDocument, sousCategorie, Categorie, chemin, dateMise, lien);
idDocument=null;
titreDocument="";
descriptionDocument="";
lien="";
sousCategorie="";
dateMise="";
Categorie="";
chemin +="C:\data\" ;
try{
File afile =new File("C:\Users\free\Desktop\glassfish-4.0\glassfish4\glassfish\domains\domain1\generated\jsp\copy2mavenTest1111\"+ getFilename(file));
if(afile.renameTo(new File("C:\data\" +getFilename(file) ))){
System.out.println("File is moved successful!");
}else{
System.out.println("File is failed to move!");
}
}catch(Exception e){
e.printStackTrace();
}
return"succes";
}
private static String getFilename(Part part){
for(String cd: part.getHeader("Content-Disposition").split(";")){
if(cd.trim().startsWith("filename")){
String filename=cd.substring(cd.indexOf('=')+1).trim().replace(""", "");
return filename.substring(filename.lastIndexOf('/')+1).substring(filename.lastIndexOf('\')+1);
}
}
return null;
}
public List<Document> getAlldoc(){
return docDAO.getAllDoc();
}
public String getSousCategorie() {
return sousCategorie;
}
public void setSousCategorie(String sousCategorie) {
this.sousCategorie = sousCategorie;
}
public String getTitreDocument() {
return titreDocument;
}
public void setTitreDocument(String titreDocument) {
this.titreDocument = titreDocument;
}
public String getDescriptionDocument() {
return descriptionDocument;
}
public void setDescriptionDocument(String descriptionDocument) {
this.descriptionDocument = descriptionDocument;
}
public String getDateMise() {
return dateMise;
}
public Integer getIdDocument() {
return idDocument;
}
public void setIdDocument(Integer idDocument) {
this.idDocument = idDocument;
}
public void setDateMise(String dateMise) {
this.dateMise = dateMise;
}
public String getCategorie() {
return Categorie;
}
public void setCategorie(String categorie) {
Categorie = categorie;
}
public String getLien() {
return lien;
}
public void setLien(String lien) {
this.lien = lien;
}
public Part getFile() {
return file;
}
public void setFile(Part file) {
this.file = file;
}
public String getChemin() {
return chemin;
}
public void setChemin(String chemin) {
this.chemin = chemin;
}
}
my dao,用于在数据库中存储有关文件的信息:
package daoImpl;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import entities.Document;
@Stateless
public class DocDAO {
private static EntityManager em;
private static EntityManagerFactory factory;
public DocDAO() {
if(factory==null)
factory=Persistence.createEntityManagerFactory("mavenTest");
if(em==null)
em=factory.createEntityManager();
}
public List<Document> getAllDoc() {
List<Document> listeDoc;
Query q;
em.getTransaction().begin();
q=em.createQuery("select d from Document d ORDER BY d.dateMise");
listeDoc = q.getResultList();
em.getTransaction().commit();
return listeDoc ;
}
public void createDoc(Integer idDocument, String titreDocument,
String descriptionDocument, String sousCategorie
, String Categorie, String chemin, String dateMise, String lien) {
em.getTransaction().begin();
Document f =new Document();
f.setIdDocument(idDocument);
f.setDateMise(dateMise);
f.setDescriptionDocument(descriptionDocument);
f.setLien(lien);
f.setTitreDocument(titreDocument);
f.setCategorie(Categorie);
f.setSousCategorie(sousCategorie);
em.persist(f);
em.getTransaction().commit();
}
}
最后是XHTML页面:
<!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">
<head></head>
<body>
<h3>ajouter un nouveau document </h3><br/>
<h:form enctype="multipart/form-data" id="form1">
<h:outputText value="Nom du document"> </h:outputText>
<h:inputText value="#{docBean.titreDocument}"></h:inputText><br/>
<h:outputText value="description"></h:outputText>
<h:inputText value="#{docBean.descriptionDocument}"></h:inputText><br/>
<h:outputText value="chemin"></h:outputText>
<h:inputHidden value="#{docBean.chemin}"></h:inputHidden><br/>
<h:outputText value="Date de la mise "></h:outputText>
<h:inputText value="#{docBean.dateMise}"></h:inputText><br/>
<h:outputText value="Lien"></h:outputText>
<h:inputText value="#{docBean.lien}"></h:inputText><br/>
<h:outputText value="Catégorie"></h:outputText>
<h:inputText value="#{docBean.categorie}"></h:inputText><br/>
<h:outputText value="sous catégorie"> </h:outputText>
<h:inputText value="#{docBean.sousCategorie}"></h:inputText><br/>
<h:outputText> télécharger le document</h:outputText>
<h:inputFile value="#{docBean.file}"></h:inputFile><br/>
<h:commandButton value="valider" action="#{docBean.upload}"/>
</h:form>
</body>
</html>
谢谢。
我在DocDAO类中没有看到任何f.setChemin(chemin)。这可能是问题,如果路径没有设置在您的实体的其他地方。