How to download file from website- Java / Jsp

Here i show a simple java example to demonstrate how to let user download a file from website. No matter you are using struts , JSP, Spring or whatever other java framework, the logic is same.

1) First we have to set HttpServletResponse response to tell browser about system going to return an application file instead of normal html page

response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition",
"attachment;filename=downloadfilename.csv");

we can also specified a download file name in attachment;filename=, above example export a csv file name “downloadfilename.csv” for user download.

2) There have 2 ways to let user download a file from website

Read file from physical location

File file = new File("C:\\temp\\downloadfilename.csv");
FileInputStream fileIn = new FileInputStream(file);
ServletOutputStream out = response.getOutputStream();
 
byte[] outputByte = new byte[4096];
//copy binary contect to output stream
while(fileIn.read(outputByte, 0, 4096) != -1)
{
	out.write(outputByte, 0, 4096);
}
fileIn.close();
out.flush();
out.close();

Export database data or string directly to InputStream for user download.

StringBuffer sb = new StringBuffer("whatever string you like");
InputStream in = new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
ServletOutputStream out = response.getOutputStream();
 
byte[] outputByte = new byte[4096];
//copy binary contect to output stream
while(in.read(outputByte, 0, 4096) != -1)
{
	out.write(outputByte, 0, 4096);
}
in.close();
out.flush();
out.close();

3) Done

Here i show my struts example to demonstrate how to directly write data into InputStream and output it as “temp.cvs” to let user download.

public ActionForward export(ActionMapping mapping, ActionForm form,
	HttpServletRequest request, HttpServletResponse response)
	throws Exception {
 
	//tell browser program going to return an application file 
        //instead of html page
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition","attachment;filename=temp.csv");
 
	try 
	{
		ServletOutputStream out = response.getOutputStream();
		StringBuffer sb = generateCsvFileBuffer();
 
		InputStream in = 
                    new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
 
		byte[] outputByte = new byte[4096];
		//copy binary contect to output stream
		while(in.read(outputByte, 0, 4096) != -1)
		{
			out.write(outputByte, 0, 4096);
		}
		in.close();
		out.flush();
		out.close();
 
	  }
	  return null;
	}
 
private static StringBuffer generateCsvFileBuffer()
{
	StringBuffer writer = new StringBuffer();
 
	writer.append("DisplayName");
	writer.append(',');
	writer.append("Age");
	writer.append(',');
	writer.append("HandPhone");
	writer.append('\n');
 
        writer.append("mkyong");
	writer.append(',');
	writer.append("26");
	writer.append(',');
	writer.append("0123456789");
	writer.append('\n');
 
	return writer;
}

