The project needs to download the import template, springboot use the following download method to solve the problem:
/**
* Import Template Download
* @param req
* @param resp
* @param context
* @return
*/
@RequestMapping (value = "Wsku/downloadfile", method = {requestmethod.post, requestmethod.get})
Public @ResponseBody void DownLoadFile (HttpServletRequest req,httpservletresponse resp) {
String filedownname = "Price List-template. xlsx";
Logger.debug ("Download template file name:" +filedownname);
try {
InputStream FIS = WSkuController.class.getResourceAsStream ("/exceltemplet/pricelisttemplate.xlsx");
byte[] buffer = new byte[fis.available ()];
Fis.read (buffer);
Fis.close ();
Response.reset ();
Response.setcontenttype ("bin");
String fileNames = filedownname;
String Agent = Request.getheader ("user-agent");
String codedfilename = "";
if (null! = Agent &&-1! = Agent.indexof ("MSIE") | | | null! = Agent &&-1! = Agent.indexof ("Trident")) {//I E
String name = Java.net.URLEncoder.encode (FileNames, "UTF8");
Codedfilename = name;
} else if (null! = Agent &&-1! = Agent.indexof ("Mozilla")) {//Firefox, Chrome, etc.
Codedfilename = new String (filenames.getbytes ("UTF-8"), "iso-8859-1");
}
Response.AddHeader ("Content-disposition", "attachment; Filename=\ "" + codedfilename + "\" ");
Response.getoutputstream (). write (buffer);
} catch (IOException e) {
E.printstacktrace ();
Logger.debug ("Download price list-template file Error" +e.getmessage (), E);
}
}
Where the file is stored under the following path:/imfbp-istorewares-admin/src/main/resources/exceltemplet/pricelisttemplate.xlsx
Springboot Download template file load classpath file path