导出数据包及下载

来源:互联网 发布:近现代文学 知乎 编辑:程序博客网 时间:2024/06/06 11:46

解决思路:

1.将数据导出为csv文件,以及图片导出,存放在服务器的一个文件夹里。

2.将此文件夹压缩为**.zip文件(还在服务器上)

3.客户端下载时,下载此zip文件。

4.下载完之后,再将服务器上的暂存文件删掉

 public ModelAndView exportDataPacke(HttpServletRequest request,
   HttpServletResponse response) throws Exception {
  int sellerid = new Integer(request.getParameter("sellerid"));
  SellerManager sm = new SellerManager();
  CompanyOfSeller seller = sm.getCompanySellerByID(sellerid);
  List<DataPacket> dps = seller.bgetDataPacket();
  String path = ApplicationContext.getAppPath();
  File f = new File(path + "//product_datapacket");
  if (!f.exists()) {
   f.mkdir();
  }
  try {
   //--------------------------- 导出为csv文件------------------------------------------------
   FileWriter fw = new FileWriter(f + "//data.csv");
   fw.write("宝贝名称,宝贝类目,店铺类目,新旧程度,省,城市,出售方式,宝贝价格,加价幅度,宝贝数量,有效期,"
     + "运费承担,平邮,EMS,快递,付款方式,支付宝,发票,保修,自动重发,放入仓库,橱窗推荐,发布时间,心情故事,"
     + "宝贝描述,宝贝图片,宝贝属性,团购价,最小团购件数,邮费模版ID,会员打折,修改时间,上传状态,"
     + "图片状态,返点比例,新图片,销售属性组合,用户输入ID串,用户输入名-值对,/r/n");
   for (int i = 0; i < dps.size(); i++) {
    Product product = dps.get(i).bgetProduct();
    StringBuffer buffer = new StringBuffer();
    buffer.append(product.getName() + ",");
    buffer.append("0,0,5,,,b,");
    buffer.append(product.bgetDefaultSpec().getPrice());
    buffer.append(",0,100,14,1,,,,2,1,0,0,1,0,0,,,");
    buffer.append(product.getContent());
    buffer.append(",images/"
      + product.bgetDefaultImage().getRealFileName());
    buffer.append(",,,0,0,0,0,,100,1,");
    buffer.append("0,100");
    buffer.append(",,,,,");
    fw.write(buffer + "/r/n");

    //------------------------------------ 导出图片---------------------------------------
    ProductImage image = product.bgetDefaultImage();
    String imageName = ApplicationContext.getAppPath()
      + File.separator + image.PRODUCT_IMG_PATH
      + File.separator + image.getRealFileName();
    String name[] = image.getRealFileName().split("/");
    File file = new File(f + "//images");
    file.mkdir();
    file = new File(file + "//" + name[0]);
    file.mkdir();
    InputStream input = null;
    FileOutputStream output = new FileOutputStream(file + "//"
      + name[1]);
    try {
     byte[] tempbytes = new byte[100];
     int byteread = 0;
     input = new FileInputStream(imageName);
     while ((byteread = input.read(tempbytes)) != -1) {
      output.write(tempbytes, 0, byteread);
     }
    } catch (Exception e1) {
     e1.printStackTrace();
    } finally {
     if (input != null) {
      try {
       input.close();
       output.close();
      } catch (IOException e1) {
      }
     }
    }
   }

   fw.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
  //---------------------------------------压缩文件------------------------------
  // 压缩baseDir下所有文件,包括子目录
  String baseDir = path + "//product_datapacket";
  List<File> fileList = getAllFiles(new File(baseDir));

  // 压缩文件名
  ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(
     path + "//product_datapacket.zip"));

  ZipEntry ze = null;
  byte[] buf = new byte[1024];
  int readLen = 0;
  for (File f1 : fileList) {
      // 创建一个ZipEntry,并设置Name和其它的一些属性
      String real = f1.getAbsolutePath();
      // 取得压缩文件中的路径
      if (real.indexOf(baseDir) != -1) {
   real = real.substring(real.indexOf(baseDir) + baseDir.length()
    + 1);
      } else {
   real = f1.getName();
      }
      ze = new ZipEntry(real);
      ze.setSize(f1.length());
      ze.setTime(f1.lastModified());

      // 将ZipEntry加到zos中,再写入实际的文件内容
      zos.putNextEntry(ze);
      InputStream is = new BufferedInputStream(new FileInputStream(f1));
      while ((readLen = is.read(buf, 0, 1024)) != -1) {
   zos.write(buf, 0, readLen);
      }
      is.close();
  }
  zos.close();
  
  //-------------------------- 下载压缩数据包----------------------------------------
  String fileName = request.getParameter("fileName");
  File fl=new File(path + "//product_datapacket.zip");
  response.setContentType("application/x-download");
  response.setHeader("Content-Disposition", "attachment;filename="
    + java.net.URLEncoder.encode(fileName, "UTF-8"));
  response.setContentLength((int) fl.length()); // 设置下载内容大小
        System.out.println("============="+fl.length());
  byte[] buffer = new byte[4096]; // 缓冲区
  BufferedOutputStream output = null;
  BufferedInputStream input = null;
  try {
   output = new BufferedOutputStream(response.getOutputStream());
   input = new BufferedInputStream(new FileInputStream(fl));
   int n = (-1);
   while ((n = input.read(buffer, 0, 4096)) > -1) {
    output.write(buffer, 0, n);
   }
   response.flushBuffer();
  } catch (Exception e) {
  } // 用户可能取消了下载
  finally {
   if (input != null)
    input.close();
   if (output != null)
    output.close();
  }
  return new ModelAndView("/workspace/refresh");
 }

 


    /**
     * 获取某个文件夹下的所有文件(包括子文件夹)
     *
     * @param baseDir
     * @return
     */
    private static List<File> getAllFiles(File baseDir) {
 List<File> files = new ArrayList<File>();
 File[] tmp = baseDir.listFiles();
 for (int i = 0; i < tmp.length; i++) {
     if (tmp[i].isFile()) {
  files.add(tmp[i]);
     }
     if (tmp[i].isDirectory()) {
  files.addAll(getAllFiles(tmp[i]));
     }
 }
 return files;
    }

原创粉丝点击