java 下载图片 弹出框_javaweb多图片打包下载,弹出提示框问题_html/css_WEB-ITnose

该博客内容涉及文件下载和压缩的实现。通过遍历指定ID列表,获取操作员信息,并根据操作员的文件路径创建文件对象。如果文件存在,将其添加到文件列表中。接着,创建一个临时文件用于存储压缩包,并设置文件输出流和压缩流。调用方法将文件列表压缩并写入到输出流中,最后下载压缩文件。注意,此代码片段不适用于AJAX请求,建议使用动态创建的form表单提交以弹出保存对话框。
摘要由CSDN通过智能技术生成

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();

}

}

回复讨论(解决方案)

你用的是ajax来请求的,ajax请求是不会弹出提示保存位置选框的。建议你用js或jquery动态的创建form表单来提交,记得要加上

response.setHeader("Content-Disposition", "attachment;filename="+imageName);。

本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉

本文系统来源:php中文网

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值