2012-09-08 71 views
0

我能夠讀取字節並在系統控制檯上打印出來。但是由於GAE不支持文件創建,我通過StackOverflow進行搜索,發現我可以寫入GAE blobstore。但我不知道如何去做,因爲我是GAE的新手。將字節數組寫入GAE Blobstore

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { 
    resp.setContentType("text/html;charset=UTF-8"); 
    // resp.setContentType("text/plain"); 
    PrintWriter out = resp.getWriter(); 
    try { 
     ServletFileUpload upload = new ServletFileUpload(); 
     FileItemIterator iterator = upload.getItemIterator(req); 
     // out.println("<html><body>"); 

     while (iterator.hasNext()) { 
      FileItemStream item = iterator.next(); 
      InputStream in = item.openStream(); 

      if (item.isFormField()) { 
       out.println("<br />Got a form field: " + item.getFieldName()); 
      } else { 
       out.println("<br />Got an uploaded file: " + item.getFieldName() + ", name = " + item.getName()); 

       ZipInputStream zis = new ZipInputStream(
       new BufferedInputStream(in)); 

       ZipEntry entry; 

       // Read each entry from the ZipInputStream until no 
       // more entry found indicated by a null return value 
       // of the getNextEntry() method. 

       byte[] buf = new byte[10244]; 
       int len; 
       while ((entry = zis.getNextEntry()) != null) { 

        out.println("Unzipping: " + entry.getName()); 

        FileService fileService = FileServiceFactory.getFileService(); 

        AppEngineFile file = fileService.createNewBlobFile("text/plain"); 

        boolean lock = false; 
        FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock); 

        PrintWriter outter = new PrintWriter(Channels.newWriter(writeChannel, "UTF8")); 

        StringBuilder sb = new StringBuilder(buf.length); 
        if (entry.getName().equalsIgnoreCase("booking.csv")) { 
         int count = 0; 
         while ((len = zis.read(buf, 0, buf.length)) != -1) { 
          //I'm trying to write byte[] into blobstore instead of printing using 
          //System.out.write(buf, 0, len); 
         } 

有什麼建議嗎?

回答

1

試試這個:

FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock); 
while ((len = zis.read(buf, 0, buf.length)) != -1) { 
    writeChannel. write(ByteBuffer.wrap(buf, 0, len), null); 
} 
+0

感謝您的幫助!它完美的作品! – chuntato

+0

FileService是正在關閉的Files API的一部分... https://cloud.google.com/appengine/docs/deprecations/files_api –

0

最簡單的方法:

import com.google.appengine.api.files.FileService; 
    import com.google.appengine.api.files.AppEngineFile; 
    import com.google.appengine.api.files.FileWriteChannel; 
    import com.google.appengine.api.blobstore.BlobKey; 
    import com.google.appengine.api.images.ImagesServiceFactory; 
    import com.google.appengine.api.images.ServingUrlOptions; 
    ... 


    // your data in byte[] format 
    byte[] data = image.getData(); 
    /** 
    * MIME Type for 
    * JPG use "image/jpeg" for PNG use "image/png" 
    * PDF use "application/pdf" 
    * see more: https://en.wikipedia.org/wiki/Internet_media_type 
    */ 
    String mimeType = "image/jpeg"; 

    // save data to Google App Engine Blobstore 
    FileService fileService = FileServiceFactory.getFileService(); 
    AppEngineFile file = fileService.createNewBlobFile(mimeType); 
    FileWriteChannel writeChannel = fileService.openWriteChannel(file, true); 
    writeChannel.write(java.nio.ByteBuffer.wrap(data)); 
    writeChannel.closeFinally(); 

    // your blobKey to your data in Google App Engine BlobStore 
    BlobKey blobKey = fileService.getBlobKey(file); 

    // THANKS TO BLOBKEY YOU CAN GET FOR EXAMPLE SERVING URL FOR IMAGES 

    // Get the image serving URL (in https:// format) 
    String imageUrl = 
     ImagesServiceFactory.getImagesService().getServingUrl(
     ServingUrlOptions.Builder.withBlobKey(blobKey 
      ).secureUrl(true));