使用servlet,如何从数据库下载多个文件并将其压缩以供客户端下载 [英] Using a servlet, how do you download multiple files from a database and zip them for client download

查看:130
本文介绍了使用servlet,如何从数据库下载多个文件并将其压缩以供客户端下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个jsp / servlet web应用程序,客户端可以通过下拉框选择课程和作业,然后单击按钮下载该课程下列出的数据库中的所有文件/分配组合。 servlet代码不能正常工作,因为zip文件没有作为附件发送到浏览器。我确实有一个工作代码一次下载一个文件,但有些东西被这个代码卡在了压缩文件。数据库中的所有文件本身都是zip文件,所以我试图压缩一堆zip文件。我不认为这会要求他们的处理方式与压缩任何其他格式的文件不同。谁能看到缺少的东西?这是我处理下载的servlet中的doGet方法代码。这些代码大部分都是在stackoverflow上找到的。请注意,FileSubmitted对象是我的DOA,包含数据库中每个文件的所有文件信息,包括Blob本身:

I have a jsp/servlet web app in which the client can choose a "course" and an "assignment" via dropdown boxes, and then click a button to download all the files in the database that are listed under that course/assignment combination. The servlet code isn't quite working, as the zip file is not being sent to the browser as an attachment. I do have working code for downloading one file at a time, but something is getting stuck with this code for zipping the files. All the files in the database are actually zip files themselves, so I am trying to zip up a bunch of zip files. I didn't think this would require that they be treated differently than zipping any other format of files. Can anyone see what is missing? Here is my doGet method code in the servlet that handles the downloading. Much of this code was found here on stackoverflow. Please note that the FileSubmitted object is my DOA containing all the file info for each file in the database, including the Blob itself:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    List<FileSubmitted> fileList = new ArrayList<FileSubmitted>();
    String course= request.getParameter("course");
    String assignment = request.getParameter("assignment");

    java.sql.PreparedStatement pstmt = null;
    java.sql.Connection conn = null;
    ResultSet rs;
    String queryString;

    try {
        conn = ConnectionManager.getConnection();
        conn.setAutoCommit(false);

        queryString = "SELECT * FROM files WHERE courseID=\""+course+"\" AND assignmentID=\""+assignment+"\";";
        pstmt = conn.prepareStatement(queryString);
        rs = pstmt.executeQuery(queryString);
        while(rs.next())
        {
            fileList.add(new FileSubmitted(rs.getString("username"),
                                           rs.getString("courseID"),
                                           rs.getString("assignmentID"),
                                           rs.getString("fileName"),
                                           rs.getString("mimeType"),
                                           (Blob) rs.getBlob("contents")));
        }


        response.setContentType("application/zip");
        response.setHeader("Content-Disposition", "attachment; filename=\"allfiles.zip\"");

        ZipOutputStream output = null;
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];

        try {
            output = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE));

            for (FileSubmitted file : fileList)
            {
                InputStream input = null;
                try {
                        input = new BufferedInputStream(file.getContents().getBinaryStream(), DEFAULT_BUFFER_SIZE);
                        output.putNextEntry(new ZipEntry(file.getFileName()));
                        for (int length = 0; (length = input.read(buffer)) > 0;)
                        {
                            output.write(buffer, 0, length);
                        }
                    }//try
                    catch (SQLException e) {e.printStackTrace();}
                    finally{}
                    output.closeEntry();
            }//for
          }//try
          finally{}
     } 
     catch (Exception e1) {e1.printStackTrace();}
     finally{}
}


推荐答案

如果这样可以对别人有帮助,我找到了问题的答案。上面发布的代码实际上非常适合从数据库下载多个文件并创建一个zip文件供客户端下载。问题是我通过ajax调用servlet,显然你无法通过ajax调用下载文件。所以我改变了我的jsp页面,通过提交表单来调用servlet,然后完全下载到浏览器。

In case this can be helpful to anyone else, I found the answer to the problem. The code posted above actually works perfectly for downloading multiple files from a database and creating a zip file for the client to download. The problem was that I was calling the servlet via ajax, and apparently you can't download files via an ajax call. So I changed my jsp page to call the servlet via submitting a form, and then the download came through to the browser perfectly.

这篇关于使用servlet,如何从数据库下载多个文件并将其压缩以供客户端下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