Copy and paste the tragedy ---- spring implements file download and HttpStatus. CREATED,

Source: Internet
Author: User

Copy and paste the tragedy ---- spring implements file download and HttpStatus. CREATED,

Today, I was so miserable by my laziness and copy and paste it...

There is a best practice for downloading files from spring on the Internet:

@RequestMapping("download")  public ResponseEntity<byte[]> download() throws IOException {      HttpHeaders headers = new HttpHeaders();      headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);      headers.setContentDispositionFormData("attachment", "xxx.txt");      return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(getDictionaryFile()),                                        headers, HttpStatus.CREATED);  }

The returned status is HttpStatus. CREATED (201). At that time, I took it for granted that since the file is downloaded, it must be a new one, so create has nothing to do with it, so I did not further look at the significance of the 201 status code. then, this code is actually executed on Google and Firefox, so it has always been copied, pasted, copied, and pasted... today, I found that this code is actually problematic on ie.

Then, after one afternoon and one night, I thought it was a MediaType problem, but n methods were still fruitless. because I know that HttpStatus is returned here. OK is also acceptable, so it's just a matter of all the numbness attempts to replace it with OK (200), and the result is really good under ie. this reminds you to carefully query HttpStatus. the meaning of CREATED (201) is as follows:

The server has created a document and the Location header provides its URL.

The reference address given in the source code is no longer accessible. Therefore, you can only use a simple Baidu http status code. The above is only one of the explanations, but it generally means that the server has already created a document, this actually has nothing to do with the downloaded file ..

Therefore, the correct statement should be:

@RequestMapping("download")  public ResponseEntity<byte[]> download() throws IOException {      HttpHeaders headers = new HttpHeaders();      headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);      headers.setContentDispositionFormData("attachment", "xxx.txt");      return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(getDictionaryFile()),                                        headers, HttpStatus.OK);  }

 

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.