Document streaming in jsp

hi there,
i'm trying to stream some pdfs to the user's browser so that the first page of the pdf displays whilst the rest of the document loads.
i have been able to get this to work when i call a file directly from the file system but i need to be able to stream the file to the user. the file streams successfully but only when the entire file has been read. can anyone suggest what i'm doing wrong ?
here is my code :
BufferedInputStream in = null;
OutputStream sos = null;
byte[] buffer = new byte[1024 * 4];
int r = 0;
try { 
in = new BufferedInputStream(new FileInputStream(f));
sos = response.getOutputStream();
while ((r = in.read(buffer, 0, buffer.length)) != -1) { 
sos.write(buffer, 0, r);
sos.flush();
sos.flush();
} finally { 
if(in != null) in.close();
if(sos != null) sos.close();
many thanks in advance ...

there is a standard one:
HttpServlet.getLastModified() Why reinvent a wheel?
Paul

Similar Messages

  • Xml document transfer from JSP app to an ASP app

    Hello,
    The client has an existing Active Server Pages application. In-house we have built another application in JSP. We need to send an xml document that we are generating in Java Server Pages along with session login data to the existing ASP application.
    In turn, the ASP application will use XSLT to render the xml on the web browser.
    The JSP web interface consists of a final results screen which contains a hypertext link that links up to the existing ASP application. When a user clicks the hypertext link, the page gets redirected to the Index.asp.
    Do you have any insight as to how I can transfer that xml document from the JSP application to the ASP application along with the session data?
    Thanks for your time.

    Hello,
    I am also interested in this... can you provide an example. Assume I know how to actually create the xml file (which isn't true yet... but I think I know where to go to find out)... what would the jsp (or servlet) code look like to include that xml file as a parameter.
    Thanks
    Eric-

  • To open a pdf document or word document from a JSP into a new window

    Hi,
    How to open a PDF document or word document from a JSP into a new window. i.e say from a jsp when a hyperlink or button is clicked it should open a new window with the pdf document or word document in it.
    thanks in advance,
    radki_j

    Hi ,
    when i tried to use the below code to open my word document , it will open as html and with some garbage details. Doesnt open as word document. How to solve this kind of probem?Need advice urgently.
    Open Document

  • Edit a word document from a JSP page

    hi all,
    i would like to know if it is possible to edit a Word document from a JSP page (without uploading it).
    thanks.

    Technically no. But what you can do is output a word document to the browser with the contenttype "application/msword" and if word is installed it will be opened inside the browser.

  • Rendering pdf document properly in JSP

    I am currently working on a Portal project, which requires generating a PDF file and passing this to a JSP file providing an option to the user to either view the PDF file or download and save it. I've accomplished the first part of file creation (I am using iText for generating the pdf document), but I�m unable to send the file from server to the client side (ie to the browser).
    The code
    response.setContentType("application/pdf");
    response.setHeader("Content-Disposition", "inline; filename=file.pdf");
    which works perfectly fine when used as a standalone to popup an PDF file in tomcat server but fails to accomplish to render the outputstream properly(the stream is rendered in the JSP page improperly as text instead of associating with the application for reading pdf document�s) in the Portal Server Environment. Can anyone throw some light into this problem?

    The code is in JSP.
    What is the difference in putting the code in JSP or servlet.
    How is it working in standalone mode in tomcat application server.

  • Viewing document (blob) in JSP doesn't work

    I am currently supporting an application which talks to an Oracle 11gR1 database using several application servers. Our application is working on the following application servers:
    - Apache Tomcat 5 and 6
    - Oracle Application Server 10gR3
    - WebLogic (configuration 1)
    We have an second server which has WebLogic (configuration 2); however, we cannot get the JSP to display on that server. The same WAR file has been deployed to all four application servers, yet only the WebLogic Server configuration 2 doesn't work.
    WebLogic Server (configuration 2) appears to work (doesn't give an error message); however, the web-page tries to show a document (which appears to be a copy/representation of the existing HTML web page - i.e. a form filter screen). Any insight into the possible problem?
    The code we are using is shown below:
    public ActionForward execute(ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response) throws Exception {
    String fileIdString = request.getParameter("fileIdString");
    try {
    // Get the File to send to the client
    int fileId = Integer.parseInt(fileIdString);
    CustomFile file = fileService.loadFileById(fileId);
    if (file == null) {
    log.error("The fileId (" + fileId + ") could not be found - DNE.");
    int lastDot = file.getFilename().lastIndexOf('.');
    String fileExtension = (lastDot!=-1 ? file.getFilename().substring(lastDot) : "" );
    FileType fileType = fileService.loadByFileExtension(fileExtension);
    if (fileType == null) {
    log.error("The system could not find the file extension (" + fileExtension + ").");
    // Setup the response header so a file can be streamed to the client
    response.setHeader("Content-disposition", "attachment; filename=" + file.getFilename());
    response.setContentLength((int) file.getFile().length());
    response.setContentType(fileType.getWebContent());
    // Break the file down into its bytes so it can be streamed
    ServletOutputStream outputStream = response.getOutputStream();
    InputStream inputStream = file.getFile().getBinaryStream();
    byte[] buffer = new byte[2048];
    int bytesRead = inputStream.read(buffer);
    while (bytesRead >= 0) {
    if (bytesRead > 0) {
    outputStream.write(buffer, 0, bytesRead);
    bytesRead = inputStream.read(buffer);
    // Garbage collection
    outputStream.flush();
    outputStream.close();
    inputStream.close();
    return null;
    } catch (Exception exception) {
    log.error(exception.getMessage());
    return mapping.findForward("failure");
    We had a siimilar issue with some displaying RTF documents and the solution was to remove ALL whitespace characters in the JSP (which seemed to work). I don't see how we can remove the whitespace in the above ActionServlet code.
    Thanks in advance for the help!

    Hi Peter,
    Sorry i am not able to understand what u want to say.If u give some more details it will be useful.
    If i understand u correctly u want the button to be hidden initially and visible after some contidion becomes true right?
    If yes call a javascript function as soon as ur condition becomes true and inside that function make the span visible by writting the following code.
    document.getElementById('id of span').style.visibility='visible';
    Dont forget to give an id to span. it is necessary.
    U can use span itself no need of going for div
    If my view is wrong tell me detaily what is ur ultimate aim?
    Regards,
    Tamil K
    Message was edited by:
            Tamil Venthan

  • How to display a document content in JSP page

    Hi friends,
    I am trying to display a document's content in a JSP page after user authentication.For that I mapped a jsp file with extension .sens in Content Management SDK manager,and put it the file in the directory '/ifs/jsp-bin' of webstarterapp,but it is not working.Down I am giving the code snippet,please help me.I need this to be done fast.
    <HTML>
    <HEAD>
    <%@ page contentType="text/html;charset=windows-1252"%>
    <%@ page import="oracle.j2ee.connector.Login" %>
    <%@ page import="oracle.ifs.adk.filesystem.IfsFileSystem"%>
    <%@ page import="oracle.ifs.adk.http.HttpUtils"%>
    <%@ page import="oracle.ifs.beans.LibrarySession"%>
    <%@ page import="oracle.ifs.beans.DirectoryUser"%>
    <%@ page import="oracle.ifs.beans.PublicObject"%>
    <%@ page import="oracle.ifs.beans.Document"%>
    <%@ page import="java.io.*"%>
    <jsp:useBean id="login" scope="page" class="oracle.j2ee.connector.Login">
    <TITLE>FileExtension.jsp</TITLE>
    </HEAD>
    <BODY BGCOLOR="SILVER">
    <%
    String theDisplay = "initialized";
    String theHidden = "initialized";
    boolean theAdminRights = false;
    try
    login = (Login) request.getSession(true).getValue("IfsHttpLogin");
    LibrarySession ifsSession = login.getSession();
    IfsFileSystem ifsFile = new IfsFileSystem(ifsSession);
    DirectoryUser du = ifsSession.getDirectoryUser();
    String path = HttpUtils.getIfsPathFromJSPRedirect(request) ;
    PublicObject po = ifsFile.findPublicObjectByPath(path);
    if(po instanceof Document)
    Document doc =(Document) po;
    InputStream in= ifsFile.getDocumentContent(doc);
    int i=in.read();
    while(i!=-1)
    out.write(i);
    in.close();
    out.flush();
    out.close();
    theAdminRights = du.isAdminEnabled();
    theDisplay = po.getAttributeByUpperCaseName("DISPLAY").toString();
    theHidden = po.getAttributeByUpperCaseName("HIDDEN").toString();
    catch (Exception e)
    e.printStackTrace();
    %>
    Here is some static content.
    <P>
    The Display Value: <%= theDisplay %>
    <P>
    <%
    if (theAdminRights)
    %>
    The Hidden Value: <%= theHidden %>
    <%
    %>
    </jsp:useBean>
    </BODY>
    </HTML>

    these may help you.
    Google map integration to EBS

  • Document security with JSP

    Hi,
    I was wondering if anyone could help or give me some ideas for this problem I have.
    The web site that I'm developing uses jsp. It has a public section and a private section that requires a username and password to log in to it. Users have the ability to upload reports to the site, which can be of various formats, including HTML (Preferably), PDF, Word, and Excel. They can also mark these reports as private, so that only people that are logged in can view them, although at the moment this just doesn't show the link on the public pages.
    The problem is that people can access the reports directly using the URL without logging in (admitedly they would have to know the URL first), but they could enter, for example,
    http://www.my-domain.com/reports/myreport.doc
    This means there's nothing to stop them accessing these private documents.
    Is there a way of placing the reports outside the browsable tomcat tree and then use the JSP to display it, or the other option I can think of is to use windows security on the folder, but I wouldn't know how to let the JSP access it without opening it up to anonymous users.
    Does anyone have any ideas?
    If you have any questions just reply to this post and I'll get back to you.
    Thanks,
    Dave.

    You need to take a look at reamls:
    http://jakarta.apache.org/tomcat/tomcat-4.1-doc/realm-howto.html
    In short, you define your realm, which is used to authentify your users.
    Then, you set constraints in your web.xml file (it can be as specific as a file, or as generic as a whole folder).
    If someone type a url towards a protected file, they will automagically be redirected towards a login page.
    Hope this helps!
    Don't hesitate to ask if you have more questions.
    Patrick

  • Data Streaming in JSP custom tag

    We have a JSP custom tag which uses a StringBuffer object to store the HTML content. In the doEndTag method, we use the JSPWriter's print method to send the buffer's content to the browser.
    Now, there are cases when the HTML content becomes very large,in the order of 1 - 10MB. In such cases, is there a way we can start streaming the data from the custom tag as the buffer is being built, rather than waiting for the entire buffer to be written out in the doEndTag.

    Yeah, just call the JSPWriter's print method instead of using the StringBuffer, or write your own subclass of StringBuffer (or StringBuilder if Java 5+) which will do this for you automatically when the size is at a certain point, and clear it out)

  • Read a .dot (MS Word Document Template) from jsp

    Hi,
    I have a link to a .dot (MS Word document Template file on the jsp page. While trying to access it from a link on the browser, the file is getting displayed in binary format.
    If I try a normal word document or excel document, I am able to open/save the document.
    I tried adding the following lines in the web.xml too with the same results.
    <mime-mapping>
    <extension>dot</extension>
    <mime-type>application/msword</mime-type>
    </mime-mapping>
    and tried with this also
    <mime-mapping>
    <extension>dot</extension>
    <mime-type>application/vnd.ms-word</mime-type>
    </mime-mapping>
    Can anybody help me on this ?
    Thanks n advance, Sajiv

    As you had directed, I tried a sample jsp with just
    the link to the document. When I click on the link,
    it allows me to open/save the document and the
    default type it gives is with the extension.doc.
    I am able to save the file as a .dot itself.
    So, if this is coming from an application in a
    server, is there any particular reason why it is not
    functioning properly ?I don't fully understand if the problem is already solved or not?
    When you mean by UserAgent, what do you actually
    mean?An useragent is just a client application which can access network sources. In this case it is likely a web browser, for example Internet Explorer or Firefox. It might be useful to try as many useragents as possible so that you can exclude the useragent's default behaviour.

  • Document.lastmodified in JSP?

    I am trying to implement the JavaScript document.lastmodified function in a JSP, but the date does not generate on my HTML page (no errors occur, date does not dispplay). Using document.lastmodified works fine if it's plugged right into the HTML page, so I am not sure why it does not work from within the JSP. If anyone has any ideas and/or an example I can try, I would appreciate it. Thank you.
    Paul

    there is a standard one:
    HttpServlet.getLastModified() Why reinvent a wheel?
    Paul

  • Document Object within JSP func decl.

    I am having difficulties accessing the standard document object from with a JSP function declaration.
    ie.
    <%!
    public void foobar() {
    document.form.blah
    %>
    **ERROR: document object does not exist. **
    Is there anyway to get ahold of the document object? Maybe a package I need to import?!? If someone could point me to some useful documentation I would greatful. I've been searching the API documentation all afternoon. sigh

    I think you might be confused - if not I apologize in advance for the following explanation.
    The standard document object is a client side concept and is usually accessed by Javascript running in the client browser.
    JSPs run on the server and process HTTP requests from the client. In response to these requests, JSPs generate HTML, JavaScript and other elements that can be interpreted by the browser.
    So, a JSP could include some Javascript that references the document object. However, that Javascript would not run until it was received by the browser. There is no way for a server side method to access the client document object.
    Perhaps if you describe what you're trying to do then someone can suggest an alternative design.

  • Problem while i am sending image as a stream between jsp pages

    Hi,
    I want to send the image from one jsp page to another jsp via stream.
    Help to continue the process...

    Thak you for ur reply.
    But in Servlet-Chaining concept, one servlet output stream passed to another servlet and goes on...
    like that, is possible to do same in jsp.
    My actual task is to send the image from one jsp page to another, the sample code i paste here
    <%@ page import="java.io.*" autoFlush="true" %>
    <%
    System.out.println("======Send.jsp=====");
               ServletContext sercon = getServletContext();
                 String filename = sercon.getRealPath("/logo.png");
         RequestDispatcher rd = sercon.getRequestDispatcher ( "/read.jsp" ) ; 
         InputStream file=new BufferedInputStream(new FileInputStream(filename));
         int i,j=0;
                      try
              OutputStream os=response.getOutputStream();
              while((i=file.read())!=-1)
                   j++;
                   os.write((byte)i);
    System.out.println("Bytes Read : "+j);
         rd.forward(request,response);
              os.flush();
              file.close();
         catch(Exception e)
              System.out.println("Error"+j);
    %>
    <%@ page import="javax.swing.*,java.io.*,java.util.*"%>
    <%            
         byte[] by;
         int i,j=0;
    System.out.println("======Read.jsp=====");
         response.setContentType("image/png");
         InputStream is=request.getInputStream();
    System.out.println("In Read.jsp read bytes : "+is.available());
         ByteArrayOutputStream os=new ByteArrayOutputStream();
         while((i=is.read())!=-1)
              j++;
              os.write(i);
         by=os.toByteArray();
         out.println(by+"\n"+i);
         os.close();
         is.close();
    %>If i run this code (send.jsp) the image is displayed in the current page itself, and not forward to read.jsp
    Is any other alternative solutions then point out.

  • Using an XML document in several JSP Pages

    I am using a certain XML document in several different JSP pages (I have several JSP pages in which I need to get data from and save data to the document)
    I'd thought about using it as a bean so that I don't have to reopen it in every page, but I can't since the "Docuemnt" object is actually an interface.
    This is the code I'm using to open the document:
    final String fileName= "./doc/config.xml";
    int listLength, listLengthAuthors;
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    File xmlFile = new File(fileName);
    xmlDoc = builder.parse(xmlFile);
    Any ideas?

    I'd thought about using it as a bean so that I don't have to reopen it in every page, but I can't since the "Docuemnt" object is actually an interface.Use it as a bean. The Document "class" is an interface, but the object you get from DocumentBuilder is an object just like any other object, and there are no restrictions on how you can use it. Try it.

  • Video Streaming with JSP and JMF.

    Hi. I have this problem. I'm projecting (not building yet) a system. In this system i have a Local Computer, a server, and a Web Page (JSP). The Local Computer is going to capture video from a WebCam with JMF. This videos is going to be passed to the server by RTP Control. A Web Page (JSP) is gonna acess the server to get this video and show it (Real Time Streaming).
    My doubt is, how I can do that on JSP ? How i can get the video from server (remebering that this one is getting the video from the local computer) and show it in my page ?

    no one ????

Maybe you are looking for