结合:页面下载远程服务器单个及多个文件并打成压缩包下载到本地

调整为多个文件下载时创建临时压缩包,下载完成后,临时压缩包删除

路径调整:new File(request.getSession().getServletContext().getRealPath("/")+"/" + ns.getFile_name()+".zip");//本地tomcat路径webapps下

整合后:

            File zipFile = null;
                    ZipOutputStream out = null;
                    String newfilename ="";
                    if(sids.length==1 && ns.getType()==0 ){
                        Networkfile nf = networkfileService.selectById(ns.getFid());
                        URL httpurl=new URL("http://服务器路径/"+nf.getSf_fid());
                        HttpURLConnection httpConn=(HttpURLConnection)httpurl.openConnection();
                        httpConn.setDoOutput(true);// 使用 URL 连接进行输出
                        httpConn.setDoInput(true);// 使用 URL 连接进行输入
                        httpConn.setUseCaches(false);// 忽略缓存
                        httpConn.setRequestMethod("GET");// 设置URL请求方法
                        //可设置请求头
                        httpConn.setRequestProperty("Content-Type", "application/octet-stream");
                        httpConn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
                        httpConn.setRequestProperty("Charset", "UTF-8");
                        //可设置请求头
                        byte[] file =input2byte(httpConn.getInputStream());
                        response.setContentType("application/octet-stream");
                        response.setHeader("Content-Disposition", "attachment;filename=" + new String(ns.getFile_name().getBytes("UTF-8"),"ISO-8859-1"));
                        response.getOutputStream().write(file);
                        System.out.println("单个下载成功");
                     }else{
                         System.out.println("压缩开始...");
                         for (String i : sids) {
                             ns = networkstructureService.selectById(Integer.parseInt(i));
                             if(zipFile == null){
                                 //拿取第一个选中项名称,作为文件夹名称,创建临时文件夹
                                  newfilename = request.getSession().getServletContext().getRealPath("/")+"/" + ns.getFile_name()+".zip";
                                  zipFile = new File(newfilename);
                                  //创建zip输出流
                                  out = new ZipOutputStream( new FileOutputStream(zipFile));
                              }
                             //调用压缩打包函数
                             compress(out,ns,ns.getFile_name());
                          }
                         System.out.println("创建临时压缩包完成");
                         // 下载,并删除临时文件zip
第一种调用读取                         
downloadFile2(newfilename.substring(newfilename.lastIndexOf("/")+1),zipFile,response);
第二种调用读取 利用上面单个文件读取方法直接输出也可以
byte[] file =input2byte(new FileInputStream(zipFile.getPath()));
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment;filename=" + new String(newfilename.substring(newfilename.lastIndexOf("/")+1).getBytes("UTF-8"),"ISO-8859-1"));
response.getOutputStream().write(file);
}
if(out!=null)out.close(); if(zipFile!=null)zipFile.delete();//删除临时压缩包文件

下载,删除临时文件方法

public static void downloadFile2(String filename,File file,HttpServletResponse response) throws FileNotFoundException {
        try {
             // 以流的形式下载文件。
             BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
             byte[] buffer = new byte[fis.available()];
             fis.read(buffer);
             fis.close();
             OutputStream bos = new BufferedOutputStream(response.getOutputStream());
             response.setContentType("application/zip");
             response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("UTF-8"),"ISO-8859-1"));
             
             bos.write(buffer);
             bos.flush();
             bos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

以上综合代码参考两个文献地址:

https://www.cnblogs.com/zeng1994/p/7862288.html
https://blog.csdn.net/java_zhaoyu/article/details/82378906
 

页面中js代码由ajax方法调整为download

自动调用浏览器自带下载弹窗

download()方法

//传入参数src为文件地址
function  download (src) {
    var download_file= {} 
   if (typeof(download_file.iframe) == "undefined") {
     var iframe = document.createElement("iframe");
     download_file.iframe = iframe;
     document.body.appendChild(download_file.iframe);
   }
   download_file.iframe.src = src
  download_file.iframe.style.display = "none";
 }

 

posted on 2019-07-05 12:37  茫无所知  阅读(112)  评论(0编辑  收藏