Servlet for download files from a specific folder?
Solution 1
Since you're handling the data on the doGet
method, you can pass a parameter to the servlet where you indicate the name of the file you want to download. For this, you should assume that the file name exists in your base path. The code could go like this:
HTML
<body>
Click on the link to download:
<a href="DownloadServlet?fileName=data.xls">Download Link</a>
</body>
Java Servlet Code:
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
//retrieving the parameter by its name
String fileName = request.getParameter("fileName"); //this will return `data.xls`
//using the File(parent, child) constructor for File class
File file = new File(filePath, fileName);
//verify if the file exists
if (file.exists()) {
//move the code to download your file inside here...
} else {
//handle a response to do nothing
}
}
Note that since the code now uses File(String parent, String child)
constructor, your filePath
should not contain the separator anymore (this will be handled by Java):
public void init() {
// the file data.xls is under web application folder
filePath = getServletContext().getRealPath("");
}
Solution 2
you can do like that
public void doGet(HttpServletRequest req, HttpServletResponse res){
res.setContentType("text/html);
PrintWriter out=response.getWriter();
String fileName="home.txt";
String filePath="d:\\";
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition","attachment;fileName=\""+fileName+"\"");
int i;
FileInputStream file=new FileInputStream(filePath +fileName);
while((i=file.read()) !=-1){
out.write(i);
}
file.close();
out.close();
}
AdiCrainic
Updated on July 09, 2022Comments
-
AdiCrainic almost 2 years
I am new to JAVA technology,especially Servlets.I need to make a Web application project which have an upload and a download files to/from a server(tomcat).I have already an upload servlet,which works fine.
i have also a download servlet,found on the internet.But the problem is that this servlet allows downloading only a specific file,and the path to this specific file is given in the servlet. I need to let the client see the entire content of my upload folder and select which file he wants to download from this folder.
The code of the download servlet is this:
import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class DownloadServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { static final long serialVersionUID = 1L; private static final int BUFSIZE = 4096; private String filePath;` public void init() { // the file data.xls is under web application folder filePath = getServletContext().getRealPath("") + File.separator;// + "data.xls"; } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { File file = new File(filePath); int length = 0; ServletOutputStream outStream = response.getOutputStream(); ServletContext context = getServletConfig().getServletContext(); String mimetype = context.getMimeType(filePath); // sets response content type if (mimetype == null) { mimetype = "application/octet-stream"; } response.setContentType(mimetype); response.setContentLength((int)file.length()); String fileName = (new File(filePath)).getName(); // sets HTTP header response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); byte[] byteBuffer = new byte[BUFSIZE]; DataInputStream in = new DataInputStream(new FileInputStream(file)); // reads the file's bytes and writes them to the response stream while ((in != null) && ((length = in.read(byteBuffer)) != -1)) { outStream.write(byteBuffer,0,length); } in.close(); outStream.close(); } }
The JSP page is this:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Download Servlet Test</title> </head> <body> Click on the link to download: <a href="DownloadServlet">Download Link</a> </body> </html>
I searched a lot of servlets but all of them were like this...they allowed downloading only a specific file. Can anyone help me? Thank you very much!