Re: Problem with Uploading file from Servlet to Client machine.

Yuri Singidas <[email protected]>
Newsgroups gmane.comp.java.sun.servlet
Organization OIT
Message-ID <[email protected]>
Try this,

           File f = null;
           f = new File(pdfFolder+"/tech.pdf");

            if (f.exists()){
                  ServletOutputStream  out = response.getOutputStream ();
                  response.setContentType( "application/pdf" );  // MIME
type for pdf doc
                 response.setHeader("Content-Disposition","attachment;

         filename=output.pdf;");

                  BufferedInputStream  bis = null;
                  BufferedOutputStream bos = null;

                  try {

                     // Use Buffered Stream for reading/writing.
                     bis = new BufferedInputStream(new FileInputStream(f));
                     bos = new BufferedOutputStream(out);

                     byte[] buff = new byte[2048];
                     int bytesRead;

                     // Simple read/write loop.
                     while(-1 != (bytesRead = bis.read(buff, 0,
buff.length))) {
                         bos.write(buff, 0, bytesRead);
                     }


                   } catch(final MalformedURLException e) {
                           System.out.println ( "MalformedURLException." );
                           throw e;
                   } catch(final IOException e) {
                           System.out.println ( "IOException." );
                           throw e;
                   } finally {
                           if (bis != null)
                               bis.close();
                           if (bos != null)
                               bos.flush();
                               bos.close();

                   }

Jasthi Bhanu Prasad (CTC) wrote:

>Hi all,
>        I'm using following code to upload file from servlet to client
>machine for saving that in the client machine.
>        But, I could only see the dialogue box with "save" "Open" "cancel".
>If I click 'Open" it is reporting file corruption. If I save, I can see the
>size of file as 1 byte.
>        Can any one help me by pointing the mistake in the code? or correct
>approach....
>
>                        FileInputStream fo = new
>FileInputStream("c:\\tech.pdf");
>                        response.setContentType("application/pdf");
>
>response.setHeader("Content-Disposition","attachment;
>filename=output.pdf;");
>                        javax.servlet.ServletOutputStream out =
>response.getOutputStream();
>                        long avl = 0;
>                        while( (avl = fo.available()) != 0)
>                        {
>                                byte[] bytes = new byte[(int)avl];
>                                out.write(fo.read(bytes));
>                        }
>                        out.flush();
>                        out.close();
>                        fo.close();
>
>
>
>>Thanks and Regards,
>>  Bhanu
>>
>>
>>
>>
>
>___________________________________________________________________________
>To unsubscribe, send email to [email protected] and include in the body
>of the message "signoff SERVLET-INTEREST".
>
>Archives: http://archives.java.sun.com/archives/servlet-interest.html
>Resources: http://java.sun.com/products/servlet/external-resources.html
>LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
>
>
>

___________________________________________________________________________
To unsubscribe, send email to [email protected] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.