Java implementation downloads any format file

Source: Internet
Author: User

The first step is to write an HTML file of your own (write the file template according to your own needs)

 

The second part uses STRUTS2 as action


	Public String Downloadfiles () {try {httpservletrequest request = Servletactioncontext.getrequest ();
			HttpServletResponse response = Servletactioncontext.getresponse ();

			Solve the Chinese garbled problem Response.setcontenttype ("Application/x-download;charset=utf-8"); /** Read template file content/FileInputStream input = new FileInputStream (Request. Getrealpath ("") + "/web-inf/tem
			P.html "));
			int bytelength = input.available ();
			String title = "Dream of the Three Kingdoms download any format file"; String content = "Test .... ... ..... ..... ..... ...... ...... ...
			";

			String editer = "Three faithful fans of the Dream";
			byte[] bytes = new Byte[bytelength];
			Input.read (bytes);
			Input.close ();
			String templatecontent = new string (bytes);

			System.out.println ("template file content = = =" + TemplateContent);
			TemplateContent = Templatecontent.replaceall ("# # #title ###", title);
			TemplateContent = Templatecontent.replaceall ("# # #content ###", content); TemplateContent = TemplateContent. ReplaceAll ("# # #author ###", editer);//replace the corresponding place in the template System.ouT.print ("Replaced file contents" + TemplateContent); String name = "Three Kingdoms of Dreams. html";//download file name byte[] outputcontent = templatecontent.getbytes ()//download file contents Response.setcontentty
			PE ("Application/x-msdownload");
			Response.setheader ("Content-disposition", "attachment;filename=" + New String (Name.getbytes ("GBK"), "iso-8859-1")); Response.getoutputstream (). write (outputcontent);
			Write file Response.getoutputstream (). Flush ();
		Response.getoutputstream (). Close ();
		catch (FileNotFoundException e) {//TODO auto-generated catch block E.printstacktrace ();
		catch (Unsupportedencodingexception e) {//TODO auto-generated catch block E.printstacktrace ();
		catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
	return null; }

Finally we can download the file by accessing the action

The above information is expected to help you.





Related Article

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.