Problems retrieving a file uploaded through a web service

Posted on 2011-03-25
Last Modified: 2012-06-27
I have a web service that uploads a few files to the server.   The service uses Glassfish V3.  The folder and files it creates can be found in my domain folder in the Netbeans config/GF3 folder.  When I try to read that file afterwards from my web application (which contains the web service), or from any other web application using the same server and domain, I get a "resource not available" message.  What am I doing wrong?

For example, my web service operates from this url :

and is told to write to errors/

Say my file is test.txt, none of these paths gets me to the file that was written:

I am using Netbeans 6.9.1 and J2EE to develop this application.
Question by:Emilie
  • 2
  • 2
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 35217289
you want mention apsolutae path not relative path ??

can you post the code

Author Comment

ID: 35217971
The code below shows the steps I take to save the file from the web service.  I also save a copy of the path to each file to my database.

The second part is in a different application (running with the same server, and associated to the same domain).  I only included the method that tries to retrieve the path.

The method getPath() returns the path stored in my database.  In this case for instance, it will return :

If I put that directly in my browser, the file will open correctly.  But the image won't show in my page.  Any anchor tag pointing to it will also not prompt any action from the browser.

I have also tried storing the relative path, in which case I get something like:

This also doesn't work.

Right now I'm testing on a pc, with a local server.  I don't know if I would encounter the same problem once it's deployed to an actual server. (I don't have access to a staging environment yet).
public class ErmResource {
public static final String ERROR_RESPOSITORY = "errorReports\\";

public String loadFile(@Context HttpServletRequest request) {
     try {
       RESTwsEJBRemote wsEJB = (RESTwsEJBRemote) new InitialContext().lookup("ejb/wsEJB");
       zipFile = new File(fileRepository + fileName);

try {
   if (zipFile.getPath().lastIndexOf("\\") != -1 && !zipFile.exists()) {
        new File(zipFile.getPath().substring(0, zipFile.getPath().lastIndexOf("\\"))).mkdirs();
            return true;
} catch (Exception e) {
     return false;



public class RetrieveDetailedError extends AJAXAction {

public String createAttachments(int appId, String errorUID) throws Exception {
        String html = "";
        try {
            int errorId = searchEJB.getErrorId(errorUID);
            List<Attachment> attchmnts = searchEJB.getAttachments(errorId, " not in ('image')");
            String screenshot = "";
            String logs = "";
            String others = "";

            html += "<div class='trigger'>";
            html += "<div class='sectionTitle' id='mouseover'><img class='expand' src='../images/collapsed.png' align='left'/>";
            html += "<p>Attachments</p></div></div>";
            html += "<div class='toggle_container'>";
            html += "<div class='block'><p>";

            if (!attchmnts.isEmpty()) {
                Iterator itr = attchmnts.iterator();
                while (itr.hasNext()) {
                    Attachment attchmnt = (Attachment);
                    int i = 1;
                    String name = findAttachmentName(attchmnt.getpath());
                    if (name.length() < 2) {
                        name = attchmnt.getpath();

                    if ("log".equalsIgnoreCase(attchmnt.getType())) {
                        logs += "<a class='default' href='" + attchmnt.getpath() + "'>" + name + "</a><br/>";
                    } else if ("image".equalsIgnoreCase(attchmnt.getType())) {
                        screenshot += "<a class='default' href='" + attchmnt.getpath() + "'>Image" + i + "</a><br/>";
                    } else {
                        others += "<a class='default' href='" + attchmnt.getpath() + "'>" + name + "</a><br/>";


                if (screenshot.length() > 1) {
                    html += "<p><b>Screenshot</b><br/><br/>" + screenshot + "</p>";

                if (logs.length() > 1) {
                    html += "<p><b>Logs</b><br/><br/>" + logs + "</p>";

                if (others.length() > 1) {
                    html += "<p><b>Other Attachments</b><br/><br/>" + others + "</p>";

                html += "</p></div>";
                html += "<img class='bottom' src='../images/tabBoxBottom.png'/></div>";
                html += "<img class='bottomShadow' src='../images/tabShadowBottom.png' />";

        } catch (Exception e) {
            throw e;
        return html;


Open in new window

LVL 20

Accepted Solution

Sathish David  Kumar N earned 500 total points
ID: 35239287

Author Closing Comment

ID: 35259177
Thanks, these links were pretty useful.  Ended up solving my problem by saving the file using the Servlet Context but recording its location in the database using the URI context.  I was thus able to retrieve the files from another web app.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial will show you how to add an attribute to an XML ( stream returned from a Windows Communication Foundation ( (WCF) Web Service.  Some knowled…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question