• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

trouble with accessing jpeg image outside of webapp with getResource(AsStream)

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, after looking around a lot through the forums I'd first like to say thanks to everyone here. I've been hangin' out on the ranch for a little while and this is the first time I've actually felt like I needed to post. I always find an answer or am pointed in the right direction! (sorry for any improper use of jargon I'm not 100% on that stuff)

My problem is this: I have an app that works great on my local machine but not the server and the broken part is how I am accessing some image files.
I create jpg graphs in two different conditions:
When a file is fist inserted into the database
OR
If the file was already in the database prior to having graphs created on insertion

I save the graphs using a system call to a folder like this:
D:\data\IRdb\graphs\graph.jpg

and then update a database with these file paths.

So, on the server the path is not exactly the same but I need to figure out how to get to these with a servlet link.


This is the code that works fine on my machine. I have a little icon for each graph in the table and you should be able to hit the button to display the corresponding jpeg. When doing this on the server everything saves alright its just the html part with the href that will not work. I get a "resource cannot be found" error.

I first tried getServletContext().getRealPath() even though all the posts said not to (I just had to find out for myself) and of course it didn't work on the server (even though we DO use tomcat on our server which should explode the files but I've moved on from there anyways).

I tried making something similar to Ben Souther's SimpleServlet example also but I ended up with the browser trying to save an image rather than have it pop up in the window.

I think that I probably need to use getResource() or getRescourceAsStream() in order to access my files.
When I try something like:

getServletContext().getResource("/") will return something but ONLY if i have the files in the WEB-INF/graphs directory in eclipse(I made another directory after frustration with the one on the D drive, I can't really get either to work anyways). This is no good because in some cases the picture doesn't even exist so I really need the directory rather than a file link.

getServletContext().getResourceAsStream("/WEB-INF") is really the only thing that doesn't turn out null but right now doesn't help because that is not where my files are. I am pretty sure this is what I somehow need to use but I am iffy on things like using InputStream and what classpath is. Otherwise I have been getting null return's for filepath strings i have been giving this function.

So that is what I know. If there was something I was unclear about or left out please let me know. I appreciate any help. I thought this was going to be an easy problem but have been banging my head off of walls for days.

[ April 02, 2007: Message edited by: Jenny Brook ]
[ April 02, 2007: Message edited by: Jenny Brook ]
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

First off, please go back and fix the formatting of your posted code to remove the excess indenting. You can see how hard it making your post to read. And making your post hard to read is a good way to get people to ignore it.

ServletContext.getRealPath() and ServletContext.getResourceAsStream() will only work for resources that are within the folder hierarchy of the web application. If the files are outside of the bounds of the web app, these methods are useless to you. You'll need to use good old File I/O.

You should never hard-code paths in your app. You can provide the path in a number of standard means: as a context parameter in web.xml, or via a Java properties file.
[ April 02, 2007: Message edited by: Bear Bibeault ]
 
Jenny Brook
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm okay thanks,

Could someone maybe point me to an example of how to do that with web.xml or the properties file, or where I could read up on it? I am new to servlets and right now am just adding some things to something that existed long before I got here. I putzed around a little last night but had trouble finding anything that I understood.

Thanks,
Jenny
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's an example of setting a context param in web.xml:



You retrive the value in code with ServletContext.getInitParameter()
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic