• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Download file

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for that post, it was exactly what I was looking for.

Could you tell what does 'com.bsl.common.configuration.CommonConfiguration' do?

SV.
 
You totally ruined the moon. You're gonna hafta pay for that you know. This tiny ad agrees:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic