java web 多图片打包下载,弹出提示框问题_html/css_WEB-ITnose
程序员文章站
2022-06-07 21:55:54
...
在spring mvc框架中,前台ajax调用方法,对图片批量下载,如何弹出提示保存位置选框?
Controller方法
@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 fileList = new ArrayList();
String path = request.getSession().getServletContext().getRealPath("");
String[] trims = ids.split(",");
String type=""; //文件格式后缀
for(int i=0;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";
/**在服务器端创建打包下载的临时文件*/
File f = new File(path+"/tmp");
if(!f.exists()){
f.mkdirs();
}
String outFilePath = path+"/tmp/"+fileName;
File file = new File(outFilePath);
/**文件输出流*/
FileOutputStream outPutStream = new FileOutputStream(file);
/**压缩流*/
ZipOutputStream toClient = new ZipOutputStream(outPutStream);
//this.downloadZip(file);
/**压缩文件*/
opService.downloadZip(fileList,toClient);
/**下载压缩*/
opService.downloadZip(file, response);
return mav;
}
下载方法
**
* 下载打包的文件
* @throws IOException
* */
public void downloadZip(File file,HttpServletResponse response) throws IOException{
/**依流的形式下载文件*/
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(); //将生成的服务器端文件删除
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
Controller方法
@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=""; //文件格式后缀
for(int i=0;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";
/**在服务器端创建打包下载的临时文件*/
File f = new File(path+"/tmp");
if(!f.exists()){
f.mkdirs();
}
String outFilePath = path+"/tmp/"+fileName;
File file = new File(outFilePath);
/**文件输出流*/
FileOutputStream outPutStream = new FileOutputStream(file);
/**压缩流*/
ZipOutputStream toClient = new ZipOutputStream(outPutStream);
//this.downloadZip(file);
/**压缩文件*/
opService.downloadZip(fileList,toClient);
/**下载压缩*/
opService.downloadZip(file, response);
return mav;
}
下载方法
**
* 下载打包的文件
* @throws IOException
* */
public void downloadZip(File file,HttpServletResponse response) throws IOException{
/**依流的形式下载文件*/
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(); //将生成的服务器端文件删除
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
回复讨论(解决方案)
你用的是ajax来请求的,ajax请求是不会弹出提示保存位置选框的。建议你用js或jquery动态的创建form表单来提交,记得要加上
response.setHeader("Content-Disposition", "attachment;filename="+imageName);。