1

我想将文件上传到 Google 云存储 Api 中的存储桶中。但是当我运行servelet类时,它成功部署并在浏览器中显示输出,例如“现在在这里查看您的文件内容,您已上传到存储中..文件上传完成”。

但问题是servelet类不会建立与谷歌云存储的连接。文件不会上传到bucket中。一旦检查

代码并就如何使用此源代码连接到存储桶提供建议。

 public class TestCloudStorageServlet extends HttpServlet{

 private static final long serialVersionUID = 1L;
 private StorageService storage = new StorageService();
 private static final int BUFFER_SIZE = 1024 * 1024;
 private static final Logger log = Logger.getLogger(TestCloudStorageServlet.class.getName()); 
 @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
   log.info(this.getServletInfo()+" Servlets called....");
     resp.setContentType("text/plain");
    resp.getWriter().println("Now see here your file content, that you have uploaded on    storage..");

  ServletFileUpload upload = new ServletFileUpload();
     System.out.println(upload);
    FileItemIterator iter;
   try {
    iter = upload.getItemIterator(req);

    while (iter.hasNext()) {
   FileItemStream item = iter.next();
   String fileName = item.getName();
   String mime = item.getContentType();

   storage.init(fileName, mime);
   InputStream is = item.openStream();

   byte[] b = new byte[BUFFER_SIZE];
   int readBytes = is.read(b, 0, BUFFER_SIZE);
   while (readBytes != -1) {
       storage.storeFile(b, readBytes);
       readBytes = is.read(b, 0, readBytes);
   }

   is.close();
   storage.destroy();

    resp.getWriter().println("File uploading done");

     //resp.getWriter().println("READ:" + storage.readTextFileOnly(fileName));

                     log.info(this.getServletName()+" ended....");        

      }
     } 
       catch (FileUploadException e) {
           System.out.println("FileUploadException::"+e.getMessage());
         log.severe(this.getServletName()+":FileUploadException::"+e.getMessage());
      e.printStackTrace();
       } catch (Exception e) {
    log.severe(this.getServletName()+":Exception::"+e.getMessage());
         System.out.println("Exception::"+e.getMessage());
     e.printStackTrace();
     }
       }

我的 StorageService 类用于上传文件。

       public class StorageService {

         public static final String BUCKET_NAME = "mybucketname";  

        private FileWriteChannel writeChannel = null;
            FileService fileService = FileServiceFactory.getFileService();
        private OutputStream os = null;
        private static final Logger log =            Logger.getLogger(StorageService.class.getName());

       public void init(String fileName, String mime) throws Exception {
      System.out.println("Storage service:init() method:  file name:"+fileName+"  and                mime:"+mime);
     log.info("Storage service:init() method:  file name:"+fileName+" and mime:"+mime);
        log.info("test.."); 
   GSFileOptionsBuilder builder = new GSFileOptionsBuilder()
        .setAcl("public_read")
        .setBucket(BUCKET_NAME)
        .setKey(fileName)
        .setMimeType(mime);
   log.info("test..");
 AppEngineFile writableFile = fileService.createNewGSFile(builder.build());
 boolean lock = true;
 writeChannel = fileService.openWriteChannel(writableFile, lock);
    os = Channels.newOutputStream(writeChannel);
      }

        public void storeFile(byte[] b, int readSize) throws Exception {
      os.write(b, 0, readSize);
        os.flush();
     }


      public void destroy() throws Exception {
      log.info("Storage service: destroy() method");
      os.close();
    writeChannel.closeFinally();
       }

       }
4

0 回答 0