Recently, the project encountered a series of pictures need to download the package needs, drawing on some of the common methods of online, on the way to share out the method to achieve, I do not remember learn who is calling brothers blog, in short, very grateful , that is entered, the basic functions package to download:
1.controller level code:
/**
* Image compression packing
*/
@RequestMapping(value = "/zipFile")
public void compressionFile(HttpServletRequest request, HttpServletResponse response,String busiId) throws Exception{
// business code, according to the front desk came the query ID to the resource table picture list
SubMetaData subMetaData = subMetaDataService.findByBusiId (busiId);
if (subMetaData != null) {
List<SubMetaDataAtt> list = subMetaDataAttService.findByDataId(subMetaData.getDataId());
if (list.size() > 0){
subMetaDataAttService.downloadAllFile(request,response,list);
}
}
}
2.service layer common file download package
/**
* Multiple file compression package, to solve the garbage problem after downloading the file name
*
*/
public void downloadAllFile(HttpServletRequest request, HttpServletResponse response, List<SubMetaDataAtt> list) throws UnsupportedEncodingException{
String downloadName = "attached images .zip";
String userAgent = request.getHeader("User-Agent");
// for IE or IE as the core browser:
if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
downloadName = java.net.URLEncoder.encode(downloadName, "UTF-8");
} else {
// handle non-IE browser:
downloadName = new String(downloadName.getBytes("UTF-8"), "ISO-8859-1");
}
// After the above process name to solve the garbage problem after downloading the file name
response.setContentType("multipart/form-data");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", downloadName));
//response.setHeader("Content-Disposition", "attachment;fileName=" + downloadName);
OutputStream outputStream = null;
ZipOutputStream zos = null;
try {
outputStream = response.getOutputStream();
zos = new ZipOutputStream(outputStream);
// stream writing zip file, the method below posted
downloadTolocal(zos,list);
} catch (IOException e) {
logger.error ( "downloadAllFile- download all attachments failed", e);
}finally {
if(zos != null) {
try {
zos.close();
} catch (Exception e2) {
logger.info ( "Error occurred while closing the input stream", e2);
}
}
if(outputStream != null) {
try {
outputStream.close();
} catch (Exception e2) {
logger.info ( "Error occurred while closing the input stream", e2);
}
}
}
}
3. js request reception method:
Note: Do not download files to use AJAX request, so is unable to respond to the request, generally using Window.open approach.
window.open (context + "/ Sub / submetadataatt / the ZipFile busiId =?" + downloadId); // here downloadId is a variable I need to spread the background.
Summary: Operating on upload, download, in fact, be very familiar with java's IO, before they can play the turn, we must grasp the foundation can navigate in the project, not something I need to learn from others, we Working together, come on!
Detailed configuration information can refer to the article I wrote: http://blog.ncmem.com/wordpress/2019/08/28/java%e6%89%b9%e9%87%8f%e4%b8%8b % e8% bd% bd /