Smajl
Smajl

Reputation: 7995

Save and load operations in a Portlet - jsp file cannot find the file

I am generating .svg files in a Liferay portlet na saving them in a svg folder which is located on a server (JBoss AS). These files are generated and saved in a .java class, something like this:

private void saveSVG(Document doc, String fileName) {
    // save svg to file
    try {
        File file = new File("svg/" + fileName + ".svg"); // make file
        PrintWriter writer;
        writer = new PrintWriter(new FileOutputStream(file)); // write and
                                                                // save file
        DOMUtilities.writeDocument(doc, writer);
        writer.flush();
        writer.close();

        System.out.println("File path: " + file.getPath());
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

The file is saved in server/bin/svg directory. But when I try to access the file in my .jsp page, it cannot seem to find the file (its probably looking in another directory). How can I tell it, where the desired file is? I could use absolute path, store it somewhere and pass it to the .jsp page, but that doesnt seem like very elegant solution.

<object data="svg/topBar.svg" type="image/svg+xml"></object> 

Or how can I save the file to a relative path where the .jsp page will find it using the upper code?

Thanks for any help!

EDIT: "topBar.svg" is obviously the fileName I am using in this example

Upvotes: 1

Views: 755

Answers (1)

YMomb
YMomb

Reputation: 2387

The problem is that using new File() in the Java code, create the file in a location which is relative to the location of the command line when you started your JBoss while the path in the JSP is relative to your webapp context.

Not sure but I think you can try:

File file = new File(getPortletContext().getRealPath("svg/topBar.svg"))

Upvotes: 2

Related Questions