Java file download code, personally think this is the simplest, just three steps, you can download.

Source: Internet
Author: User

Today to bring you the simplest way to download the file, we follow the steps, three steps can be downloaded.

1, the following is the main method of downloading files, you can directly copy to the project inside, add a jar package on it.

/** * Download file public method, direct call can download * @param pathurl The path of the file to download @param name of the file after download, because many files in the upload, are changed the name, here can be repaired Change back * @return/public httpservletresponse downloadFile (/*httpservletresponse response,httpservletrequest requ  Est,*/string Pathurl, String name) {try {//) Here gets a response object, if you don't know how to get it, you can also put HttpServletResponse  
		  
	    Response is passed as a parameter to this method and can be used directly httpservletresponse response = GetResponse ();  
	    if (Pathurl = null) && ("". Equals (Pathurl)) {return null;  
	  
	    File File = new file (Pathurl); This gets the absolute path of the server. 
	    If it is not used, the method that is commented out below can be String FilePath = Getservletcontext (). Getrealpath ("/"); Before using the following method, you need to pass HttpServletRequest request as a parameter to this method and use it directly//string FilePath = Request.getsession (). 
	  
	    Getservletcontext (). Getrealpath ("/");  
	  
	    InputStream fis = new Bufferedinputstream (new FileInputStream (FilePath + pathurl));  
	byte[] buffer = new byte[fis.available ()];  
	    Fis.read (buffer);  
	  
	    Fis.close ();  
	  
	    Response.reset ();  
	  
	    OutputStream toclient = new Bufferedoutputstream (Response.getoutputstream ());  
	  
	    Response.AddHeader ("Content-disposition", "attachment;filename=" + New String (Name.getbytes (), "iso-8859-1"));  
	  
	    Toclient.write (buffer);  
	  
	    Toclient.flush ();  
	  
	    Toclient.close ();  
	    Response.setcontenttype ("Application/octet-stream");
	    Here's the AddHeader method, if the error, please use the comment off the bottom of the method. 
	    Response.AddHeader ("Content-length", File.length ());
	  Change the second argument to String type//response.addheader ("Content-length", String.valueof (File.length ()));  
	  catch (IOException ex) {ex.printstacktrace ();  
	return null;   }


2, the first step is only a method, is generally used for encapsulation. We need to go to the Controller or action when downloading the following example:

@RequestMapping (value= "/downloadexcel.do")
	@ResponseBody public
	void Downloadexcel (/*httpservletresponse Response,httpservletrequest request,*/@Param ("Pathurl") String Pathurl) 
			throws exception{DownloadFile
		
		(/* HttpServletResponse response,httpservletrequest Request,*/pathurl, "test.xlsx");//This is the way to invoke the download in this step,
		
	}



3, the final step, you need to add a label on the top of the page, call just action or controller, you can achieve the download

<li> Excel template download: <a href= "downloadexcel.do?pathurl=test.xlsx" style= "color:red;" target= "_blank" > click here </a></li>

OK, workpiece, you click on the Download button on the page to see if you can download it. is the name of the file after the download changed to the name you want? If you succeed, just a little bit of praise.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.