Java fooling around notes: Downloading files with spring's responseentity

Source: Internet
Author: User

This time to note the new download file method, before are InputStream and OutputStream, a little trouble, today in the project found in a method, and then to the spring website to see, on the whole of a most simple piece of code out, directly on the code ...

Fileutils is in the Commons-io package ...

@RequestMapping ("/t")
@ResponseBody
Public responseentity<byte[]> T () throws IOException {

	file File = new file ("F:" + file.separator + "1.png"); You put the file path//New String ("Online consumption record". GetBytes ("GBK"), "iso-8859-1")
	httpheaders headers = new Httpheaders ();//Set a head
	headers.setcontentdispositionformdata ("Attachment", "1.png");//file properties, which is what the file is called
	Headers.setcontenttype ( Mediatype.application_octet_stream);/content is byte stream return
	new responseentity<byte[]> ( Fileutils.readfiletobytearray (file), headers, httpstatus.ok);//Start Download
}

OK, it is so simple, the speed of hemp, has not been tested ... Need to be compared

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.