Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Giving a URL for File

 
Ayaan Dar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ayaan Dar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got exactly what i was looking for at http://www.exampledepot.com/egs/javax.servlet/GetImage.html
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic