Emilie
asked on
Problems retrieving a file uploaded through a web service
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 :
http://localhost:8080/WS/resources/errorRetriever
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:
http://localhost:8080/WS/resources/errorRetriever/errors/text.txt
http://localhost:8080/WS/resources/errors/text.txt
http://localhost:8080/WS/errors/text.txt
http://localhost:8080/errors/text.txt
I am using Netbeans 6.9.1 and J2EE to develop this application.
For example, my web service operates from this url :
http://localhost:8080/WS/resources/errorRetriever
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:
http://localhost:8080/WS/resources/errorRetriever/errors/text.txt
http://localhost:8080/WS/resources/errors/text.txt
http://localhost:8080/WS/errors/text.txt
http://localhost:8080/errors/text.txt
I am using Netbeans 6.9.1 and J2EE to develop this application.
ASKER
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 :
C:\Users\...\.netbeans\6.9 \config\GF 3\domain1\ errors\dev 341557169\ dev3415571 69.jpg
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:
errors\dev341557169\dev341 557169.jpg
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).
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 :
C:\Users\...\.netbeans\6.9
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:
errors\dev341557169\dev341
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).
@Path("errorRetriever")
public class ErmResource {
public static final String ERROR_RESPOSITORY = "errorReports\\";
[...]
@POST
@Produces("text/plain")
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();
}
item.write(zipFile);
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) itr.next();
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/>";
i++;
} 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;
}
[...]
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
can you post the code