Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

struts and file download

Thread view: 
MileHighCelt - 18 Jan 2006 22:52 GMT
I am getting an Outputstream already obtained when my Action passes the
HttpServletResponse to another method, which then does:

      response.setContentType("text/csv");
      response.setHeader("Content-disposition",
attachment.toString());
      response.setHeader("Pragma", null);
      response.setHeader("Cache-Control","no-store");

     ....
           OutputStream os = response.getOutputStream();
        ....
         os.write(s.getBytes());   // where s is a String;

    then it writes some strings to that stream.  The problem seems to
be that somewhere after this code the IllegalStateException is being
thrown.  I am trapping it in my code but it isn't occuring there.

So my question is, what is the preferred method of returning a file for
the user to download when they click a button/link to trigger this
action?  Is struts already using the HttpServletResponse and triggering
all these exceptions?
MileHighCelt - 19 Jan 2006 21:21 GMT
Nevermind - I found that if I use a printwriter instead of the
OutputStream this clears up.  For those looking for a solution, try :

StringBuffer attachment = new StringBuffer();

attachment.append("attachment;filename=").append(file.getName());
   try {
       response.setContentType(contentType);
    response.setContentLength((int) file.length());
    response.setHeader("Content-disposition", attachment.toString());
    java.io.PrintWriter os = response.getWriter();

       BufferedReader reader = new BufferedReader(new FileReader(file));
    String line;

    char[] buffer = new char[4096];
    int read = 0;

    while((read = reader.read(buffer))>0) {
      line = new String(buffer,0,read);
      os.write(line);
    }
    os.flush();
    close();
    reader.close();


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.