How to get the Real Path of a file which is accessed  by URL?

iam using tomcat6.0.
I have a file xyz.xml at the top of the webapplication HFUSE which i can able to access by URL
http://localhost:8080/HFUSE/xyz.xml
My problem is how to get the realpath of the file "xyz.xml" for reading and writing purposes.
I tried various things but i could not able to successfully solved the problem?
1) File f = new File("/xyz.xml");
print(f.getAbsolutePath()) ============== it is not fetching the file @ http://localhost:8080/HFUSE/xyz.xml rather it is creating a file
at the root of the drive where eclipse is running.
2) File f = new File("xyz.xml");============> this is also not working , it is creating the file xyz.xml in the eclipse directory ..................
Can anyone please guide on this problem?

RevertInIslam wrote:
If you want your context root(i.e HFUSE)
use this:
request.getContextPath() //where request is HttpServletRequest object to get the needful path.
e.g:
File f = new File(request.getContextPath()+"/xyz.xml");//it will create the file inside HFUSE.
Hope this helps.
Regards
BWrong. The File constructor expects an absolute filesystem path. The HttpServletRequest#getContextPath() doesn't return the absolute filesystem path, it only returns the relative path from the current context root. Use ServletContext#getRealPath() instead, it returns the absolute filesystem path for the given relative path from the current context root.
File file = new File(servletContext.getRealPath("/"), "xyz.xml");

