Servlet code to download text file from website – Java
Here’s a servlet code example to download a text file from a website.
For example
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” .
1. Create a text file named “testing.txt” , put it into the project root folder.
\--servlet (project root folder) \--testing.txt (download file here) \--WEB-INF \--web.xml
2. Servlet code
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. Web deployment descriptor
<?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 to the Tomcat folder
\--Tomcat
\--webapps
\--servlet
\-- testing.txt (download file)
\--WEB-INF
\--web.xml
\--classes
\--com
\--mkyong
\--ServletDownloadDemo.class4. Done , access URL http://localhost:8080/servlet/DownloadDemo , it will prompts user to download the text file automatically.
You may interest this file download example in Struts 1.x
[...] http://www.mkyong.com/servlet/servlet-code-to-download-text-file-from-website-java/ [...]
Someone necessarily help to make critically articles I might state. That is the very first time I frequented your web page and to this point? I surprised with the research you made to create this actual publish amazing. Fantastic job!
I have a situation where i have created my PDF file in a servlet using bytearrayoutputstream. When run on its own the servlet does work and produces my PDF file, however, it is an instance where i need the file to be opened from a JSP on a button commmand. Is anyone familiar with how I would go about that?
i’m using jBoss server so can any one help me for that…
Servlet is standard, it should work in jboss.
Great article! Thanks a lot
Thanks for posting such a useful example.
Hello,
My name is Ming. I am a student at GSU. I find th eprogram very useful but I’d like to ask the author and other forum members a question regarding download a PDF file to my pc. I’d like to know if this program works for download PDF file to my pc ? Thanks in advance.
yes, the concept is apply to any file type, but you may need just change the “response.setContentType(“text/plain”);” to “application/octet-stream” or other content type to suit your need.
Thank you, for the note. If the text file have space padding between the text then if we use “text/plain” content type and download a file,all the space padding reduced to single space. And if we use “application/octet-stream” then space padding will remain intact.
really good information thanks admin
Thanks Nice Post. I added this thread to favorites.
what is package com.mkyong? and where can i download it?
can it used to open a web browser with java program?
thanks
1. “package com.mkyong” is just the source code packaging structure, refer to http://java.sun.com/docs/books/jls/third_edition/html/packages.html
2. To open a web browser, pls refer to this example
http://www.mkyong.com/java/open-browser-in-java-windows-or-linux/
Thanks for posting such a useful example.
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 [...]