aspose file tools*
The moose likes Servlets and the fly likes Giving a URL for File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Giving a URL for File" Watch "Giving a URL for File" New topic
Author

Giving a URL for File

Ayaan Dar
Greenhorn

Joined: Oct 27, 2010
Posts: 10
Hi,

I am facing a small issue. I have a Servlet that receives a request with data in the Query String. The servlet creates a file, and writes some data to it. Now the Servlet should return the URL to the file. How can i do this? I have tested the writing a file, in eclipse and it give a temp directory like
D:\Eclipse Workplaces\ImageService\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\MyService\MyService. This is not what i want. I want to write to a directory of my choice and then return the Web url of this file like http://localhost:8080/MyService/122-1234-123.txt. How can i accomplish this. Really stuck. Any help would be nice.

Kind regards,
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

Unless you write the file to a location inside the web application -- which is not recommended, you cannot simply give a URL to the file.

Rather, you can store the file anywhere and write a streaming servlet that knows how read that file and to stream the contents as a response. The URL would be to the servlet, not the file.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ayaan Dar
Greenhorn

Joined: Oct 27, 2010
Posts: 10
So you are saying what i am trying to do is not possible. Well what if i write the file lets say an image to some absolute path like c:\images\. No i get a request for a file name called 1234-4452-22. My servlet realises that the user is requesting for a MyPic.jpg. I read the JPG into a file object. Now what . How can i serve this image to the user. Setting the content type or something. The URL i provide, will be used in a html image tag like

<img src="http://localhost:8080/MyService/FileName=1234-4452-22 /> . How can i accomplish this, using a Servlet, a HTML page to display the image etc.

Kind Regards.

Ayaan Dar
Greenhorn

Joined: Oct 27, 2010
Posts: 10
Got exactly what i was looking for at http://www.exampledepot.com/egs/javax.servlet/GetImage.html
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

You've pretty much got the idea. The servlet will read the file and stream the content as the response, setting the headers to the appropriate content type.

There are numerous examples in previous posts this forum.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

Ayaan Dar wrote:Got exactly what i was looking for at http://www.exampledepot.com/egs/javax.servlet/GetImage.html


Well not exactly. That example uses getRealPath() which assumes that the file is within the web app. It should not be.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Giving a URL for File