Servlet code to download text file from website – Java
Here’s a servlet code example to download a text file from a website.
Download Request
Let’s say a text file named “testing.txt” , and you want to let user download it with a URL , for example “http://localhost:8080/servlet/DownloadDemo” .
Solution
1) Create a text file named “testing.txt” , put in project root folder
\--servlet (project root folder) \--testing.txt (download file here) \--WEB-INF \--web.xml
2) Servlet code as follow
package com.mkyong; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.servlet.ServletContext; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletDownloadDemo extends HttpServlet{ private static final int BYTES_DOWNLOAD = 1024; public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{ response.setContentType("text/plain"); response.setHeader("Content-Disposition", "attachment;filename=downloadname.txt"); ServletContext ctx = getServletContext(); InputStream is = ctx.getResourceAsStream("/testing.txt"); int read=0; byte[] bytes = new byte[BYTES_DOWNLOAD]; OutputStream os = response.getOutputStream(); while((read = is.read(bytes))!= -1){ os.write(bytes, 0, read); } os.flush(); os.close(); } }
The “getResourceAsStream()” method with a forward slash (“/”), which represent the root of your web application.
3) Deployment descriptor as follow
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>serlvetdemo</display-name> <servlet> <servlet-name>ServletName</servlet-name> <servlet-class>com.mkyong.ServletDownloadDemo</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletName</servlet-name> <url-pattern>/DownloadDemo</url-pattern> </servlet-mapping> </web-app>
4) Compiled it and copy it to the following Tomcat folder
\--Tomcat
\--webapps
\--servlet
\-- testing.txt (download file)
\--WEB-INF
\--web.xml
\--classes
\--com
\--mkyong
\--ServletDownloadDemo.class4) Done , access http://localhost:8080/servlet/DownloadDemo to download the text file
Here’s another file download example in Struts
- Java Core Technology - Java RegEx, Java XML, Java I/O, Java Misc
- J2EE Frameworks - Hibernate, Spring 2.5, Spring MVC, Struts 1.x, Struts 2.x
- Build Tools - Maven, Archiva
- Unit Test - jUnit, TestNG
- Client Scripts - jQuery
Yor this code is not working for me it just creates the file but the contents are not downloading……
Plz reply as soon as possible
Thanks in Adv.
The code is well tested, why not sharing your code, may be something wrong in your code?
Websites RSS feed is not work in my browser (google chrome) how can I mend it?
[...] Here’s a file download example in Servlet code [...]