【问题标题】:Writing to CSV Files and then Zipping it up in Appengine (Java)写入 CSV 文件,然后在 Appengine (Java) 中压缩
【发布时间】:2011-07-04 23:26:47
【问题描述】:

我目前正在谷歌 appengine 上用 Java 完成一个项目。

Appengine 不允许存储文件,因此无法使用任何磁盘上的表示对象。其中一些包括 File 类。

我想将数据写入并导出到几个csv文件,然后将其压缩,并允许用户下载。

如何在不使用任何 File 类的情况下做到这一点?文件处理经验不是很丰富,希望大家多多指教。

谢谢。

【问题讨论】:

    标签: java google-app-engine csv export


    【解决方案1】:

    如果您的数据不是很大,则意味着可以保留在内存中,然后导出为 CSV 并压缩和流式传输以供下载,所有这些都可以即时完成。缓存可以在任何这些步骤中完成,这在很大程度上取决于您的应用程序的业务逻辑。

    【讨论】:

      【解决方案2】:

      您可以创建一个 zip 文件并在用户下载它时添加到它。如果您使用的是 servlet,这很简单:

      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      
          // ..... process request
      
          // ..... then respond
          response.setContentType("application/zip");
          response.setStatus(HttpServletResponse.SC_OK);
      
          // note : intentionally no content-length set, automatic chunked transfer if stream is larger than the internal buffer of the response
          ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream());
      
          byte[] buffer = new byte[1024 * 32];
          try {
                  // case1: already have input stream, typically ByteArrayInputStream from a byte[] full of previoiusly prepared csv data
      
                  InputStream in = new BufferedInputStream(getMyFirstInputStream());
                  try {
                      zipOut.putNextEntry(new ZipEntry("FirstName"));
      
                      int length;
                      while((length = in.read(buffer)) != -1) {
                          zipOut.write(buffer, 0, length);
                      }
      
                      zipOut.closeEntry();
                  } finally {
                      in.close();
                  }
      
                  // case 2: write directly to output stream, i.e. you have your raw data but need to create csv representation
      
                  zipOut.putNextEntry(new ZipEntry("SecondName"));
      
                  // example setup, key is to use the below outputstream 'zipOut' write methods
                  Object mySerializer = new MySerializer(); // i.e. csv-writer
                  Object myData = getMyData(); // the data to be processed by the serializer in order to make a csv file
      
                  mySerizalier.setOutput(zipOut);
      
                  // write whatever you have to the zipOut
                  mySerializer.write(myData);
      
                  zipOut.closeEntry();
      
                  // repeat for the next file.. or make for-loop
      
              }
          } finally { 
              zipOut.close();
          }
      }
      

      除非您有内存限制,否则没有理由将数据存储在文件中。文件为您提供 InputStream 和 OutputStream,它们都具有内存中的等价物。

      请注意,创建 csv 写入器通常意味着执行类似this 的操作,其中的重点是获取一段数据(数组列表或映射,无论您拥有什么)并将其制成 byte[] 部分。使用 DataOutputStream(如果您愿意,可以自己制作)或 OutputStreamWriter 之类的工具将 byte[] 部分附加到 OutputStream 中。

      【讨论】:

      • 您好,感谢您的帮助!这真的很有帮助。但是你能告诉我你如何使用条纹框架来做到这一点吗?另外,我怎样才能专门完成这部分?对象 mySerializer = new MySerializer(); // 即 csv-writer Object myData = getMyData();
      • 不,我不使用 Strips 框架,但请随时在论坛中提出其他问题;如果您可以在条带中找到输出流,那么它可能就是您想要的。查看其他 cmets。
      猜你喜欢
      • 1970-01-01
      • 2013-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多