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.