Loading images dynamic

596 views
Skip to first unread message

Matthias

unread,
Jul 22, 2008, 6:01:48 AM7/22/08
to Google Web Toolkit
First of all I want to say that I have already read several posted
message concerning this issue. Unfortunately nothing really satisfied
me.
I have created an gwt application which creates images on the server
side. Now I want to store the images on the server in the public path
and send the name of the image and the path (eg. /images/test.jpg)
back to to the client using rpc. On client side I want to set the url
of the image with "image.setUrl("/images/test.jpg") to display it.
Now my questions:
Is it possible to determine the public folder and the existing
subfolder "images" in my RemoteServlet ?
I tried "getServletContext().getRealPath("/public/images")" but the
path was from the tomcat directory and I was not able to access these
files from my apllication, because the application searches within /
src/.../public/images.

Will my approach really work ? Or do I have to do everything
different ?

Thanks.



olivier nouguier

unread,
Jul 22, 2008, 10:08:07 AM7/22/08
to Google-We...@googlegroups.com
Not sure to reply to your problem ;) but:
Saying you've genererated your images in "/tmp/generated"
/tmp/generated/img1.png
You could simply use a servlet|filter to match your URLs http://hostname/generated/ add proxify your "/tmp/generated" content.
HIH

PS: be awere of FS attack when doing this ...
--
"Quand le dernier arbre sera abattu, la dernière rivière asséchée, le dernier poisson péché, l'homme va s'apercevoir que l'argent n'est pas comestible"
- proverbe indien Cri

dablack

unread,
Jul 22, 2008, 10:10:06 AM7/22/08
to Google Web Toolkit
Matthias,

I believe I understand what you are trying to do. I've done something
similar using a database table holding path and filename information
for site images.

The way I understand it is that you are running your site on a tomcat
installation and from the root path of that installation (webapps/
ROOT). I also assume that your images directory is right off that root
path (webapps/ROOT/images). Am I correct? When I deploy my site to my
tomcat server from my development computer that is how my images
directory in my public directory is deployed, so I assume it is the
same in your case.

If so, you should be able to refer to your images as either "./images/
test.jpg" or "images/test.jpg". I perfer to use the preceding
"./" (dot slash) to indicate that the images path is off the current
directory which should be your sites root directory.

I hope this helps.

Matthias

unread,
Jul 22, 2008, 10:31:45 AM7/22/08
to Google Web Toolkit
Thanks for your quick responses.
I already tried to work with relative pathes, but sometimes the
hostmodus will not work properly and sometimes the webmodus will not
work.
Therefore I decided to write a servlet (it was mentioned in other
threads already).
My servlet looks like this:

public void doGet(HttpServletRequest request, HttpServletResponse
response) throws IOException
{
// Get the absolute path of the image
String outPutLocationFile =
(ServerHelper.getImageWorkingDirectory()).trim();
String filename = request.getParameter("img"); // get
the name of the requested image
String filePathName = (outPutLocationFile + File.separator +
filename).trim();
ServletContext sc = getServletContext();
String mimeType = sc.getMimeType(filePathName);

logger.info("outPutLocationFile: " + outPutLocationFile);
logger.info("filename : " + filename);
logger.info("filePathName : " + filePathName);
logger.info("mimeType : " + mimeType);

if (mimeType == null)
{
logger.error("Could not get MIME type of: " +
filePathName);

response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}

// Set content type
response.setContentType(mimeType);

// Set content size
File file = new File(filePathName);
response.setContentLength((int)file.length());

// Open the file and output streams
FileInputStream in = new FileInputStream(file);
OutputStream out = response.getOutputStream();

// Copy the contents of the file to the output stream
byte[] buf = new byte[1024];
int count = 0;
while ((count = in.read(buf)) >= 0)
{
out.write(buf, 0, count);
}
in.close();
out.close();
}

The servlet is called by the client using the Requestbuilder (request
parameters are directly encoded in the url string):

try
{
RequestBuilder rb = new RequestBuilder(RequestBuilder.POST ,
imgUrl);

rb.sendRequest(null, new RequestCallback()
{
public void onError(Request request, Throwable
exception)
{
Window.alert("Error ("+ exception +")");
}

public void onResponseReceived(Request request, Response response)
{

response.getStatusText();
}});
}