Similar Messages

  • How to get the absolute path of a file from the local disk given the file n

    how to get the absolute path of a file from the local disk given the file name

    // will look for the file at the current working directory
    // this is wherever you start the Java application (cound be C: and your
    // application is located in C:/myapp, but the working dir is C:/
    File file = new File("README.txt"); 
    if (file != null && file.exists())
        String absolutePath = file.getAbsolutePath();

  • How to get the real path of the xml file

    I have a java application
    following is the package structure
    com>>gts>>xml
    having file---------> MyXML.xml
    com>>gts>>java
    having java program to read the file
    Problem is if I use File file = new File("..\\xml\\MyXML.xml");
    java.io.FileNotFoundException: E:\LEARNING_WORK_SPACE\JavaXml\..\xml\MyXml.xml (The system cannot find the path specified)
         at java.io.FileInputStream.open(Native Method)
         at java.io.FileInputStream.<init>(Unknown Source)
         at java.io.FileInputStream.<init>(Unknown Source)
         at sun.net.www.protocol.file.FileURLConnection.connect(Unknown Source)
    How do I get the real path of the xml file.
    Edited by: shashiwagh on Jan 29, 2010 11:46 AM

    Hi,
    if your XML file is inside a package you can easily get it from the classloader.
    Note that your application maybe packaged inside a jar so it is not safe to use java.io.File for this purpose.
    You have an xml file in :
    com/gts/xml/MyXML.xml
    in a class of the same module (that will packaged in the same jar) for example com.gts.java.XmlLoader :
    // To get the stream :
    InputStream is = this.getClass().getResourceAsStream("/com/gts/xml/MyXML.xml");
    // Or the URL :
    URL xml = this.getClass().getResource("/com/gts/xml/MyXML.xml");Hope it helps.

  • How to get the real path of the application in a JavaBean?

    Hi everyone,
    I know this has been asked several times before, but I I am not really satisfied with the answers... :-)
    I am writing a webapp which uses XML files for storing information, the "business" logic is in JavaBeans. So far I just used hardcoded paths, now I will have to put the thing on a server and need a better solution.
    I understand I can use getServletContext().getRealpath() but that doesn't work in a Bean.
    One solution would be to fill a (hidden) field calling that and passing the field to the Bean, but I don't really like that solution.
    I am using Java Studio Creator 2 (Update 1), isn't there a way to access the ApplicationBean instance in my JavaBeans? As I understand it, I could get the path from there as well.
    Thanks!

    When you create the bean that contains the business logic you will have to pass in the real path either as a constructor parameter or by setting a property of the bean.

  • How to get the full path of a file

        If the user selects a file through a browse button, Is there a way, I can get the full path of the file. The file can be from his local hard drive as well.. Can someone advise    

    To upload multiple files (example is for 3), try something like this.
    == Form section ==
    There would be three(3) file form fields for the selection of a file/document to upload, which
    each being uniquely named.
    <div style="clear: both;">
    <cfinput type="file" name="upload_01" dir="ltr" lang="en" size="70" xml:lang="en">
    </div>
    <div style="clear: both;">
    <cfinput type="file" name="upload_02" dir="ltr" lang="en" size="70" xml:lang="en">
    </div>
    <div style="clear: both;">
    <cfinput type="file" name="upload_03" dir="ltr" lang="en" size="70" xml:lang="en">
    </div>
    == Processing section ==
    Checks to see if any files / documents were selected for upload. If none were, then
    displays an alert indicating as such. If at least one file/document was selected, then
    the process would process the upload.
    <cfif isdefined('form.upload_01') and trim(form.upload_01) eq "" and
    isdefined('form.upload_02') and trim(form.upload_02) eq "" and
    isdefined('form.upload_03') and trim(form.upload_03) eq ""
    >
        <p>Did not select any files to upload</p>
    <cfelse>
        <cfif isdefined('form.upload_01') and trim(form.upload_01) neq "">
            <cffile
                action="upload"
                destination="C:\path\to\upload\files\to"
                filefield="upload_01"
                nameconflict="overwrite"
                accept="
                  application/msword
                , application/vnd.openxmlformats-officedocument.wordprocessingml.document
                , application/vnd.ms-excel
                , application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
                , application/pdf">
                <cfcookie name="upload_01_doc" value="#cffile.serverfile#">
                <cfcookie name="upload_01_doc_ext" value="#cffile.serverfileext#">
        </cfif>
        <cfif isdefined('form.upload_02') and trim(form.upload_02) neq "">
            <cffile
                action="upload"
                destination="C:\path\to\upload\files\to"
                filefield="upload_02"
                nameconflict="overwrite"
                accept="
                  application/msword
                , application/vnd.openxmlformats-officedocument.wordprocessingml.document
                , application/vnd.ms-excel
                , application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
                , application/pdf">
                <cfcookie name="upload_02_doc" value="#cffile.serverfile#">
                <cfcookie name="upload_02_doc_ext" value="#cffile.serverfileext#">
        </cfif>
        <cfif isdefined('form.upload_03') and trim(form.upload_03) neq "">
        <cffile
            action="upload"
            destination="C:\path\to\upload\files\to"
            filefield="upload_03"
            nameconflict="overwrite"
            accept="
                  application/msword
                , application/vnd.openxmlformats-officedocument.wordprocessingml.document
                , application/vnd.ms-excel
                , application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
                , application/pdf">
            <cfcookie name="upload_03_doc" value="#cffile.serverfile#">
            <cfcookie name="upload_03_doc_ext" value="#cffile.serverfileext#">
        </cfif>
        <cflocation url="completed.cfm" addtoken="no">
    </cfif>
    == Completed section ==
    Can confirm to the person what files were actually uploaded, by displaying file name and file extension.
    You can you use the file extension information to display a respective icon for the file type.
    <cfoutput>
    <cfif isdefined('cookie.upload_01_doc')>
    01. #cookie.upload_01_doc# - #cookie.upload_01_doc_ext#<br /></cfif>
    <cfif isdefined('cookie.upload_02_doc')>
    02. #cookie.upload_02_doc# - #cookie.upload_02_doc_ext#<br /></cfif>
    <cfif isdefined('cookie.upload_03_doc')>
    03. #cookie.upload_03_doc# - #cookie.upload_03_doc_ext#<br /></cfif>
    </cfoutput>
    Leonard B

  • How to get the actual path of a file present in the Web content folder

    I have a jasper file which is present inside the Web-Content folder of my project.
    I need to pass this file as an argument to the FileInputStream object. How can i give the actual path of the jasper file, since this project can be deployed anywhere else also.

    You may be able to use the getRealPath or getResource methods in ServletContext

  • How to get the real time controller download file for cRIO 9074

    Hello
    I am a beginner of LabView. Now I do a project in school with cRIO 9074.  
    My problem is that I have deleted the embedded controller system by fault. But in the Measurement and Automation software I didn't find the controller sysyem which have the fonction of monitor on line. How can I download the load file?
    Wait for your help, and  thank you for your help very much.
    Solved!
    Go to Solution.

    Even if you format the cRIO system it should be found by the MAX and by opening the Software config you can reinstall the system files assuming that they are installed on the connecting system (PC).
    So you have to use the MAX to setup the cRIO. Maybe it is of help to set the DIP switches to NoApp and IPReset first.
    Hope it helps
    Christian

  • How do I get the real path!!!

    I am trying to get the real path:
    http://xxxxx/listusers.do?section=xxx
    But when I am using request. getRequestURI() I get
    listuser.jsp which is the value that listusers.do returns.
    How do I get the real path?????

    hehe
    You can't get the real path from a virtual
    path..that's why you have virtual paths.

  • How to get the full path instead of just the file name, in �FileChooser� ?

    In the FileChooserDemo example :
    In the statement : log.append("Saving: " + file.getName() + "." + newline);
    �file.getName()� returns the �file name�.
    My question is : How to get the full path instead of just the file name,
    e.g. C:/xdirectory/ydirectory/abc.gif instead of just abc.gif
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.filechooser.*;
    public class FileChooserDemo extends JFrame {
    static private final String newline = "\n";
    public FileChooserDemo() {
    super("FileChooserDemo");
    //Create the log first, because the action listeners
    //need to refer to it.
    final JTextArea log = new JTextArea(5,20);
    log.setMargin(new Insets(5,5,5,5));
    log.setEditable(false);
    JScrollPane logScrollPane = new JScrollPane(log);
    //Create a file chooser
    final JFileChooser fc = new JFileChooser();
    //Create the open button
    ImageIcon openIcon = new ImageIcon("images/open.gif");
    JButton openButton = new JButton("Open a File...", openIcon);
    openButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    int returnVal = fc.showOpenDialog(FileChooserDemo.this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
    File file = fc.getSelectedFile();
    //this is where a real application would open the file.
    log.append("Opening: " + file.getName() + "." + newline);
    } else {
    log.append("Open command cancelled by user." + newline);
    //Create the save button
    ImageIcon saveIcon = new ImageIcon("images/save.gif");
    JButton saveButton = new JButton("Save a File...", saveIcon);
    saveButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    int returnVal = fc.showSaveDialog(FileChooserDemo.this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
    File file = fc.getSelectedFile();
    //this is where a real application would save the file.
    log.append("Saving: " + file.getName() + "." + newline);
    } else {
    log.append("Save command cancelled by user." + newline);
    //For layout purposes, put the buttons in a separate panel
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(openButton);
    buttonPanel.add(saveButton);
    //Explicitly set the focus sequence.
    openButton.setNextFocusableComponent(saveButton);
    saveButton.setNextFocusableComponent(openButton);
    //Add the buttons and the log to the frame
    Container contentPane = getContentPane();
    contentPane.add(buttonPanel, BorderLayout.NORTH);
    contentPane.add(logScrollPane, BorderLayout.CENTER);
    public static void main(String[] args) {
    JFrame frame = new FileChooserDemo();
    frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    frame.pack();
    frame.setVisible(true);

    simply use file.getPath()
    That should do it!Thank you !
    It takes care of the problem !!

  • How to get the current path of my application in java ?

    how to get the current path of my application in java ?
    thanks

    To get the path where your application has been installed you have to do the following:
    have a class called "what_ever" in the folder.
    then you do a litte:
    String path=
    what_ever.class.getRessource("what_ever.class").toString()
    That get you a string like:
    file:/C:/Program Files/Cool_program/what_ever.class
    Then you process the result a little to remove anything you don't want:
    path=path.substring(path.indexOf('/')+1),path.lastIndexOf('/'))
    //Might be a little error here but you should find out //quickly if it's the case
    And here you go, you have a nice
    C:/Program Files/Cool_program
    which is the path to your application.
    Hooray

  • How to get the absolute path of logicalhost server domain on Windows Sun

    i am reading a file from Xsql Folder, that is located in the logicalhost Sun\AppServer\domains\domain1\applications\j2ee-apps.(IN Sun Application Server)
    I am pretty sure that using the absolute path will solve this issue, so my first question is: How to get the absolute path of logicalhost server domain on Windows?
    i tried with System.getProperty("com.sun.aas.instanceRoot").
    but i am able to retrive Sun\AppServer\domains\domain1 upto this .i am unable to retrive Sun\AppServer\domains\domain1\applications\j2ee-apps.
    please suggest me how u can get absolute path in sun application server

    Take a look here

  • How to get the absolute path of logicalhost server domain on Windows?

    My logicalhost server domain behaves strangely. I am reading a file from collaboration definiton, that is located in the logicalhost/is/domains/domain1/config folder. I thought, that this folder is used as an application root folder so I can read files like ./file from there. And it worked.
    But then I've installed the domain as a Windows service, restarted the PC. When the domain1 gets started, I get exceptions saying that the file can't be found. Then I restart the domain (in domainmgr.bat) and it works again.
    I am pretty sure that using the absolute path will solve this issue, so my first question is: How to get the absolute path of logicalhost server domain on Windows?
    And my second question is: Why does this happen?

    The default folder for a Windows Service is the system32 folder contrary to the instance root folder when starting it as a normal process.
    That's the why, haven't tried the how, but you should be able to get the value by calling System.getProperty("com.sun.aas.instanceRoot").
    Hope this helps
    Paul

  • How to get the ablolute path of the web application in WebSphere?

    How to get the ablolute path of the web application in WebSphere?
    For example:
    I have installed IBM WebSphere on D:\WebSphere\Appserver, and I created a new appliction named "myapp" on D:\myapp,. How can I get the absolute path of application "myapp"? In other words,how can I get the absolute path of the application's
    root directory?

    In the WebSphere(default), what directory is the Java Bean's root directory ?

  • How to get the document path of the pictures uploaded for products?

    Hi Gurus,
    How to get the document path of the pictures uploaded for products uploaded through tcode COMMPR01?
    Many Thanks,
    Neeraj

    client path.
    I need to get the client path in order to download files form server to client.
    Best regards,
    Huy.

  • HT201210 I had the ios 7 beta and now I cant figure out how to get the real update

    I have the IOS 7 beta currently on my phone however now I can't figure out how to get the real update

    Be sure to log into developer.apple.com using your developer ID. there should be support on these issues.

Maybe you are looking for

  • HT201365 how do i change the passcode for using my ipad that i created when installing find my iphone. the is different from my apple id and password

    how do i change the passcode for using my ipad which i created when i set up findmyiphone.  this is different from my apple id and password

  • How to play audio

    Hi, I am developing video chat application.I am able to play published video stream.But I am unable to hear sound which is published along with the video. The video is visible but sound is not comming.What is going wrong.Does anybody could point me i

  • How can a non dba user manipulate the dump file outside of oracle ?

    I have a business request to allow a none DBA database user to dump his tables and he can move his dump file on the Unix box from a file system to another file system. This user has a none oracle unix account. When using traditional exp, this is not

  • Starting Up Time

    Hi, I recently just shifted to a mac, and bought a 12 inch powerbook, so I'm still trying to get used to this whole world of mac. I have just one question, and it regards the comp. starting up. Lately, when I switched on the power to start it up, the

  • Pages don't open--site hangups?

    I am using 10.4.6 on a mac-mini, Safari, and my internet connecton is dial-up through Draytek external modem (my only choice). I have noticed for some time that after I have been on the net for awhile, and visited 5-10 sites, the pages will begin to