使用存储在数据库jsf中的路径上传文件

时间:2014-05-20 09:39:13

标签: jsf jpa upload

我使用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;
        }

    }

我的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();

    }
}

最后是y 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>

感谢。

1 个答案:

答案 0 :(得分:0)

我在DocDAO课程中没有看到任何f.setChemin(chemin)。如果路径未在您的实体中的其他位置设置,则可能是问题。