Page 2 of 2 FirstFirst 12
Results 21 to 38 of 38
Like Tree1Likes

Thread: Name and Path of the file in JSF

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

    Default Re: Name and Path of the file in JSF

    Quote Originally Posted by Norm View Post
    Why does getPath() use the FileOutputStream class which will create a file?
    that war recommended from Konrad, see Post#12

    Quote Originally Posted by Norm View Post
    Where is the code that creates and writes the file on the server?
    There is no such a code to write the file on the server.
    I just get the name and path of the file and will pass them to to the client to get the file

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

    Default Re: Name and Path of the file in JSF

    There is no such a code to write the file on the server.
    If the file is not on the server, then there will NOT be a path to the file since it does not exist.
    This discussion is going in circles.

    You need to explain with more detail.
    Where does the file exist?
    Where are you trying to obtain the path to the file?
    Where are you saving the path to the file?
    Where are you trying to use the path to the file?
    What are you using the path to the file for?
    If you don't understand my response, don't ignore it, ask a question.

  3. #23
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    448
    Rep Power
    1

    Default Re: Name and Path of the file in JSF

    Hmm. I always thought you were uploading the file to the server. I understood that you used a file upload control so the client sends the content to the server. Doing nothing with that content at least seems strange.

    And I just gave a modification to the code in your first link which was saving the file to the disk.

    Konrad

  4. #24
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    179
    Rep Power
    2

    Default Re: Name and Path of the file in JSF

    Quote Originally Posted by Norm View Post
    Where does the file exist?
    The file is on the server, I use Tomcat 7 as server and the file on the computer where i use Tomcat 7 as server

    Quote Originally Posted by Norm View Post
    Where are you trying to obtain the path to the file?
    ??

    Quote Originally Posted by Norm View Post
    Where are you saving the path to the file?
    In mysql data base...both the name and path of the file will be saved in Data base to be called later

    Quote Originally Posted by Norm View Post
    Where are you trying to use the path to the file?
    I have another Java class where client can call the file from

    Quote Originally Posted by Norm View Post
    What are you using the path to the file for?
    to be able to call (download) the file, the client need the name of file + path of file
    I already have the method to get the name of file but still missing how to get the path on the server

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

    Default Re: Name and Path of the file in JSF

    If the file is on the server and not on the client's PC, how will the server know the name of the file and its location?
    Is the file placed on the server before the server starts executing?

    Are you asking: How can the server get the path to a file that is on the same machine as the server is executing on?
    If you don't understand my response, don't ignore it, ask a question.

  6. #26
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    179
    Rep Power
    2

    Default Re: Name and Path of the file in JSF

    Quote Originally Posted by Norm View Post
    If the file is on the server and not on the client's PC, how will the server know the name of the file and its location?
    Why should it be on the client PC?

    Quote Originally Posted by Norm View Post
    Is the file placed on the server before the server starts executing?
    yes it is

    Quote Originally Posted by Norm View Post
    Are you asking: How can the server get the path to a file that is on the same machine as the server is executing on?
    yes

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

    Default Re: Name and Path of the file in JSF

    Can you create a folder and put all the files in that folder?
    Then you need a way for some part of the server to find the path to that folder and make it available for any program on the server that needs it.
    I don't know how to do that. One way would be to hardcode the path to the folder with the files. But that is usually a poor solution. Are there tomcat configuration files where the path can be set?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Name and Path of the file in JSF

    I will post some additional codes to be more clear

    1) Server side
    Code on Post#15

    and JSF code
    Java Code:
    <?xml version='1.0' encoding='UTF-8' ?>
    <!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:h="http://java.sun.com/jsf/html">
        <h:head>
        </h:head>
        <h:body>
            Bitte wählen Sie das Benutzerhandbuch
            <p>
                <h:outputText value="#{hochladen.message}"
                              rendered="#{!empty hochladen.message}" />
    
                <h:form  enctype="multipart/form-data">
                    <h:panelGrid>
                        <h:inputFile value="#{hochladen.file}" /><br/>
                        <h:commandButton value="Hochladen" action="#{hochladen.upload()}"/>
                    </h:panelGrid>
                </h:form>
            </p>
        </h:body>
    </html>
    2) client side
    Java Code:
    <?xml version='1.0' encoding='UTF-8' ?>
    <!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:h="http://java.sun.com/jsf/html">
        <h:head>
        </h:head>
        <h:body>
            Benutzerhandbuch
            
            <h:form>
                <h:commandLink value="Download PDF" action="#{pdf.downloadPDF()}" target="_blank" />
                            
    </h:form>
        </h:body>
    </html>
    Java Code:
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.Closeable;
    import java.io.File;
    import java.io.FileInputStream;
    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.ViewScoped;
    
    import javax.faces.context.ExternalContext;
    import javax.faces.context.FacesContext;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Diese Klasse zeigt das Handbenutzerbuch in Webseite an.
     *
     * @author ralmoued
     */
    @ManagedBean(name = "pdf")
    @ViewScoped
    public class DisplayPdf implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        // Constants ----------------------------------------------------------------------------------
        private Connection con;
        private String filename;
        private String pfad;
        private int id;
    
        private static final int DEFAULT_BUFFER_SIZE = 10240; // 10KB.
    
            /**
         * Initialisiert Mysql-Verbindung
         */
        public DisplayPdf() {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbase", "root", "root");
            } catch (SQLException ex) {
                Logger.getLogger(Woerterbuch.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Woerterbuch.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    
        /**
         * Setters und getters
         * @return 
         */
        public String getFilename() {
            return filename;
        }
    
        public void setFilename(String filename) {
            this.filename = filename;
        }
    
        public String getPfad() {
            return pfad;
        }
    
        public void setPfad(String pfad) {
            this.pfad = pfad;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        /**
         * Lädt die pdf Dateiname aus DB
         * @return 
         */
        public String getFileName() {
    
            try (Statement stmt = con.createStatement();) {
                String sql = "SELECT Dateiname FROM dbase.benutzerhandbuch";
                ResultSet result = stmt.executeQuery(sql);
    
                while (result.next()) {
                    filename = result.getString("Dateiname");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return filename;
        }
    
         /**
         * Lädt die pdf Pfad aus DB
         * @return 
         */
        public String getFilePath() {
    
            try (Statement stmt = con.createStatement();) {
                String sql = "SELECT Pfad FROM dbase.benutzerhandbuch";
                ResultSet result = stmt.executeQuery(sql);
    
                while (result.next()) {
                    pfad = result.getString("Pfad");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return pfad;
        }
    
        /**
         * Zeigt die Pdf Datei auf Webseite an
         * @throws IOException 
         */
        public void downloadPDF() throws IOException {
    
            //Vorbereiten.
            FacesContext facesContext = FacesContext.getCurrentInstance();
            ExternalContext externalContext = facesContext.getExternalContext();
            HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
    
            File file;
            file = new File(getFilePath(), getFileName());
            BufferedInputStream input = null;
            BufferedOutputStream output = null;
    
            try {
                // Öffnen die Datei.
                input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
    
                // Init servlet response.
                response.reset();
                response.setHeader("Content-Type", "application/pdf");
                response.setHeader("Content-Length", String.valueOf(file.length()));
                response.setHeader("Content-Disposition", "inline; filename=\"" + getFileName() + "\"");
                output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);
    
                // Schreiben Dateiinhalte zu response.
                byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
                int length;
                while ((length = input.read(buffer)) > 0) {
                    output.write(buffer, 0, length);
                }
    
                // Finalisieren Aufgabe.
                output.flush();
            } finally {
                // close streams.
                close(output);
                close(input);
            }
    
            // Informieren JSF, dass es kein Ursache gibt, um response zu behandeln.
            // Dies ist sehr wichtig, sonst wird die folgende Ausnahme in den Protokollen zu erhalten:
            // java.lang.IllegalStateException: Cannot forward after response has been committed.
            facesContext.responseComplete();
        }
    
        /**
         * 
         * @param resource 
         */
        private static void close(Closeable resource) {
            if (resource != null) {
                try {
                    resource.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
    }

  9. #29
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    179
    Rep Power
    2

    Default Re: Name and Path of the file in JSF

    Quote Originally Posted by Norm View Post
    Can you create a folder and put all the files in that folder?
    Then you need a way for some part of the server to find the path to that folder and make it available for any program on the server that needs it.
    I don't know how to do that. One way would be to hardcode the path to the folder with the files. But that is usually a poor solution. Are there tomcat configuration files where the path can be set?
    I had that i idea but find it really poor
    I don't know too much with tomcat configuration files, i use the default tomacat server without any changes

  10. #30
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    448
    Rep Power
    1

    Default Re: Name and Path of the file in JSF

    If the file is already on the server: why do you have a page with a file upload control?

    That is already completly wrong.

    Maybe you want to build pages that shows the filesystem of the server instead? Then you could move around and select files.

    Konrad

  11. #31
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    179
    Rep Power
    2

    Default Re: Name and Path of the file in JSF

    Quote Originally Posted by kneitzel View Post
    If the file is already on the server: why do you have a page with a file upload control?
    As explained, I have pdf file and i want to let the client to be able to display this pdf on Web page.
    So my solution was, select the wanted file and store the parameters (path and name) on Data base (from Sever side)
    and from clients side, they will be able to call this file and display it on the web page (the client will send action to server which contain the command to download pdf with corrected name and path on the server (which stored in the data base).

    Quote Originally Posted by kneitzel View Post
    That is already completly wrong.
    Why it totally wrong??

    Quote Originally Posted by kneitzel View Post
    Maybe you want to build pages that shows the filesystem of the server instead? Then you could move around and select files.
    Can you explain more?
    Because i thought that is my solution !1

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

    Default Re: Name and Path of the file in JSF

    Hi,

    The inputFile control is a control that has only one purpose: upload a local file that the user has selected to the server. if I understood you correctly, you do not want to upload anything so this control seems to be a bad choice.

    You can build a page where you simply show the content of a directory. Each element is a link and clicking it will either show the choosen directory or download the file.

    The big difference between these 2 ideas is:
    The first one is used to upload files that are on the client to the server.
    The other idea offers data that is on the server (and it depends on your code whag should happen. Files could be downloaded or something could be done at the server...

    Konrad

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

    Default Re: Name and Path of the file in JSF

    Quote Originally Posted by kneitzel View Post
    The inputFile control is a control that has only one purpose: upload a local file that the user has selected to the server. if I understood you correctly, you do not want to upload anything so this control seems to be a bad choice.
    Yes you understood correctly, i have a button to let me able to browse on the the server and select a file, but after that no uploading will be done..just get the parameters of this selected file(path and name)

    Quote Originally Posted by kneitzel View Post
    You can build a page where you simply show the content of a directory. Each element is a link and clicking it will either show the choosen directory or download the file.
    I don't understand this idea completely, how can display the the directory of chosen file of this method?

    Quote Originally Posted by kneitzel View Post
    The big difference between these 2 ideas is:
    The first one is used to upload files that are on the client to the server.
    there is no need to upload any thing from the client in my recently problem

    Quote Originally Posted by kneitzel View Post
    The other idea offers data that is on the server (and it depends on your code whag should happen. Files could be downloaded or something could be done at the server...
    That is my purpose and here i have the problem, i have the data on the server and i want to offer them to the clients

    Do you still thing that my method is totally wrong?
    Last edited by vector_ever; 05-15-2014 at 11:28 AM.

  14. #34
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    448
    Rep Power
    1

    Default Re: Name and Path of the file in JSF

    Quote Originally Posted by vector_ever View Post
    Yes you understood correctly, i have a button to let me able to browse on the the server and select a file, but after that no uploading will be done..just get the parameters of this selected file(path and name)
    How could such a control display files on the server? For that the client has to know the available files on the server. Maybe you want to look for some debugging tools to have a closer look what is sent between client and server. Or read about the HTTP specification. Or is HTML the missing point? I am just wondering which part is missing so I could tell you more.

    The file upload control is just an input control of type file. And the browser offers a file chooser so the user can select a file. When the form is submitted, the file content is submitted to the server!

    Quote Originally Posted by vector_ever View Post
    I don't understand this idea completely, how can display the the directory of chosen file of this method?
    There is no directory of a choosen file. When you check the data that is sent to the server then you will see that there is just ANY filename and then the content of the file that is sent to the server. And not more!
    A client could even sent data that is not a file to the server.

    So right now I really have a problem to understand all your setup. A common thing is, that you have all data inside the database and then you just create the content of your files from your database. But if I remember correctly you still have to fill the database?

    I see 2 approaches:
    a) Just fill the data manually. Do not create a frontend for this task if it is a one time job.

    b) Forget that the data is already on the server. You also use your server as client right now, so just see the current data as data on the client. Now build your frontend that allows you to add new data and modify existing data. So you could create a new dataset and upload the file. Then you simply store the file where you think that it should be stored. (But maybe you should even think about your complete setup. Right now it seems that you extracted the tomcat download inside your Downloads directory and then used it there. I have everything in ~/bin and because of the big amount of stuff that I control myself I even have ~/bin/java so my tomcat installation is at ~/bin/java/apache-tomcat-7.0.52. But if I would run a real application, then I would have a proper installation. Old fashioned way is something like /opt/<myapp>. You could also use /usr/local/ but then it is recommend to split everything up to ./bin ./lib e.t.c. which is quite hard in my eyes. But ok, that does not really matter.)
    This is the approach that I would take. That way you can run the server somewhere else and you can maintain the data through a simple web frontend.
    (Which does not mean that you do not import existing data into your server.)

    Quote Originally Posted by vector_ever View Post
    Do you still thing that my method is totally wrong?
    As long as you try to use the file upload control for other things than uploading files: Yes, I think so.
    (But of course. I could be wrong, too. Maybe you have an idea that could work, too and I am just unable to understand it.)

    Konrad

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

    Default Re: Name and Path of the file in JSF

    Quote Originally Posted by kneitzel View Post
    The file upload control is just an input control of type file. And the browser offers a file chooser so the user can select a file. When the form is submitted, the file content is submitted to the server!
    The user (client) must not submit or upload any thing in the server...The idea the administrator must provide pdf file (user guide) which should be called from the clients.
    Now the administrator (on the server) select the file what ever browsing or not but it is the job of administrator on the server...the clients must just read the file which offered by the administrator on the server.


    Quote Originally Posted by kneitzel View Post
    I see 2 approaches:
    a) Just fill the data manually. Do not create a frontend for this task if it is a one time job.
    it is poor solution, the problem with it, that i am just the developer not the administrator, the administrator may be have no idea with dealing with the Database


    Quote Originally Posted by kneitzel View Post
    As long as you try to use the file upload control for other things than uploading files: Yes, I think so.
    (But of course. I could be wrong, too. Maybe you have an idea that could work, too and I am just unable to understand it.)
    I thing i can use Upload control to upload file or to do any thing with it (delete, copy, cut, rename...) as long as i have the proper method to do that.

    Did you tried to run my code?
    maybe still a point missing make misunderstanding..
    May be if you run the whole code it will be easy to understand my point

  16. #36
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    448
    Rep Power
    1

    Default Re: Name and Path of the file in JSF

    Quote Originally Posted by vector_ever View Post
    I thing i can use Upload control to upload file or to do any thing with it (delete, copy, cut, rename...) as long as i have the proper method to do that.
    You know that I am talking about the core html control which is displayed at the client / the browser. The
    Java Code:
    <input type="file" ... >
    thing. Which is generated in your JSF page when you have
    Java Code:
    <h:inputFile value="#{hochladen.file}" />
    So now you are even down to "download one file that the administrator defines"? So just put a setting in a config file that the administrator can change. And then provide a file download (e.g. on a simple button click).

    As long as you try to use an upload control for other things, I cannot help you at all. Sorry.

    Konrad

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

    Default Re: Name and Path of the file in JSF

    Ok, Konrad i give up, i think your solution is the best for now...

    I change the code so the
    XML Code:
    <h:inputFile value="#{hochladen.file}"/>
    let the administrator to brows and select one file
    XML Code:
     <h:commandButton value="Hochladen" action="#{hochladen.upload()}"/>
    will upload the selected file to a new folder

    With this approach i still can define the name of the selected file, but the path i will set it manually...
    For now every thing is ok... but i have two problems:

    1) How to set the path of the new folder (where i must upload the file)
    hence i use Linux and the path is : /home/ralmoued/Downloads/Benutzerhandbuch/

    this path will work perfectly on my Pc but what about another?? "ralmoued" is the name of user of my computer and ofcource it will not be the same on another server. Also how it will be if the Server will be Windows OS which have another method to name of the path??
    Any way i need the help to set a path which will be always valid.

    2)
    put a setting in a config file that the administrator can change
    How to do just thing like that??

    Thank you Konrad

  18. #38
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    448
    Rep Power
    1

    Default Re: Name and Path of the file in JSF

    Hi,

    I would suggest to read about java config files in general and after understanding the handling these files, you could concentrate on best ways to do it inside a tomcat application.
    (Google will give you a lot of hits - so regarding config file in tomcat, I found java - Reading properties from tomcat - Stack Overflow)

    And regarding your other topic: I am glad you found a solution that you can live with.

    With kind regards,

    Konrad

Page 2 of 2 FirstFirst 12

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
  •