Solved

send file to the browser and have a save as box without writing the file to the fs

Posted on 2003-10-23
3
206 Views
Last Modified: 2010-04-01
Hi all,

Goal: I want to be able to send a file to the browser without creating a file in the file system. Ideally the "save as" box should show everytime.


Info: I have files stored in database and it is assembled back into a file object in memory. I want to push this to the browser.

Thanks.
al
0
Comment
Question by:asprin_nyc
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
kennethxu earned 125 total points
ID: 9607516
I give you a servlet does download and force popup a save as window, if reads file from file system but you can easily change it to read from memory.
let me know if you have further enquires.
DownloadServlet.java
======================
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public final class DownloadServlet extends HttpServlet {

     public void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, java.io.IOException
     {
        String filePath = request.getPathInfo();
        String filename = request.getParameter( "filename" );
          if( filePath == null && filename != null ) filePath = "/" + filename;
          if( filename == null ) filename = filePath;
          if( filename != null ) filename = (new File(filename)).getName();

        if( filePath != null ) {
               InputStream in = null;
               OutputStream out = null;
               try {
                    in = getServletContext().getResourceAsStream(filePath);
                    if( in != null ) {
                    out = new BufferedOutputStream( response.getOutputStream() );
                    in = new BufferedInputStream( in );
                    response.setContentType( "application/unknow" );
                         response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");
                    int c; while( ( c=in.read() ) != -1 ) out.write( c );
                    return;
                    }
               } finally {
                    if( in != null ) try { in.close(); } catch( Exception e ) {}
                    if( out != null ) try { out.close(); } catch( Exception e ) {}
               }
          }
          response.sendError( HttpServletResponse.SC_NOT_FOUND );
     }
}
0
 

Author Comment

by:asprin_nyc
ID: 9642928
I apologize for the delay for granting the points.

Thank you.
Al
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 9643432
my pleasure and glad to know your problem is solved.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An article on effective troubleshooting
Each year, investment in cloud platforms grows more than 20% (https://www.immun.io/hubfs/Immunio_2016/Content/Marketing/Cloud-Security-Report-2016.pdf?submissionGuid=a8d80a00-6fee-4b85-81db-a4e28f681762) as an increasing number of companies begin to…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question