Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
213 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 500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In this article, the configuration steps in Zabbix to monitor devices via SNMP will be discussed with some real examples on Cisco Router/Switch, Catalyst Switch, NAS Synology device.
The decision to migrate to the cloud is not a simple one—many factors, such a cost, ease of use, and ongoing maintenance come into play. The goal is always for cloud platforms to make storage and backups more seamless and effective. Here’s a look at…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month10 days, 8 hours left to enroll

886 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