解决方案 »

  1.   

    ZipOutputStream和ServletOutputStream都是直接继承OutputStream的,不明白你说的转换是啥意思。
      

  2.   

    ZipOutputStream转成ByteArrayOutputStream,然后把ByteArrayOutputStream(bais)写到ServletOutputStream:
    ServletOutputStream sos = this.getResponse().getOutputStream();
    while ((bytesRead = bais.read(buffer, 0, 8092)) != -1) {
    sos.write(buffer, 0, bytesRead);
    }
      

  3.   

    回复1#:
    我现在最终输出需要用ServletOutputStream.write(byte[] bs),因为页面需要实现点击一个按钮,下载一个压缩包。可能我之前说错了,我现在就是不知道怎么实现这个下载压缩包(压缩包里面是一个或多个word文档,从数据库取出来的,而且取出来都是byte[]类型)的功能。
      

  4.   

    回复 #2:
    ZipOutputStream转成ByteArrayOutputStream是这样实现么:
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ZipOutputStream zos = new ZipOutputStream(bos);
      

  5.   


    byte[] b = null;
    try {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ZipOutputStream zos = new ZipOutputStream(bos);
     byte[] buffer = data;
                 ZipEntry entry = new ZipEntry("");
                 zos.putNextEntry(entry);
                 zos.write(buffer);
                 zos.closeEntry();
                 zos.close();
    b = bos.toByteArray();
    bos.close();
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    return b;这段代码有错么 = =
      

  6.   

     IOUtils.copy(origin, zos);
      

  7.   


    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipPath));
    。。
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    InputStream is = new FileInputStream(zipPath);
    while ((bytesRead = is.read(buffer, 0, 8092)) != -1) {
    bos.write(buffer, 0, bytesRead);
    }
      

  8.   

     IOUtils.copy(origin, zos);
    这就是commons-io-2.0.1.jar包里的方法,把一个流拷贝到另一个流里。
    首先你的压缩生成的是ZipOutputStream 流。你要吧这个ZipOutputStream流copy到一个临时输入流里,
      fileInputStream = new FileInputStream(file);
            origin = new BufferedInputStream(fileInputStream, bufferLen);
            IOUtils.copy(origin, ZipOutputStream);这个临时输入流也就是临时压缩文件夹。之后你再吧这个压缩好的文件读取ServletOutputStream   。
      

  9.   

    刚才说的有点乱,给你个压缩的方法吧  好久没搞这个了 都忘了。
    public void addEntry(OutputStream tOut, File directory, String baseName)//  这个方法的第一个参数可以是你的ServletOutputStream, 
    //第二个参数是你要压缩的文件,也可以是流 自己改改吧 ,第三个参数是用来得到文件的文件名的。
          throws ArchiveException, IOException {
        if (!directory.exists())
          return;
        ZipArchiveOutputStream outStream = (ZipArchiveOutputStream) tOut;
        for (File file : directory.listFiles()) {
          String relativeName = getRelativePath(baseName, file);
          ArchiveEntry entry = new ZipArchiveEntry(relativeName);
          outStream.putArchiveEntry(entry);
          if (file.isDirectory()) {
            // Add the files within the directory
            addEntry(outStream, file, baseName);
            continue;
          }
          FileInputStream fileInputStream = null;
          BufferedInputStream origin = null;
          try {
            fileInputStream = new FileInputStream(file);
            origin = new BufferedInputStream(fileInputStream, bufferLen);
            IOUtils.copy(origin, outStream);
          } finally {
            IOUtils.closeQuietly(fileInputStream);
            IOUtils.closeQuietly(origin);
          }
          outStream.closeArchiveEntry();
        }
      }
      

  10.   

    刚才着急,有些可能没说清楚,我现在的情况是没有inputstream,只有byte[],然后需要把这个byte[]打包压缩,生成什么流都行,只要最后能返回页面让我下载就好了。注意我下载的东西需要是个压缩包,而且byte[]里是多个文件。有思路的麻烦告我一声,谢谢了
      

  11.   

    自己动动脑子,静下心来就可以写出来了,下面的仅供参考:
    //得到zip输出流
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipPath));

    //写入word数据到zip流
    zos.putNextEntry(new ZipEntry(title + "(附件一).doc"));
    zos.setEncoding("GB2312");
    zos.write(baos.toByteArray());
    zos.flush();

    int bytesRead = 0;
    byte[] buffer = new byte[8092];

    //写入Excel数据流到zip流
    String excelPath = this.createYfsExcel();
    zos.putNextEntry(new ZipEntry(title + "(附件二).xls"));
    zos.setEncoding("GB2312");
    FileInputStream fos = new FileInputStream(new File(excelPath));
    while ((bytesRead = fos.read(buffer, 0, 8092)) != -1) {
    zos.write(buffer, 0, bytesRead);
    }

    zos.closeEntry();
    zos.close();
    baos.close();


    //读取打包的zip返回字节数组输入流
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    InputStream is = new FileInputStream(zipPath);
    while ((bytesRead = is.read(buffer, 0, 8092)) != -1) {
    bos.write(buffer, 0, bytesRead);
    }
      

  12.   

    ByteArrayOutputStream 转ZipOutputStream就按照我最上面的回复写就行了,有什么难的么?
      

  13.   

    你最上面不是说要把ZipOutputStream转成ByteArrayOutputStream 么?
      

  14.   


                //prepare response header
                Response.Clear();
                Response.ContentType = "application/x-zip-compressed";
                Response.AddHeader("Content-Disposition", "attachment; filename=Submission.zip");
                ZipOutputStream zipOutput = new ZipOutputStream(Response.OutputStream);
                try
                {
                    zipOutput.IsStreamOwner = false;
                    zipOutput.SetLevel(9);
                    zipOutput.Write(data, 0, data.Length);
                    Response.Flush();
                }
                catch (Exception ex)
                {
                    Response.Write(ex.ToString());
                }
                finally
                {
                    zipOutput.Finish();
                    zipOutput.Close();
                    Response.End();
                }
      

  15.   


     /**
     * 下载文件
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     */
    private ActionForward downloadZipFiles(ActionMapping mapping,
    ActionForm form, HttpServletRequest request,
    HttpServletResponse response) {
    ServletOutputStream out = null;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ZipOutputStream zos = new ZipOutputStream(bos);
    zos.setEncoding("GBK");//解决压缩包内文件名乱码问题
    if (caseMediationManager == null) {
    request.setAttribute("message", "系统内部错误,没有找到caseMediationManager");
    return mapping.findForward("error");
    }
    String applicationFormId = request.getParameter("applicationFormId");
    List<FileData> fileDataList = caseMediationManager.queryLicenceFiles(new BigDecimal(applicationFormId));
    try {
    response.reset();
    byte[] bs = null;
    List<byte[]> bytelist = new ArrayList<byte[]>();
    if (fileDataList.size() > 0 && fileDataList != null){
    for (int i = 0; i < fileDataList.size(); i++) {
    //压缩文件
    byte[] bytes = fileDataList.get(i).getFileStream();
    ZipEntry entry = new ZipEntry(fileDataList.get(i).getFileName());
    zos.putNextEntry(entry);
    zos.write(bytes);
    }
    zos.closeEntry();
    zos.close();
    bs = bos.toByteArray();
    bos.close();
    bytelist.add(bs);
    bs = this.bytesCombine(bytelist);
    StringBuffer sb = new StringBuffer(50);
    sb.append("attachment;filename=");
    sb.append("LICENCE.zip");
    response.setHeader("Content-Disposition", StringUtil.toUtf8String(sb.toString()));
    response.setContentType("text/html;charset=utf-8");
    out = response.getOutputStream();
    out.write(bs);
    out.flush();
    } else{
    response.setContentType("text/html");
    out = response.getOutputStream();
    OutputStreamWriter ow = new OutputStreamWriter(out,"utf-8");
    ow.write("资料下载失败!");
    ow.flush();
    ow.close();
    }
    } catch (IOException e) {
    log.error("[文件下载]:" + ExceptionDetail.getException(e));
    request.setAttribute("message", "文件下载时出错!");
    return mapping.findForward("error");
    } finally {
    try {
    if (null != out) {
    out.close();
    }
    } catch (IOException e) {
    log.error("[文件下载]:" + ExceptionDetail.getException(e));
    e.printStackTrace();
    }
    }
    return null;
    }

    /**
     * byte数组的合并
     * 
     * @param srcArrays
     * @return
     */
    private byte[] bytesCombine(List<byte[]> srcArrays) {
    byte[] destArrays = null;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {
    for (byte[] srcArray : srcArrays) {
    bos.write(srcArray);
    }
    destArrays = bos.toByteArray();
    bos.flush();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    bos.close();
    } catch (IOException e) {
    }
    }
    return destArrays;
    }