I receive the repsonse with status ok. But now I don't know how to
display the image ? Somebody wrote something about using the img tag
with the src attribute.
Any help would be appreciated to solve this issue.

I think this is really a better way to display the images dynamically.



dablack

unread,
Jul 22, 2008, 11:29:06 AM7/22/08
to Google Web Toolkit
Matthias,

Please clarify for me, are you wanting to return the graphic file
itself using your servlet or just the path name and filename as a
string? From your first message, I understood it to be that you just
were returning the path name and filename via RPC. I also understood
it that you were having trouble getting your client-side code access
to the actual image files based on the fact that the path and filename
concatenations for some reason were not working so you wanted to
determine the absolute path to the graphic file and return that.

marco skv

unread,
Jul 22, 2008, 11:41:32 AM7/22/08
to Google Web Toolkit
From the client I can visualize uploaded images with
img.setUrl(GWT.getModuleBaseURL() + "somePath/" + "imageFilename");

In my uploadServlet, I access to the directory where I save images
with:
getServletContext().getRealPath("/") + "somePath/";

Hope this helps
Marco

Matthias

unread,
Jul 22, 2008, 12:04:03 PM7/22/08
to Google Web Toolkit
Hello,
thanks a lot. That works for me in web and hosted mode:

image.setUrl(GWT.getHostPageBaseURL() + "ImageDeliverer?img=" +
imageName);

ImageDeliverer is the servlet name for the servlet mentioned above. It
returns the image itself called with the given imageName.
It is not necessary to store the images on filesystem, because I
return them directly with the servlet.

.

MN

unread,
Jul 22, 2008, 12:20:13 PM7/22/08
to Google Web Toolkit
but you are aware that you open some big security holes with your
ImageDeliverer?

Matthias

unread,
Jul 23, 2008, 3:19:18 AM7/23/08
to Google Web Toolkit
Hello,
no, I'm not aware of the security holes.
Why do you think so ?
I have also implemented a security feature to check the intgerity of
the session.
Did I forgot something ?

olivier nouguier

unread,
Jul 23, 2008, 4:05:59 AM7/23/08
to Google-We...@googlegroups.com
Hi,
 As long as you publish a real file system file from a naming mapping (imageFilename ==> file), there are security issues. But in your case, it's might be trivial to retrieve some file...
When is appening is I ask for http://yourhost/yourWebapp/ImageDeliverer?img=../../../../../etc/password ?

You should at least:
- forbid ".." in image name, leading "/"
- insure that resolved resource is within a "authorized folder".

Cheers

MN

unread,
Jul 23, 2008, 7:40:59 AM7/23/08
to Google Web Toolkit
i think its better resolve the access restrictions to the existing
image file directory then to overload the server with a big
bytetransfer only for the image.

if you want to restrict the access to some pictures its better to copy
the picture in a temp folder with a random GUID as path and then give
only the link back to server. then let the webserver do the delivering
for you.

then delete the temp folder after some time.

Halabe

unread,
Aug 21, 2008, 8:29:55 AM8/21/08
to Google Web Toolkit
Hi,
I am having a problem with image upload and download:

When I upload an image using the FileUpload Widget, I am not able to
see it on the client unless I refresh the page. I am getting the
following error:
Resource not found: images/imagePath/Image1.jpg could a file be
missing from the public path or a <servlet> tag misconfigured in
module.

When I check the uploaded image path, I see the image file there.

Is there any way yo refresh the Module public folder or any other way
to close this issue??

Thank you!


On Jul 23, 2:40 pm, MN <nietz...@gmail.com> wrote:
> i think its better resolve the access restrictions to the existing
> image file directory then to overload the server with a big
> bytetransfer only for the image.
>
> if you want to restrict the access to some pictures its better to copy
> the picture in a temp folder with a random GUID as path and then give
> only the link back to server. then let the webserver do the delivering
> for you.
>
> then delete the temp folder after some time.
>
> On 22 Jul., 12:01, Matthias <matthias....@bkm.de> wrote:
>
> > First of all I want to say that I have already read several posted
> > message concerning this issue. Unfortunately nothing really satisfied
> > me.
> > I have created angwtapplication which createsimageson the server
> > side. Now I want to store theimageson the server in the public path
Reply all
Reply to author
Forward
0 new messages