Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Problem with Download in jsf

  Asked By: Jaxson    Date: Jul 14    Category: Java    Views: 2335
  

I use jsf 1.2 and Richfaces 3.3.0 .
I restore file from Db when i want to download that file in jsf backing bean i get this exception :

javax.servlet.ServletException: Servlet response already use stream, Writer not possible


My code is :

public void download()
{
try
{

Attachment attach = attachments.get(attachmentRowId);
byte[] binaryData = attach.getFile();

FacesContext ctx = FacesContext.getCurrentInstance();

if (!ctx.getResponseComplete())
{

String contentType = "application/x-download";

HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();
response.setContentType(contentType);
response.setContentLength(binaryData.length);
response.setHeader("Content-Disposition", "attachment;filename=\"" + attach.getFileName() + "." + attach.getFileType() + "\"");


ServletOutputStream out = response.getOutputStream();

out.write(binaryData);
out.flush();
}

}
catch (Exception e)
{
e.printStackTrace();

}

}

every body can help me please;

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Husani Chalthoum     Answered On: Jul 14

Please tring following code:

 public  void download() {
try {
 attachment  attach = attachments.get(attachmentRowId);
byte[] binaryData = attach.getFile();

HttpServletResponse response  = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("fileName", "FILE_NAME");
response.setContentType(contentType);
response.setContentLength(binaryData.length);
response.setHeader("Content- Disposition", "attachment; filename= \"" + attach.getFileName() + "." + attach.getFileType() + "\"");
ServletOutputStream out = response.getOutputStream();
out.write(binaryData);
out.flush();
out.close();
response.flushBuffer();
FacesContext.getCurrentInstance().responseComplete();

}
catch (Exception e) {
e.printStackTrace();
}
}

 
Answer #2    Answered By: Jared Adams     Answered On: Jul 14

I have used your suggestion but it hasn't solved yet .
it write in page some character like this :
T?
?????????<A\
533;?0??&?????ᦙ\
3;@?@?@??????A\
533;n;?????????&#\
65533;?????????&#\
65533;?&?@>??

but instead, I expect that the download  dialog be open in browser.
I use firefox 3.

 
Answer #3    Answered By: Tarrant Thompson     Answered On: Jul 14

You could use this code  :

public static void  download(String address, String localFileName) {

OutputStream out = null;
URLConnection conn = null;
InputStream in = null;
try {
URL url = new URL(address);
out = new BufferedOutputStream(
new FileOutputStream(localFileName));
conn = url.openConnection();
in = conn.getInputStream();
byte[] buffer = new byte[1024];
int numRead;
long numWritten = 0;
while ((numRead = in.read(buffer)) != -1) {
out.write(buffer, 0, numRead);
numWritten += numRead;
}
System.out.println(localFileName + "\t" + numWritten);
} catch (Exception exception) {
exception.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException ioe) {
}
}
}

 
Didn't find what you were looking for? Find more on Problem with Download in jsf Or get search suggestion and latest updates.




Tagged: