| Author |
Download file
|
Stephen ODonnell
Greenhorn
Joined: Sep 07, 2004
Posts: 19
|
|
Hi, I just have quick question. How do I download a file from a servlet? At the moment I have a jsp page, which takes user details and calls a servlet which generates a file and saves it to disk. I now want to let the user download this file? Any help greatly appreciated Thanks Stephen.
|
 |
gudla Dathathreya Reddy
Greenhorn
Joined: May 05, 2004
Posts: 15
|
|
Hi Guy, This code 100% definitely solve your problem. This is below . import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionMessage; import com.bsl.common.configuration.CommonConfiguration; import com.bsl.sfa.Constants; public class DownloadServlet extends HttpServlet { private void performTask( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { HttpSession session = request.getSession(false); if (session.getAttribute(Constants.SESSION_USERID) == null) { response.sendRedirect("/sfa/error.jsp"); } else { String lsFileName = request.getParameter("filename"); String lsPathKey = request.getParameter("pathkey").replace('\\', '/'); if (lsFileName != null && lsPathKey != null) { File loDownloadFile = new File( CommonConfiguration.getProperty( lsPathKey, lsPathKey) + "/" + lsFileName);// path of the file if (loDownloadFile.isFile()) { InputStream is = new FileInputStream(loDownloadFile); ServletOutputStream loOut = response.getOutputStream(); if (lsFileName.endsWith(".pdf")) { response.setContentType("application/pdf"); } else if (lsFileName.endsWith(".doc")) { response.setContentType("application/msword"); } else if (lsFileName.endsWith(".xls")) { response.setContentType("application/vnd.ms-excel"); } else { response.setContentType( "application/download; charset=UTF-8"); } response.setHeader( "Content-Disposition", "attachment;filename=" + lsFileName); int bytesRead = 0; byte[] buffer = new byte[8192]; while ((bytesRead = is.read(buffer, 0, 8192)) != -1) { loOut.write(buffer, 0, bytesRead); } loOut.flush(); is.close(); loOut.close(); } else { throw new FileNotFoundException("File not found"); } } else { throw new FileNotFoundException("File not found"); } } } catch (Throwable t) { ActionErrors errors = new ActionErrors(); if (t instanceof FileNotFoundException) { ActionMessage error = new ActionMessage("global.error.filenotfound"); errors.add(ActionErrors.GLOBAL_ERROR, error); request.setAttribute("org.apache.struts.action.ERROR", errors); } else { ActionMessage error = new ActionMessage("global.error.general"); errors.add(ActionErrors.GLOBAL_ERROR, error); request.setAttribute("org.apache.struts.action.ERROR", errors); } this.getServletContext().getRequestDispatcher( "/error.jsp").forward( request, response); } } /* (non-Javadoc) * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { performTask(req, res); } /* (non-Javadoc) * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { performTask(req, res); } } Regards, Dathathreya Reddy, Cramer System Europe Limited, dathathreya.gudla@cramer.com
|
 |
Salvador Valencia
Greenhorn
Joined: Jan 14, 2005
Posts: 1
|
|
Thanks for that post, it was exactly what I was looking for. Could you tell what does 'com.bsl.common.configuration.CommonConfiguration' do? SV.
|
 |
 |
|
|
subject: Download file
|
|
|