Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > struts and file download

Reply
Thread Tools

struts and file download

 
 
MileHighCelt
Guest
Posts: n/a
 
      01-18-2006
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?

 
Reply With Quote
 
 
 
 
MileHighCelt
Guest
Posts: n/a
 
      01-19-2006
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(f ile.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();

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
adding action mapping to struts-config file in struts ros Java 0 05-15-2007 10:51 AM
Eclipse and Ant with Struts: how to combine to generate javadoc api, how to map struts source to binary code for debugging Merlijn Java 0 03-24-2006 01:47 PM
Is it safe to point to Internet for locating struts DTD files in struts TLDs and XML files? Katie Wright Java 8 01-07-2005 03:37 PM
File upload/download from database. Download appends aspx page to end of file Ryan Taylor ASP .Net 2 11-10-2004 08:31 PM



Advertisments