[jboss-user] [JBoss Portal] - Re: Downloading a File from within a Portlet.
yacoobkb
do-not-reply at jboss.com
Thu Jan 29 01:53:43 EST 2009
Hi,
Iam struck up with this issue for the past two days any help will be greatly appreciated.
I have a JSF(xhtml) with the following command link
| <a4j:commandLink action="#{navigateBean.downloadPDF}"
| value="View PDF"></a4j:commandLink>
|
Clicking the View PDF should call the bean and the downloadPDF method will call a servlet
Bean code:
|
| package com.portal.tata.beans;
|
| import javax.faces.context.FacesContext;
|
| import org.jboss.seam.ScopeType;
| import org.jboss.seam.annotations.Logger;
| import org.jboss.seam.annotations.Name;
| import org.jboss.seam.annotations.Scope;
| import org.jboss.seam.log.Log;
|
| /**
| * Created by IntelliJ IDEA. User: bharat Date: Sep 27, 2007 Time: 10:30:58 AM
| * To change this template use File | Settings | File Templates.
| */
|
| @Name("navigateBean")
| @Scope(ScopeType.SESSION)
| public class NavigateBean {
|
| @Logger
| static Log log;
|
| String linkType = "";
|
| public String getLinkType() {
| return linkType;
| }
|
| public void setLinkType(String linkType) {
| log.info("setLinkType", linkType);
| this.linkType = linkType;
| }
|
| public String delegateReq() {
| log.info("linkType", linkType);
| return linkType;
| }
|
| public String forward() {
| System.out.println("Check............");
| log.info("forward", "forward this");
| return "done";
| }
|
| public String downloadPDF() {
| System.out.println("NavigateBean.downloadPDF()..........");
| // We must get first our context
|
| String url = "/servlet/downloadservlet?fileName=Reports.pdf";
| FacesContext context = FacesContext.getCurrentInstance();
|
| try {
| context.getExternalContext().dispatch(url);
| } catch (Exception e) {
| e.printStackTrace();
| } finally {
| context.responseComplete();
| }
| return "success";
| }
| }
|
|
Servlet code:
|
| package com.portal.tata;
|
| import javax.servlet.http.HttpServlet;
| import javax.servlet.ServletOutputStream;
| import javax.servlet.http.HttpServletRequest;
| import javax.servlet.http.HttpServletResponse;
| import java.io.File;
| import java.io.PrintWriter;
| import java.io.FileInputStream;
| import java.io.IOException;
|
| public class DownloadServlet extends HttpServlet {
|
| public void doPost(HttpServletRequest request, HttpServletResponse response)
| throws IOException {
| doGet(request, response);
| }
|
| public void doGet(HttpServletRequest request, HttpServletResponse response)
| throws IOException {
| System.out.println("DownloadServlet.doGet()");
| String fileName = request.getParameter("fileName");
| System.out.println("fileName==="+fileName);
| if (fileName == null) {
| response.setContentType("text/html");
| PrintWriter writer = response.getWriter();
| writer.print("<h2>No fileName get parameter was passed</h2>");
| writer.close();
| return;
| }
| try {
| File newFile = new File(fileName);
| System.out.println("newFile.isFile()::"+newFile.isFile());
| System.out.println("path::"+newFile.getAbsolutePath());
|
| FileInputStream in = new FileInputStream(newFile);
| ServletOutputStream out = response.getOutputStream();
| response.setContentType("application/pdf");
| response.setContentLength((int) newFile.length());
| response.setHeader("Content-Disposition", "attachment;filename="+fileName);
|
| for (int i = in.read(); i != -1; i = in.read()) {
| out.write(i);
| }
| in.close();
| out.flush();
| out.close();
| System.out.println("========================================");
| } catch (Exception e) {
| e.printStackTrace();
| response.setContentType("text/html");
| PrintWriter writer = response.getWriter();
| writer.print("<h2>Sorry, that file cannot befound </h2>");
| writer.close();
| }
| }
| }
|
What is happening is on clicking the link the browser shows the junk character of the pdf instead of opening a pop-up save dialog box.
Thanks
Yacoob
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4205444#4205444
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4205444
More information about the jboss-user
mailing list