• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to display the allformat files on the browser using jsp or servlets

 
mohan cheepu
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i devleoped code .i display the image file and pdf file displayed on the browser if i use internet explore. i use firefox ,just downloading.
my code .please hlep to me. how display allfile formats on browser.give me source code.
<%@ page language="java" %>
<%@ page import="java.io.*"%>


<html>
<body> 

<% String ss=request.getParameter("file"); %>

<%


String fname ="d:/"+ss;
response.setHeader("Content-Disposition", "attachement; filename=\""+ss+"\"");


InputStream isStream = null;
ServletOutputStream sosStream = null;
try
{
response.flushBuffer();
isStream =new FileInputStream(fname);
sosStream =response.getOutputStream();
int ibit = 250;
while ((ibit) >= 0)
{
ibit = isStream.read();
sosStream.write(ibit);
}

}
catch (IOException e)
{
out.println(e);
}
sosStream.flush();
sosStream.close();
isStream.close();
%>

</body>
</html>
 
K Kiran Kumar
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mohan,
The following link might be helpful to you..
http://forum.java.sun.com/thread.jspa?forumID=48&threadID=549705
Regards,
Kiran.
 
mohan cheepu
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is methodreturnextension and methodreturninputstream ?
it shows error, how to pass the filename into binarry inputstreamm.


i didnt get.
 
K Kiran Kumar
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mohan,

methodreturnextension() is the extension of the file that you need to save it.
methodreturninputstream() is the method where in which you need to convert your file contents to binary stream.

You need to understand how they have implemented in their scenario and try to implement the same in your scenario.
Check in google and be patient on the way to crack the solution.

Regards,
Kiran.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One source of problems might be that you misspelled the Content-Disposition header value. It's called "attachment", not "attachement".

But if you want to display files in the browser you should use "inline" instead. Note that the user may have set the browser to not open certain file types, and save them to disk instead. There's nothing the web app can do about that.

Furthermore, streaming files from a JSP page is bound to cause problems, because JSPs are meant for outputting text, not binary data. You should use a servlet instead.
[ May 21, 2008: Message edited by: Ulf Dittmer ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:

Furthermore, streaming files from a JSP page is bound to cause problems, because JSPs are meant for outputting text, not binary data. You should use a servlet instead.


Specifically, the JSP compiler injects its own whitespace characters into the output stream which almost always causes the downloaded file to be corrupt.
We have an demo project in our CodeBarn that streams files from a servlet.
See:
http://faq.javaranch.com/java/CodeBarnSimpleStream
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic