Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing a simple file download servlet [duplicate]

How should I implement simple file download servlet?

The idea is that with the GET request index.jsp?filename=file.txt, the user can download for example. file.txt from the file servlet and the file servlet would upload that file to user.

I am able to get the file, but how can I implement file download?

like image 827
newbie Avatar asked Sep 18 '09 06:09

newbie


People also ask

Can we have multiple servlets in a project?

So the short answer is, you will create many servlets per webapp since each webapp will expose several use cases.


2 Answers

Assuming you have access to servlet as below

http://localhost:8080/myapp/download?id=7 

I need to create a servlet and register it to web.xml

web.xml

<servlet>      <servlet-name>DownloadServlet</servlet-name>      <servlet-class>com.myapp.servlet.DownloadServlet</servlet-class> </servlet> <servlet-mapping>      <servlet-name>DownloadServlet</servlet-name>      <url-pattern>/download</url-pattern> </servlet-mapping> 

DownloadServlet.java

public class DownloadServlet extends HttpServlet {       protected void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {           String id = request.getParameter("id");           String fileName = "";          String fileType = "";          // Find this file id in database to get file name, and file type           // You must tell the browser the file type you are going to send          // for example application/pdf, text/plain, text/html, image/jpg          response.setContentType(fileType);           // Make sure to show the download dialog          response.setHeader("Content-disposition","attachment; filename=yourcustomfilename.pdf");           // Assume file name is retrieved from database          // For example D:\\file\\test.pdf           File my_file = new File(fileName);           // This should send the file to browser          OutputStream out = response.getOutputStream();          FileInputStream in = new FileInputStream(my_file);          byte[] buffer = new byte[4096];          int length;          while ((length = in.read(buffer)) > 0){             out.write(buffer, 0, length);          }          in.close();          out.flush();     } } 
like image 138
Ali Irawan Avatar answered Sep 23 '22 09:09

Ali Irawan


That depends. If said file is publicly available via your HTTP server or servlet container you can simply redirect to via response.sendRedirect().

If it's not, you'll need to manually copy it to response output stream:

OutputStream out = response.getOutputStream(); FileInputStream in = new FileInputStream(my_file); byte[] buffer = new byte[4096]; int length; while ((length = in.read(buffer)) > 0){     out.write(buffer, 0, length); } in.close(); out.flush(); 

You'll need to handle the appropriate exceptions, of course.

like image 43
ChssPly76 Avatar answered Sep 23 '22 09:09

ChssPly76