Home >Web Front-end >HTML Tutorial >java web package download of multiple images, prompt box pops up_html/css_WEB-ITnose
In the spring mvc framework, when the front-end ajax call method is used to download images in batches, how to pop up the save location selection box?
Controller method
@RequestMapping("/tcdl")
public ModelAndView dlCode(HttpServletRequest request,HttpServletResponse response,@RequestParam(value="ids") String ids,@RequestParam( value = "funId", required = false) Integer funId) throws IOException{
ModelAndView mav = new ModelAndView();
//response.setHeader("Charset", "UTF-8");
//response.setContentType("text/html; charset=utf-8");
List
String path = request.getSession().getServletContext( ).getRealPath("");
String[] trims = ids.split(",");
String type=""; //File format suffix
for(int i=0;i< ;trims.length;i ){
Operator oper = opService.getOperatorById(Integer.parseInt(trims[i]));
if(!"".equals(oper.getOpCardUrl())){
File f = new File(path oper.getOpCodeUrl());
/*int k = oper.getOpCodeUrl().indexOf("other"); ///userPic/3/other/2014011617535382684910.png
int j =0;
while (j != -1) {
j = oper.getOpCodeUrl().indexOf(".");
type = type.substring(j 1);
}
File f = new File(path oper.getOpCodeUrl());
String newName = oper.getOpCodeUrl().substring(0,k 1) oper.getOpUserName() oper.getOpMobile() type ;
System.out.println(newName ",newName");*/
if(f.exists()){
// f.renameTo(new File(path oper.getOpCodeUrl(). substring(0,k 1) newName));
fileList.add(f);
}
}
}
String fileName = "twoCodeDown" ".zip";
/**Create a temporary file for packaging and downloading on the server side*/
File f = new File(path "/tmp");
if(!f.exists()){
f.mkdirs();
}
String outFilePath = path "/tmp/" fileName;
File file = new File(outFilePath);
/**file output stream*/
FileOutputStream outPutStream = new FileOutputStream(file);
/**Compressed stream*/
ZipOutputStream toClient = new ZipOutputStream(outPutStream);
//this.downloadZip(file);
/**compressed file*/
opService. downloadZip(fileList,toClient);
/**Download zip*/
opService.downloadZip(file, response);
return mav;
}
Download method
**
* Download the packaged file
* @throws IOException
* */
public void downloadZip(File file,HttpServletResponse response) throws IOException{
/**Download files as streams*/
try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file.getPath()));
FileOutputStream outStream = new FileOutputStream(file.getPath());
byte[] buffer = new byte[ bis.available()];
bis.read(buffer);
bis.close();
OutputStream toClient = new BufferedOutputStream(outStream);
response.setContentType("application/x- download");
response.setHeader("Content-Disposition", "attachment;filename=" file.getName());
toClient.write(buffer);
toClient.flush();
toClient.close();
file.delete(); //Delete the generated server-side file
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
You are using ajax to request, and the ajax request will not pop up a prompt to save. Position selection box. It is recommended that you use js or jquery to dynamically create a form for submission. Remember to add
response.setHeader("Content-Disposition", "attachment;filename=" imageName);.