Generate URL to image stored on server

1,794 views
Skip to first unread message

Gary

unread,
Jun 6, 2012, 5:09:41 AM6/6/12
to google-we...@googlegroups.com
Hi,

my application has a mechanism allowing a user to upload an image. This image is then stored on the server's disk. The important point to note is that the image is out-with the deployed WAR file.

How do I generate a URL to access the image that has been uploaded? I want to return this URL after the upload is complete, so that I can construct an image widget around it.

Derek

unread,
Jun 6, 2012, 9:30:23 AM6/6/12
to Google Web Toolkit
Not sure what you mean by "out-with," but I'll assume you mean you
upload it to what would be the war folder in a GWT project. If that's
true, use GWT.getHostPageBaseURL()+"image.png" to get the URL.

Derek

On Jun 6, 5:09 am, Gary <garyker...@googlemail.com> wrote:
> Hi,
>
> my application has a mechanism allowing a user to upload an image. This
> image is then stored on the server's disk. The important point to note is
> that the image is *out-with* the deployed WAR file.

Gary

unread,
Jun 6, 2012, 9:52:03 AM6/6/12
to google-we...@googlegroups.com
I mean it's being stored on the server's local disk, specifically the user's home directory (the user running the servlet container process, e.g. C:\Documents and Settings\garyk) I tried for days to use relative directories(i.e. creating Java 'File' objects without a full path, so that they were placed in the working directory) but this never worked. I need to pass these uploaded images to other serverside classes, which use them to generate more images, and this always failed: I figured it was because the servlet was creating and destroying temp directories.

I was also warned never to rely on a 'working directory' when programming a servlet. Is this true?

I'm basically having an all torrid time with serverside file management. I don't know if I'm misunderstanding something fundamental because what I'm trying to do is quite simple but I simply cannot get it to work. I'll break it down and perhaps someone can help me.

1. User uploads image. This is stored and the URL returned. An image widget is constructed around this URL.
This part is functional as I use gwtupload, which has a clientside function to get the URL to the file just uploaded. I need to know how gwtupload generates this.

2. If it qualifies, this image may be cropped. The crop interface simply sends a coordinate pair to the server. A new cropped image is made, and the old image deleted.
I now need to return the URL of the new image. i.e. emulate what gwtupload does with Uploader.getFileUrl().

3. User can preview their work: the generation of previews relies on knowing the locations of the previously uploaded images.
The URL to the generated preview image must be returned so the browser can show  the image.

It seems so simple yet I repeatedly fail. I hope someone can offer some help.

Piro

unread,
Jun 6, 2012, 10:24:53 AM6/6/12
to google-we...@googlegroups.com
I don't think you can access files in this manner. If it would be possible it would mean you can access any file on server(no security).

What you have to do is to store the file anywhere and generate some sort of ID for it. Then you need to implement HTTP GET request by servlet. Servlet will need to map ID to file location and write file to response (and set some things like content type).

Iam sure there are other ways (REST,...).
Piro

Gary

unread,
Jun 6, 2012, 12:09:21 PM6/6/12
to google-we...@googlegroups.com
That makes sense. I have changed the location of storage to be the working directory:

public static File dataDirectory = new File("card_designer");

I prepend this File to each new File I open. So now I just need to construct a URL to grab these images.

I have tried:

public static String accessURL = GWT.getModuleBaseURL();

On constructing an Image widget for my dynamic images, I append the relative filepath to this accessURL, as so:

Image example = accessURL + "card_designer/exampleImage.jpg";

However, I get a 404 file not found in my console on development mode:


I think I'm close, but I don't understand why the GET request fails. The images is right there on the disk.

Derek

unread,
Jun 7, 2012, 1:33:50 PM6/7/12
to Google Web Toolkit
Because you need to use GWT.getHostPageBaseURL() not
GWT.getModuleBaseURL() (or use modulebase, but prepend "../" to your
filename). I assume the actual url to get the image is
http://127.0.0.1:8888/card_designer/exampleImage.jpg . That should get
the image.

BUT, going back to the "working directory" thing, it IS problematic to
write files there. At least, it is if you expect those files to be
there long term. Every time you redeploy a war in production, your app
server (tomcat, glassfish, whatever) will overwrite everything in the
directory which will erase working directories like card_designer. If
you just need some temporary space to store an image while the user
does things like cropping the image and then they download the
finished product, then that might be fine. But if they expect that url
to work long term, I wouldn't use that space.

(Also, don't know if you've considered the problem of lots of people
uploading images named the same thing or not.)

I recently had to do something similar to what you did. We have a
directory mounted on our production servers that holds uploaded images
(and isn't accessible via the web). When someone uploads a file, I
check the MD5 hash of the file to see if it's a match of another image
(in my use case, it's very probable that a user will upload the same
image or images every time they use the app). If it's not, then I
write the file to the shared directory in the MD5hash.ext format.
Either way, I then return the calculated filename to the GWT code.

The GWT code then immediately executes a request to a servlet hosted
at "/mymodule/images/*" and uses the path info to get the filename and
goes to look it up in the shared directory and relays the data through
the servlet.

Hope that's all clear enough.

Derek

Jonathan Franchesco Torres Baca

unread,
Jun 7, 2012, 2:14:17 PM6/7/12
to google-we...@googlegroups.com
Usa el la clase Key de google engine y autogenerala

2012/6/7 Derek <derek...@gmail.com>
--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.


Nader

unread,
Jun 7, 2012, 3:17:33 AM6/7/12
to Google Web Toolkit
Image example = accessURL + "card_designer/exampleImage.jpg"

This only works if "card_designer" folder is under your Web Root so
servlet container/Http server could serve it. And that's not a good
idea to store
contents

On the other hand your current working directory is not your Web root!
So new File("card_designer") wil create a directoy under your current
working directory.

As Piro said you should store your contents (images) some where in
your server and serve file contents yourself( Write contents in
response ).

Gary

unread,
Jun 13, 2012, 9:25:15 AM6/13/12
to google-we...@googlegroups.com
I've been away from my work for a few days but today I got around to solving this issue. As the images were only temporarily needed, I decided on using the working directory to store and retrieve. 

To serve the images correctly, I wrote a simple image reading servlet that copies the input stream from an image to the HttpResponse's output stream, setting the content length and type appropriately. The image is required is specified via a parameter in the request URL.

I have included the servlet code below for those facing a similar issue. Thanks for all your help in this matter.

package carddesigner.server;

import java.io.File;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.activation.MimetypesFileTypeMap;

public class ImageServlet extends HttpServlet {
public static MimetypesFileTypeMap types = new MimetypesFileTypeMap();
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)  
throws ServletException, IOException  {
//Get the name of the image required
String imageName = request.getParameter("imageName");
File file = new File(imageName);
response.setContentLength((int) file.length());
response.setContentType(types.getContentType(file));
//Copy the input stream to the output stream
FileInputStream in = new FileInputStream(file);
OutputStream out = response.getOutputStream();
byte[] buf = new byte[1024];
int count = 0;
while ((count = in.read(buf)) >= 0) {
out.write(buf, 0, count);
}
in.close();
out.close();
}

}

Reply all
Reply to author
Forward
0 new messages