About Spingmvc implementing file downloads you don't know.

Source: Internet
Author: User

File upload download generally in the project is still very practical, here specifically to organize the download of the file, as for the upload implementation of the file will be added in the following. The download of the file is used for the download of the template file, which is very much used in the project. Today the use of the sorting out, for the Porter for reference and use, has been verified error-proof.

(1) Front Office implementation

The front-end implementation is very simple, not as complex as file uploads, as long as a hyperlink is given, linked to the background processing method, and the file name needs to be passed to the controller.

(2) Background processing

The controller in the background mainly deals with the following issues:

① find the template file based on the file name

② setting the form of the response, file header, encoding

③ reads the contents of the template file and writes it to the output stream in the form of a stream

④ closing the input and output stream

(3) The following we follow the implementation of the background of the foreground to see the implementation of the Code:

① Front Desk:

<a href= "${base}/downloadtemplate?filename=abilitytemplate.xlsx" > Template download </a>

② Backstage:

@RequestMapping (value = "/downloadtemplate", method = Requestmethod. GET)

public string Downloadabilitytemplate (string filename,httpservletrequest request,httpservletresponse response) {

Response.setcharacterencoding ("Utf-8");//Set encoding

Response.setcontenttype ("Multipart/form-data");//set type

Response.setheader ("Content-disposition", "attachment;filename=" + fileName); Setting the response header

try {

String FilePath = Config. GetValue ("path");

Gets the directory where the template file is located in the configuration file

String path = Request.getsession (). Getservletcontext (). Getrealpath ("/") +filepath;

Gets the relative directory of the template file

InputStream InputStream = new FileInputStream (The new file (path+ file). Separator + fileName);

OutputStream OS = Response.getoutputstream ();

Byte[] B = new byte[2048];

int length;

while (length = Inputstream.read (b)) > 0) {

Os.write (b, 0, length);

}//side read template file side write output stream

Os.close ();

Inputstream.close ();//Mathematic

} catch (FileNotFoundException e) {

E.printstacktrace ();

} catch (IOException e) {

E.printstacktrace ();

}

return null; Note that at this point return null

}

(4) Attention points:

① return the model layer should be return NULL, otherwise the following error occurs:

Java+getoutputstream () have already been called for this response

The location of the ② template file can be stored as needed, as long as the full path to the file can be obtained in the background.

Put in the class directory to get is:

Get the path to classes

String path = Thread.CurrentThread (). Getcontextclassloader (). GetResource (""). GetPath ();

Get the Webroot directory:

String path = Request.getsession (). Getservletcontext (). Getrealpath ("/")

About Spingmvc implementing file downloads you don't know.

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.