Page 1 of 2 12 LastLast
Results 1 to 20 of 38
Like Tree1Likes

Thread: Name and Path of the file in JSF

  1. #1
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default Name and Path of the file in JSF

    Hello,

    i want to write a jsf code to get a site where i can able to browse the files in my computer and choose on of them (upload)
    File Upload is Easy in JSF2.2 | Ramki Java Blog
    jsf - PrimeFaces fileUpload showing the file name after upload - Stack Overflow

    But acutely what i need is to return the path and return of the uploaded file
    1) method to return the name of the uploaded file
    2) method to return the path of the uploaded file

    how to do it?

  2. #2
    Join Date
    Apr 2014
    Posts
    46
    Rep Power
    0

    Default Re: Name and Path of the file in JSF

    You can see where the file gets uploaded from this blog Sblog - File upload example in JSF 2.x

  3. #3
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default Re: Name and Path of the file in JSF

    I don't have the correct answer, especially how to get path of uploaded file

    what i got, the path, where files will be uploaded (destination path) not the source path

    Any Hints??

  4. #4
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Name and Path of the file in JSF

    So you do not want to get the path of the file on the server. You want to get the path that the file had on the client.

    My understandig of the http protocol is, that this is not possible. All that is transfered is a filename (must not be the name of the file on the computer. There even must not be a file at all!) and the data/content.

    You might want to read other threads about this e.g.
    How to get full path of selected file on change of <input type=‘file’> using javascript, jquery-ajax? - Stack Overflow

    So you need some other technology (e.g. applets or silverlight) but clients have to trust your code so it gets the required rights and that is often simply not done (it is a real security issue!)

    Konrad
    gimbal2 likes this.

  5. #5
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default Re: Name and Path of the file in JSF

    Again, i want the path of the file

    this file is on the SERVER not on the client

    Actually, i want to get the path and the name of the uploaded file on server to store the path and the name on Data base

    But right now i don't know how to get the path of the uploaded file.

    All what i want by uploading the file just get the name and path of the file, not moving the file to another path.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Name and Path of the file in JSF

    Can you get the path from the code that wrote the uploaded file onto the server's disk?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Name and Path of the file in JSF

    How do you write the files? when you simply open the stream with the filename, then you could also do these steps:
    - create a new File instance with the filename.
    - get the full path with getAbsoluteFile()
    - use the file instance to open the stream

    But I would even do it different:
    - have a configuration setting where the files should be stored
    - build the full path from config setting and filename (maybe with subdirectories)
    - only store a path relative to a config directory inside the database

    Storing an absolute path can be critical, because you might want to move stuff. Or maybe you have multiple systems and files are stored in different locations on different systems. (Just imagine an active-active database cluster over data centers. Files are on a Storage system that is also replicated. So the source can be different...

    (So try to choose designs that do not limit yourself.)

    Konrad

  8. #8
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default Re: Name and Path of the file in JSF

    Quote Originally Posted by Norm View Post
    Can you get the path from the code that wrote the uploaded file onto the server's disk?
    I have the path of the destination where the file should be uploaded but not the source location of the file

  9. #9
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Name and Path of the file in JSF

    Quote Originally Posted by vector_ever View Post
    I have the path of the destination where the file should be uploaded but not the source location of the file
    So you want to get the source location and not the location on the server? Then please refer to reply #4 which I wrote in which I tried to explain you that there is no way for a html frontend to provide this information.

    Konrad

  10. #10
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default Re: Name and Path of the file in JSF

    Again, i meant with source location the location of the File on the server.

    I tried to follow yours instruction in reply #7 but still hard to make it
    some how i don't understand it fully

    Do you have example declare how to get get the path of file on the server?

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Name and Path of the file in JSF

    The code that wrote the file on the server should have the path to the file that it wrote.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Name and Path of the file in JSF

    Hi,

    if you follow your first link to File Upload is Easy in JSF2.2 | Ramki Java Blog

    The code to open the stream was:
    FileOutputStream outputStream = new FileOutputStream(getFilename(file1));

    My approach would be to have it like this:
    File outFile = new File(getFilename(file1));
    FileOutputStream outputStream = new FileOutputStream(outFile);
    That way you have a File object so you could call outFile.getAbsoluteFile();

    But you could also build a full path yourself. So imagine that you have a String fileDirectory (which value is read from a configuration file or something like that).
    Then you could have this code:
    File outFile = new File(fileDirectory + File.separator + getFilename(file1));
    FileOutputStream outputStream = new FileOutputStream(outFile);

    That way you define where the files should be stored and it is not the current working directory or something like that.

    With kind regards,

    Konrad

  13. #13
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default Re: Name and Path of the file in JSF

    Hi,

    I do exactly what you suggest, so..
    Java Code:
        private String getpath(Part part) {
            String path = "";
            FileOutputStream outputStream = null;
            try {
                File outFile = new File(getFilename(file));
                outputStream = new FileOutputStream(outFile);
    
                path = outFile.getAbsoluteFile().toString();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Hochladen.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    outputStream.close();
                } catch (IOException ex) {
                    Logger.getLogger(Hochladen.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            return path;
        }
    but this method return always another path (always i got: /home/ralmoued/Downloads/apache-tomcat-7.0.53/bin/) what ever the original path is
    always the same output.

    So what is missing?
    Last edited by vector_ever; 05-14-2014 at 06:49 PM.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Name and Path of the file in JSF

    Can you print out the values of these two items:
    file and getFilename(file))

    What file(s) are written to the folder:
    /home/ralmoued/Downloads/apache-tomcat-7.0.53/bin/
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default Re: Name and Path of the file in JSF

    Quote Originally Posted by Norm View Post
    Can you print out the values of these two items:
    file and getFilename(file))
    i can just print the value of getFilename(file), it will give the name of the uploaded file, and there is no problem with it

    Quote Originally Posted by Norm View Post
    What file(s) are written to the folder:
    /home/ralmoued/Downloads/apache-tomcat-7.0.53/bin/
    actually no file will be uploaded to these path, and i have no idea why i got every time the same path

    Any way i will post the whole Code, maybe it will me more clear

    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.Serializable;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.SessionScoped;
    import javax.servlet.http.Part;
    public class Hochladen implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        private Connection con;
        private Part file;
        private String message;
    
        /**
         * Initialisiert Mysql-Verbindung
         */
        public Hochladen() {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbase", "root", "root");
            } catch (SQLException ex) {
                Logger.getLogger(Hochladen.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Hochladen.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    
        /**
         * Setter und Getter fr part
         *
         * @return file
         */
        public Part getFile() {
            return file;
        }
    
        public void setFile(Part file) {
            this.file = file;
        }
    
        /**
         * Setter und Getter fr Message (Status des Hochladen)
         *
         * @return message
         */
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    
        /**
         * Fgt pdf Dateiname und Pfad in der Datnbank ein
         */
        public void upload() {
            boolean fileSuccess = false;
            if (file.getSize() > 0) {
                try (Statement stmt = con.createStatement();) {
    
                    //Zeilenzhler
                    ResultSet result = stmt.executeQuery("SELECT COUNT(*) FROM  dbase.benutzerhandbuch");
                    result.next();
                    //Berechnung Anzahl der Zeilen
                    int rowCount = result.getInt(1);
    
                    //falls die Datenbank leer ist dann(INSERT), ansonst (UPDATE)
                    String sql;
                    if (rowCount > 0) {
                        sql = "Delete From dbase.benutzerhandbuch";
                        stmt.executeUpdate(sql);
                    }
                    sql = "INSERT INTO dbase.benutzerhandbuch(Dateiname, Pfad) "
                            + "VALUES('" + getFilename(file) + "','" + getpath() + "')";
    
                    stmt.executeUpdate(sql);
    
                    System.out.println("file name and path added Successfully");
                    fileSuccess = true;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (fileSuccess) {
                System.out.println("File uploaded successfully");
                setMessage("Datei erfolgreich hochgeladen");
            } else {
                setMessage("Fehler, whlen Sie mindestens eine Datei!");
            }
        }
    
        /**
         * Ldt pdf Dteiname aus DB
         *
         * @param part
         * @return
         */
        private static String getFilename(Part part) {
            String filename = "";
            for (String cd : part.getHeader("content-disposition").split(";")) {
                if (cd.trim().startsWith("filename")) {
                    String name = cd.substring(cd.indexOf('=') + 1).trim().replace("\"", "");
                    filename = name.substring(name.lastIndexOf('/') + 1).substring(name.lastIndexOf('\\') + 1); // MSIE fix.
                }
            }
            return filename;
        }
    
        /**
         * Ldt pdf Pfad aus DB
         *
         * @param part
         * @return
         */
        private String getpath() {
            String path = "";
            FileOutputStream outputStream = null;
            try {
                File outFile = new File(getFilename(file));
                outputStream = new FileOutputStream(outFile);
    
                path = outFile.getAbsolutePath();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Hochladen.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    outputStream.close();
                } catch (IOException ex) {
                    Logger.getLogger(Hochladen.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            return path;
        }
    }

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Name and Path of the file in JSF

    no file will be uploaded to these path
    Strange. When I execute that code in post#13 (slightly modified) a file is created by the FileOutputStream class. It is in the folder pointed to by the value in the path variable.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default Re: Name and Path of the file in JSF

    Quote Originally Posted by Norm View Post
    Strange. When I execute that code in post#13 (slightly modified) a file is created by the FileOutputStream class. It is in the folder pointed to by the value in the path variable.
    I uses Ubunto Linux not windows
    Is that make any difference?

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Name and Path of the file in JSF

    It shouldn't
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default Re: Name and Path of the file in JSF

    Quote Originally Posted by Norm View Post
    It shouldn't
    Did you try the code in post#15?

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Name and Path of the file in JSF

    No, I don't have the environment to test it in.

    Why does getPath() use the FileOutputStream class which will create a file?

    Where is the code that creates and writes the file on the server?
    Last edited by Norm; 05-14-2014 at 08:32 PM.
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. File Path
    By novice in forum New To Java
    Replies: 12
    Last Post: 08-04-2011, 03:25 PM
  2. file path
    By billq in forum New To Java
    Replies: 2
    Last Post: 04-11-2010, 05:47 PM
  3. ant with a jar file in path to run
    By javastuden in forum Advanced Java
    Replies: 0
    Last Post: 02-22-2010, 05:59 AM
  4. file path
    By ras_pari in forum Advanced Java
    Replies: 2
    Last Post: 10-12-2009, 11:25 AM
  5. file path
    By Arsench in forum New To Java
    Replies: 7
    Last Post: 10-30-2008, 05:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •