将JSTL集合POST到servlet并填充需要双引号的String []

时间:2014-12-12 02:43:53

标签: java jsp servlets

好的,我在最初的帖子/问题中并不太清楚。

我的问题是我有一个jsp页面,一个人可以选择要下载的几个文件(它发布的servlet应该压缩它们并作为下载提供)。

jsp页面跟踪JSTL集合中选择的文件。

当该人点击下载按钮时,它会创建一个var来将所选文件发布到servlet,

<input type="hidden" name="files" value='<c:forEach var="item" items="${list}">${item}.ssg, </c:forEach>'>

将'xyz,xyz,xyz'之类的内容发送到servlet并分配给String [] srcFiles。

但问题是String []需要用双引号

进行格式化
String[] srcFiles = {"xyz", "xyz", "xyz"}

不喜欢

String[] srcFiles = {xyz, xyz, xyz}

zip部分不会正确迭代数组并返回一个0字节的zip文件。

如果我使用双引号手动填充带有多个文件的String [] srcFiles,整个过程就会发挥作用。

很抱歉我的帖子不清楚。

package com.share.memberarea.zip;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.*;

public class zipSSG extends HttpServlet {

    private static final long serialVersionUID = -4814365525955498299L;

    public void doGet(HttpServletRequest request, HttpServletResponse response)

    throws ServletException, java.io.IOException {

        String dl = request.getParameter("download");

        if (dl != null) {   

        String zipFile = "/home/httpd/html/research-material/ssg/ssg.zip";
        String[] srcFiles = {request.getParameter("files")};
        String zipDir = "/home/httpd/html/research-material/ssg";

        try {

            // create byte buffer
            byte[] buffer = new byte[1024];

            FileOutputStream fos = new FileOutputStream(zipFile);

            ZipOutputStream zos = new ZipOutputStream(fos);

            for (int i=0; i < srcFiles.length; i++) {

                File srcFile = new File(srcFiles[i]);

                FileInputStream fis = new FileInputStream(zipDir + "/" + srcFile);

                // begin writing a new ZIP entry, positions the stream to the start of the entry data
                zos.putNextEntry(new ZipEntry(srcFile.getName()));

                int length;

                while ((length = fis.read(buffer)) > 0) {
                    zos.write(buffer, 0, length);
                }

                zos.closeEntry();

                // close the InputStream 
                fis.close();

            }

            // close the ZipOutputStream
            zos.close();

        } // end try

        catch (IOException ioe) {
            System.out.println("Error creating zip file: " + ioe);
        }

        //

        String zipLoc = "ssg.zip";

        ServletOutputStream stream = null;
        InputStream in = null;

        try {
            stream = response.getOutputStream();
            File zip = new File(zipDir + "/" + zipLoc);

        response.setContentType("application/zip");
        response.addHeader("Content-Disposition", "Attachment; Filename=" + zipLoc);

        response.setContentLength( (int) zip.length() );


        in = new BufferedInputStream(new FileInputStream(zip));

        byte[  ] buf = new byte[8 * 1024];
        int bytesRead;

        while ((bytesRead = in.read(buf)) != -1) {
            stream.write(buf, 0, bytesRead);
        }

        } catch (IOException ioe) {

            throw new ServletException(ioe.getMessage());

        } finally {

            if (in != null) in.close(  );           
        }

        //

    } // end doGet 

    }

    public void Redirect(HttpServletRequest request, HttpServletResponse response) throws IOException{
    response.sendRedirect("/memberarea/df-search.html");

    }

}

1 个答案:

答案 0 :(得分:0)

根据我的理解,您希望将您的请求值从JSP分割为String []数组,该数组基本上是您的文件列表,包含在您的zip文件中。

您可以使用String.split("regex")直接将字符串xyz,xyz,xyz转换为String[] srcFiles = ...,如下所示:

String fileStr = request.getParameter("files");
String[] srcFiles = fileStr.split(","); // your file name is separated by ',' (comma)

我希望这会有所帮助。

如果这不是你想要的,请告诉我。