有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

servlets在Xpages中使用Java下载多个文件

我在xpage中使用此代码从远程服务器下载文件。当用户单击xpage中的下载按钮时,会显示一个新的下载。xsp页面打开并运行下面的代码

#{javascript:
var exCon = facesContext.getExternalContext();
var response = exCon.getResponse();
var out = response.getOutputStream();

var zipfile = sessionScope.thezip;
var dname = zipfile.substring(zipfile.lastIndexOf("\\")+1);

dl.download(zipfile,dname);
sessionScope.thezip = null;

response.setContentType("application/zip, application/octet-stream");
response.addHeader("Content-disposition","attachment; filename="+dname);
response.setHeader("Cache-Control", "no-cache");

facesContext.responseComplete();
out.close();

下载方法(db.Download(string,string))是一种Java方法,作为托管bean提交给xpage

public void download(String filepath, String dname){
        Connection con = null;
        PreparedStatement stm = null;
        ResultSet rs = null;
        try{
            Class.forName(jdbcClass);
            con = DriverManager.getConnection(url);

                String insSql = "INSERT INTO Cache(name,zip) SELECT '"+filepath+"',* FROM OPENROWSET(BULK N'"+filepath+"', SINGLE_BLOB) AS import;";
                stm = con.prepareStatement(insSql);
                stm.executeUpdate();

            String sql = "SELECT TOP 1 * FROM Cache where name = ?;";
            stm = con.prepareStatement(sql);
            stm.setString(1, filepath);
            rs = stm.executeQuery();

            while(rs.next()){
                InputStream zip = rs.getBinaryStream("zip");

                FacesContext facesContext = FacesContext.getCurrentInstance(); 
                ExternalContext externalContext = facesContext.getExternalContext();
                HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
                response.setContentType("application/zip, application/octet-stream");
                response.setHeader("Content-disposition","attachment; filename="+dname);
                response.setHeader("Cache-Control", "no-cache");

                byte[] buf = new byte[8192];
                int c = 0;

                while ((c = zip.read(buf, 0, buf.length)) > 0) {
                    OutputStream o = response.getOutputStream();
                    o.write(buf, 0, c);
                    o.close();
                }
                zip.close();
            }

        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try{
                if(stm!=null){stm.close();}
                if(rs!=null){rs.close();}
                if(con!=null){con.close();}
            }catch(Exception ex){ex.printStackTrace();}
        }

    }

这段java代码运行一个sql查询,以获取一个以字节为单位的zip文件,并将其存储在一个表中。然后选择这一行,并将字节返回给调用方java方法。通过这种方式获取远程文件,因为没有Web服务器提供url

我的问题是如何使用httpResponse outputStream下载2或3个文件?如果我复制粘贴代码,我只得到第一个文件。我试图不关闭outputStream,但我得到一个错误,该流已经在使用中

有人知道吗

注:以上代码经过测试,如果我只想下载1个文件,就可以正常工作


共 (1) 个答案

  1. # 1 楼答案

    最好的选择可能是使用java.util.zip类将多个文件动态组合成另一个ZIP文件。您可以使用ZipOutputStream包装输出流,然后在结果集行中循环,创建ZipEntry对象来区分其中的每个文件