Here’s a file download example in Servlet code

Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • Pingback: kangen water machine()

  • Pingback: streaming movies()

  • Pingback: Google()

  • Pingback: Blue Coaster33()

  • Pingback: us?ugi szklarskie piotrków()

  • KyungHwan Min

    I just want to leave one comment. If you download xlsx Excel file, writing codes in JSP will result in file corruption. Writing download logic in servlet is working from my experiment.

    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

    response.setContentType(“text/html”);
    String filename = request.getParameter(“filename”);
    //System.out.println(“Filename = ” + filename);
    response.setContentType(“application/octet-stream”);
    response.setHeader(“Content-Disposition”,”attachment;filename=”+filename);
    String basePath = “/root/sub_path”;
    File file= new File(basePath+”/”+filename);
    System.out.println(“Filename = ” + basePath+”/”+filename);
    FileInputStream fileIn = new FileInputStream(file);
    ServletOutputStream sos = response.getOutputStream();

    byte[] outputByte = new byte[4096];

    while(fileIn.read(outputByte, 0, 4096) != -1 )
    {
    sos.write(outputByte , 0, 4096);
    }
    fileIn.close();
    sos.flush();
    sos.close();
    }

  • Ahmed

    Hallo,

    Can you explain how can i download files with Apache Wicket?

    Thanx!

  • Soheb
  • Soheb

    I am using JSF and in one of the managed bean (view scope) i have a method as:

    public String viewReport() {

    HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();

    response.setContentType(“application/vnd.ms-excel”);
    response.setHeader(“Content-Disposition”,”attachment;filename=book1.xls”);

    try {
    File file = new File(“C:\\soheb\\book1.xls”);
    FileInputStream fileIn = new FileInputStream(file);
    ServletOutputStream out = response.getOutputStream();

    byte[] outputByte = new byte[4096];
    //copy binary contect to output stream
    while(fileIn.read(outputByte, 0, 4096) != -1)
    {
    out.write(outputByte, 0, 4096);
    }
    fileIn.close();
    out.flush();
    out.close();
    }
    catch(IOException e) {
    e.printStackTrace();
    }
    return null;
    }

    The excel which is getting retrieved is corrupted file. It has some strange characters above and below is the entire code of JSP. I even tried application/octet-stream for contenttype too.

    I tried the same with a plain text file and i was able to open it through.

    Please help me with this problem, Thanks in advance.

  • sudha

    your website is so useful

  • http://www.movie4free.in sunil yadav

    best site for technical interview question………………….

  • Sudeep Nayak

    The root post worked for me. And it was really helpful.
    I just lost 3 days to download a zip file.

    You code really rocks.

    Thanks and keep sharing.

  • http://Cyomr.com.tw/vlbook/index.php _

    Whats up are using WordPress for your site platform? I’m new to the blog world but I’m trying to get started and create my own.
    Do you need any html coding expertise to make your own blog?
    Any help would be really appreciated!

  • Pingback: Struts 2 download file example()

  • Jia Bin

    Hi, is this possible to do this in struts 2? Thanks…

  • Enrico Bergamo

    Hello, I’m having the same problem as Nayana here. My files are stored in a BLOB field in DB2. I’m trying to retrieve them, but I only have success for text files, but we need to retrieve DOC, PDF and XLS as well.

    Considering they are stored base64 encoded, I’m using the following code to download the file, but as I said before, just worked on text files

     
     
    <%@ page import="java.io.*"%>
    <%@ page import="com.ibm.misc.*"%>
     
    <%
    	String fileName = request.getParameter("fileName");
    	String fileType = request.getParameter("fileType");
    	String fileContent = b64Decode(request.getParameter("fileContent"));//Blob content
     
    	response.setContentType(fileType);
    	//response.setContentType("application/octet-stream");
    	response.setHeader("Content-Disposition", "attachment;filename=\""
    			+ fileName + "\"");
    	response.setContentLength((int) fileContent.length());
     
    	try {
     
    		StringBuffer sb = new StringBuffer(fileContent);
    		InputStream in = new ByteArrayInputStream(sb.toString().getBytes());
    		//InputStream in = new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
    		ServletOutputStream sos = response.getOutputStream();
     
    		byte[] outputByte = new byte[4096];
    		//copy binary contect to output stream
    		while (in.read(outputByte, 0, 4096) != -1) {
    			sos.write(outputByte, 0, 4096);
    		}
    		in.close();
    		sos.flush();
    		sos.close();
     
    	} catch (IOException ex) {
    		ex.printStackTrace();
    	}
    %>
     
    <%!public String b64Decode(String msg) {
    		BASE64Decoder decoder = new BASE64Decoder();
    		byte[] decodedBytes = null;
    		try {
    			decodedBytes = decoder.decodeBuffer(msg);
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		return new String(decodedBytes);
    	}%>
    • sara

      hello sir,
      i want clear code for downloading files in jsp

    • http://www.mkyong.com/java/how-to-download-file-from-website-java-jsp/ Rodney

      Oh This Works All Fine, But I Want To Download A Password Protected File From The Net Like In Firefox When The URL is Entered It Prompts A Password Box To Download The File.

      With This Code So Far I Get Errors As The Web Host Gives Access Denied.

      ¿ I Want [Yeah I Demand] A Code in That if The File [URL] is Password Protected It Will Tell You To Enter The Password And Then Download The File if Password is Correct?

      Thank-You BTW

  • Nayana

    I have stored excel file as BLOB. On click of filename in the JSP user should be able to retrieve the BLOB and display it in the excel. Could you please help me here?

    • http://sexydelhiescorts.in Anamika

      I am also looking to learn java can you share any website where I can learn it properly.

  • Gabe T.

    Thank you so very very much for sharing your solution online :D!
    Exactly what I was looking for.
    Much appreciated, sir :)!

  • Pingback: Programming anxiety()

  • Nikhil

    Hi,

    I am using

    response.setContentType(“application/octet-stream”);
    response.setHeader(“Content-Disposition”, “attachment;filename=” + outfile.getName());

    It ends up downloading my jsp page in test format.

    Please help.

    Thanks & Regards,
    Nikhil

    • webcom

      response.setContentType(“application/octet-stream”);
      response.setHeader(“Content-Disposition”, “attachment;filename=” + outfile.getName());

      The great thing i discovered is you actually dont need to specify the application type in order to open downloaded file. Since outfile.getName() parameter contains the file extension (myfile.txt, myfile.doc…), any file type can be downloaded with this Content type declaration
      I tried this with Word, PDF, XLS, JPG, MPG, ZIP they all work with “application/octet-strea” with no problem

      • http://pornmovies69.com Sunaina

        I want to learn java please tell me any website where I can learn it.

  • Tomino

    Hi,

    I did it this way. It works great in Mozilla and IE7 and bellow. But same code doesn’t work in IE8. When executing out.flush() or out.close(), the browser gets closed with no exception thrown. Does anybody have an idea what is wrong?

    I tried content type “application/octet-stream”, “application/force-download” as well as “application/vnd.ms-excel”, cause I download an excel file.

  • Shashank

    Hi,

    I modified the above code to download a word document. Below are the changes –

    response.setContentType(“application/doc”);
    response.setHeader(“Content-disposition”,”attachment; filename=Wordfile.doc”);

    The document downloads fine. But when I open the document in MSWord, I get the below error –

    “Word Cannot Start the Converter MSWRD632.WPC”

    After clicking the OK button a couple of times the documents opens up fine. Please let me how to fix this issue.

  • Roshan

    Thanks very much… you save my time…you have release my stress..Thanks again..god bless u..

  • Pochacco

    Hi, I’m a little confused as to where I put these codes into.
    Sorry I’m not so good with html stuff.

  • http://boxesandarrows.com/person/298004-jamieholi882 Sibyl Cocoran

    hi-ya, great post.

  • http://www.downloadmela.com mahesh

    very useful content dude

  • Haja

    while(in.read(outputByte, 0, 4096) != -1)
    {
    out.write(outputByte, 0, 4096);
    }

    change this code to

    int byteRead;
    while((byteRead = in.read(outputByte, 0, 4096)) != -1)
    {
    out.write(outputByte, 0, byteRead );
    }

    • http://www.mkyong.com mkyong

      Thanks for sharing your tips :)

      • Von Landon

        Please update the code at the top of this page to reflect this important fix.

    • Onymakris

      Great article. Thanks.

    • Gachugu

      Excellent! And this addition was very nice too… otherwise the download appends bytes to make the chunk 4096! Good work guys.

  • Pingback: Servlet code to download the text file – Java | Servlet()

  • Ronnie

    From my web layer i pass an id value, then a type value(string), and two ate values….
    Actually i useed an Http service and sent these parameters using send() method to the EJB…

  • mkyong

    From your reply, it’s look like you are using adobe flex. Sorry, i have no experience in flex, may be there have a flex’s way to do it. Please try find in Google, here are few examples in general java web application, hope it help.

    General Java Serlvet
    ————————
    http://www.java2s.com/Code/Java/Servlets/GetoutputstreamfromHttpServletResponse.htm

    Struts example
    —————–
    http://www.allapplabs.com/struts/struts_example.htm

    • Ronnie

      Thx Yong :)

  • mkyong

    The code look like a service layer, please get the HttpServletResponse object from web layer and pass it to your service layer, every web application must contains HttpServletResponse object.

    • Ronnie

      Thx Yong :)
      But cud u plz tel me hw to pass the response object from web layer? Can you explain it briefly using an example??

    • http://www.google.com/ Marlie

      Superior thinking denmsortated above. Thanks!

  • Ronnie

    When i use response.setContentType statement ,
    it says response has not been initialized….

  • Ronnie

    I have changed the writer to StringBuffer Type..
    So tat is not the issue :)

  • Ronnie

    Hi,
    Sorry for the delay.
    Plz find my code below. It is givin an exception. This code is in a Session Bean. The input is coming from Flex. Iam able to generate the file, but not able to make it available as a downloadable file :(

    Could you plz help me out…
    Its kinda urgent…..

    Thx in Advance…
    Also i would appreciate if this code isnt made public….

    Also iam new to httprequest and response part…
    Plz enligten me on wat i shud be sending in the request, from wer i shud be sending the request and wat response shud hold….

    Code:

    public void exportPowerData(int id,String type,String from,String to)
    {

    ………………….

    String sFileName = “c:\\test.csv”;

    response.setContentType(“application/octet-stream”);
    response.setHeader(“Content-Disposition”,”attachment;filename=sFileName”);

    Date fromTimeStamp = stringToDate(from);
    Date toTimeStamp = stringToDate(to);

    System.out
    .println(“INFO:************* Executing EJB ********************** “);

    Session session = HibernateUtil.getSessionFactory().openSession();

    Criteria crit =
    session.createCriteria(Power.class);

    crit.add(Restrictions.between(“lastUpdatedOn”, new Date(fromTimeStamp.getTime()), new Date(toTimeStamp.getTime())));

    List list = crit.list();

    FileWriter writer = new FileWriter(sFileName);

    writer.append(“Dc_Equipment”);
    writer.append(‘,’);
    writer.append(“Equipment Type”);
    writer.append(‘,’);
    writer.append(“Last_UpdatedOn”);
    writer.append(‘,’);
    writer.append(“Power(In Watts)”);
    writer.append(‘\n’);

    for(Iterator it = list.iterator();it.hasNext();)
    {
    Power p = (Power)it.next();

    System.out.println(“Home: ” + p.getHome());
    System.out.println(“Type: ” + p.getType());
    System.out.println(“Last_Updated_On: ” + p.getLastUpdatedOn());
    System.out.println(“Power: ” + p.getValue());

    String str = p.getHome();
    String str1 = p.getType();
    String str2 = p.getLastUpdatedOn();
    String str3 = p.getValue();
    double d = Double.valueOf(str3).doubleValue();

    total_power = d+total_power;
    String dbl_to_str = Double.toString(d);

    writer.append(str);
    writer.append(‘,’);
    writer.append(str1);
    writer.append(‘,’);
    writer.append(str2);
    writer.append(‘,’);
    writer.append(dbl_to_str);
    writer.append(‘\n’);

    }

    System.out.println(“Total Power Consumed:” +total_power);
    String dbl_to_str1 = Double.toString(total_power);
    writer.append(“Total Power: “);
    writer.append(dbl_to_str1);
    writer.append(“\n”);

    FileInputStream fileIn = new FileInputStream(sFileName);
    ServletOutputStream out = response.getOutputStream();

    byte[] outputByte = new byte[4096];
    //copy binary content to output stream
    while(fileIn.read(outputByte, 0, 4096) != -1)
    {
    out.write(outputByte, 0, 4096);
    }
    fileIn.close();
    out.flush();
    out.close();

    //fileDownload(writer);

    writer.flush();
    writer.close();

    session.clear();
    transaction.commit();
    session.close();
    HibernateUtil.shutDown();

    //return null;

    }catch(Exception e){
    System.out.println(e.getMessage());

    }

    }

    public Date stringToDate(String value)
    {
    try
    {
    DateFormat formatter ;
    Date date ;
    formatter = new SimpleDateFormat(“yyyy-MM-dd”);
    date = (Date)formatter.parse(value);
    return date;

    } catch (ParseException e)
    {System.out.println(“Exception :”+e);
    return null;
    }

    }

    public static void main(String args[])
    {

    ExportDataEJB obj = new ExportDataEJB();

    obj.exportPowerData(1, “Home”, “2009-10-30″, “2009-11-06″);

    }

  • Ronnie

    Hi,
    Wat are these parameters (HttpServletRequest request, HttpServletResponse response) carrying? I tried to create an execute() function withe two parameters and then cal this function from main. But when i run it it is showin error.

    Code:

    public static void main(String[] args){

    execute(null, null);

    }

    public static void execute(HttpServletRequest request, HttpServletResponse response) {
    // TODO Auto-generated constructor stub

    response.setContentType(“application/octet-stream”);
    response.setHeader(“Content-Disposition”,”attachment;filename=temp.csv”);
    ……………………….

    i have errors at the function call and response.setContentType statement it says exception….

    • Ronnie

      when i run it, it says nullpointer exception….
      i know ther error is due to the null values tat im passing to execute method.
      But im not sure hw to overcome????

    • mkyong
      • Ronnie

        Im using Struts….
        But this part of the code is in a EJB session bean…

        • mkyong

          I’m not really familiar with either EJB2 nor EJB3,so not much comment on here, just get your HttpServletRequest in struts and pas to your EJB , it’s should be easily to get it, check your struts API documentation.

          If you want more detail, please send me your source code ;)

  • Ronnie

    I have a session bean wer i generate a file. Can i make it available for download? Can i use the same code in this session bean?

    • mkyong

      yes, the codes are pure java, no hidden secrets there :)

      • Ronnie

        Thx :)

  • Ronnie

    Hi All,
    Forgiv ma ignorance, but wat are these
    (ActionMapping mapping, ActionForm form) parameters for????

    • mkyong

      ActionMapping mapping and ActionForm form are example in struts

      • sravanthi

        Hi all,ActionMapping and ActionForm are predified classes in strunts, how can you say those are examples please tell me

        • http://www.mkyong.com mkyong

          Sorry, i means the above code snippet is a Strut example.

  • stan

    Thanks very much for the code. It works great for me.
    But I think there is a little bug in the binary download file code.
    If the file size is not a multiple of 4096, the last loop (at the end of the file) would carry over data from the previous loop and write it one more time in the output stream.

  • Mahi

    Hi All,
    theres is a requirement to export the csv file, file name contains the UTF-8 charecters..
    Pls help me

  • assman

    There is no need to split the string into bytes and then write it to the output stream. You should explain the reason you do this because it doesn’t make sense and its error prone.

    Instead of :

    InputStream in = new ByteArrayInputStream(sb.toString().getBytes(“UTF-8″));

    byte[] outputByte = new byte[4096];
    //copy binary contect to output stream
    while(in.read(outputByte, 0, 4096) != -1)
    {
    out.write(outputByte, 0, 4096);
    }

    Just do the following:

    out.print(sb);

    Much much better.

    • mkyong

      thanks for tips

      sorry for that, i’m just used to this binary copy method to copy anything~

    • http://dd yogesh

      nothing to reply

    • http://sexydelhiescorts.in Anamika

      l like this blog as it too useful and nice way to solve problems…

  • Praveen

    it was amazing…. you are genius thank you so much. whole concept was crystal clear..

    • mkyong

      hope help :)

      • Arun

        please help me to download files i tried the above codes not working using netbeans with apache as server when i try to download file it is downloaded in bytes not of its original size.

  • Mustak

    Hi,

    I want to download a file which is in another website and I can download it by going into that site and by clicking on download button. What I want is I don’t want to download it like this, instead I want a script which goes to that page and downloads the file and saves in the location which I have mentioned in the script. Also it should download the file in every 2 mins. Can you please help me in doing this?

    • mkyong

      you want a program to download a file from another website and save into your pc location? is this what you means? Yes, it is possible in java, java socket can achieve it.

      Howeber i will suggest you to use java HttpClient library and regular expression to extract the website content.

      • madhan

        i need to download a large file,
        for example 1GB file to download with in 1 minute

  • Pingback: How to download file from website- Java / Jsp()