This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes File Mapping from Servlet to Client Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "File Mapping from Servlet to Client" Watch "File Mapping from Servlet to Client" New topic
Author

File Mapping from Servlet to Client

RamaSarma Vemuri
Greenhorn

Joined: Aug 07, 2004
Posts: 9
Hi,

I want help in Servlets....

My problem is, i am having one servlet and one client(Html) and one imageFile. My servlet knows the filePath, now it has to process the filePath so that the imageFile is displayed in Html. Now, the issue is in servlet is there any other procedure other than streaming the file in to ServletoutputStream??? I know that the following code solves my problem,
File f = new File("F:\\Images\\2\\3\\2004\\12282004\\08\\1.jpg");

byte[] imageData = new byte[(int) f.length()];

FileInputStream fis = new FileInputStream(f);

fis.read(imageData);

response.setContentType("image/jpeg");

ServletOutputStream out = response.getOutputStream();

out.write(imageData);

But what I want to know is, instead of Streaming the file into byteArray and set into ServletOutputStream, is there any other alternative???

Thanks in advance

Regards,

Rama Sarma Vemuri L S K
Mobile: +91-9885584568
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

As a general rule you Servlet shouldn't really be outputting any HTML. I know it can, and ther are examples on Sun's site which do just that, but it makes for very unmaintainable code. You might consider JSPs.

That being said, remember that your Servlet is juts creating a static web page, and how web pages work. You don't add much be streaming the file which just including it in an <img /> tage wouldn't do anyway. So why not just serve the image as you would for a static HTML page, and link to it?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File Mapping from Servlet to Client