【问题标题】:FileUpload: Get the full path of the CSV fileFileUpload:获取 CSV 文件的完整路径
【发布时间】:2018-10-24 13:59:40
【问题描述】:

我正在处理一个项目,或者我想上传一个 Excel CSV 文件,其中包含使用查询 LOAD DATA LOCAL 的数据库中的表中的数据,我使用文件的完整路径测试查询它的路径(例如C: //../file.csv)直接在查询中它没有问题,我想使用库primefaces p: fileUpload,当我从我的桌面或我的电脑上的另一个目录中选择一个文件CSV时,它只返回名称我选择的文件而不是命中的完整路径我有一个错误:

org.hibernate.exception.GenericJDBCException: could not execute statement
java.io.FileNotFoundException: extraction1.csv (The specified file can not be found)

强制,因为没有带有文件夹名称的文件的完整路径,我想要的是从我选择的位置返回文件的名称及其根文件,以便我的请求可以正确执行,如在下面的代码中所示,我希望在我选择时文件所在的路径也与相关文件的名称一起返回,谢谢。

prelevServ.importToDB("C:\\Users\\helyoubi\\Desktop\\Japon 2\\"+fichierUpload.getFileName());

我的 JSF 表单:

<h:form enctype="multipart/form-data">

    <p:growl id="messages" showDetail="true" />

    <p:fileUpload label="Choisir" value="#{importFichier.fichierUpload}" mode="simple" skinSimple="true"/>

    <p:separator/>

    <p:commandButton value="Envoyer" ajax="false" action="#importFichier.importation}" />

</h:form>

我的 managedBean 公式:

    @ManagedBean
public class ImportFichier implements Serializable{
     
    /**
     *
     */
    private static final long serialVersionUID = 1L;
 
    private UploadedFile fichierUpload;
     
    private PrelevementServices prelevServ = new PrelevementServicesImpl();
     
 
     
    public UploadedFile getFichierUpload() {
        return fichierUpload;
    }
 
 
 
 
    public void setFichierUpload(UploadedFile fichierUpload) {
        this.fichierUpload = fichierUpload;
    }
 
 
 
 
    public void importation() {
 
        if(fichierUpload.getFileName()!= null) {
         
            //prelevServ.importToDB("C:\\Users\\helyoubi\\Desktop\\Japon 2\\"+fichierUpload.getFileName());
             
            prelevServ.importToDB(fichierUpload.getFileName());
             
            FacesMessage message = new FacesMessage("Succesful", fichierUpload.getFileName()+ " is uploaded.");
             
            FacesContext.getCurrentInstance().addMessage(null, message);
             
        }else {
             
            FacesMessage message = new FacesMessage("Le chemin du fichier : "+fichierUpload.getFileName()+" est introuvable");
             
            FacesContext.getCurrentInstance().addMessage(null, message);
             
        }
         
     
        System.out.println("CSV added to your the DB Table");
         
    }
     
     
 
}

我的要求:

@Override
    public void importToDB(String cheminFichier) {
         
        session.beginTransaction();
         
        session.createSQLQuery("LOAD DATA LOCAL INFILE :filename INTO TABLE Prelevement_saisons FIELDS TERMINATED BY ',' ENCLOSED BY '\"'(espece,saison,departement,commune,code,attributions,realisations)").setString("filename", cheminFichier).executeUpdate();
         
        session.getTransaction().commit();
         
    }

【问题讨论】:

  • 我很确定它只是存储在一个临时位置(至少对于较大的文件而言)所以基本上在您的 fileUploadEvent 方法中,您需要将文件输入流写入特定的服务器上的文件夹(已经存在!),然后您将知道可以从数据库导入中加载它的路径。如果您没有如何处理事件和保存文件的示例,我可能会为您挖掘一些旧代码
  • 是的,如果你有文件输入流的脚本,它会帮助我thanx

标签: sql jsf jakarta-ee primefaces managed-bean


【解决方案1】:

尝试发送 .getPath():

prelevServ.importToDB(fichierUpload.getPath());

【讨论】:

    猜你喜欢
    • 2010-11-10
    • 2013-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-31
    • 2012-10-19
    • 2015-11-03
    相关资源
    最近更新 更多