2014-04-17 74 views
3

我的项目是由Eclipse的GAE Plugin创建的(没有Maven),我想要发布我的代码:将文件从HTML表单通过Servlet上传到Google云端存储(使用Google云端存储客户端库for Java)

针对home.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
    <head> 
    <title>Upload Test</title> 
    </head> 
    <body> 
     <form action="/upload" method="post" name="putFile" id="putFile" 
       enctype="multipart/form-data"> 
       <input type="file" name="myFile" id="fileName"> 
       <input type="submit" value="Upload"> 
     </form> 
    </body> 
    </html> 

UploadServlet.java:

import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.PrintWriter; 
import java.nio.channels.Channels; 
import java.util.Enumeration; 
import java.util.logging.Logger; 

import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import org.apache.commons.fileupload.FileItemIterator; 
import org.apache.commons.fileupload.FileItemStream; 
import org.apache.commons.fileupload.servlet.ServletFileUpload; 

import com.google.appengine.tools.cloudstorage.GcsFileOptions; 
import com.google.appengine.tools.cloudstorage.GcsFilename; 
import com.google.appengine.tools.cloudstorage.GcsOutputChannel; 
import com.google.appengine.tools.cloudstorage.GcsService; 
import com.google.appengine.tools.cloudstorage.GcsServiceFactory; 
import com.google.appengine.tools.cloudstorage.RetryParams; 

public class UploadServlet extends HttpServlet { 

    private static final Logger log = Logger.getLogger(UploadServlet.class.getName()); 

    private final GcsService gcsService = GcsServiceFactory.createGcsService(new RetryParams.Builder() 
    .initialRetryDelayMillis(10) 
    .retryMaxAttempts(10) 
    .totalRetryPeriodMillis(15000) 
    .build()); 

    private String bucketName = "myBucketNameOnGoogleCloudStorage"; 

    /**Used below to determine the size of chucks to read in. Should be > 1kb and < 10MB */ 
     private static final int BUFFER_SIZE = 2 * 1024 * 1024; 

    @SuppressWarnings("unchecked") 
    @Override 
    public void doPost(HttpServletRequest req, HttpServletResponse res) 
      throws ServletException, IOException { 

     String sctype = null, sfieldname, sname = null; 
     ServletFileUpload upload; 
     FileItemIterator iterator; 
     FileItemStream item; 
     InputStream stream = null; 
     try { 
      upload = new ServletFileUpload(); 
      res.setContentType("text/plain"); 

      iterator = upload.getItemIterator(req); 
      while (iterator.hasNext()) { 
       item = iterator.next(); 
       stream = item.openStream(); 

       if (item.isFormField()) { 
        log.warning("Got a form field: " + item.getFieldName()); 
       } else { 
        log.warning("Got an uploaded file: " + item.getFieldName() + 
          ", name = " + item.getName()); 

        sfieldname = item.getFieldName(); 
        sname = item.getName(); 

        sctype = item.getContentType(); 

        GcsFilename gcsfileName = new GcsFilename(bucketName, sname); 

        GcsFileOptions options = new GcsFileOptions.Builder() 
        .acl("public-read").mimeType(sctype).build(); 

        GcsOutputChannel outputChannel = 
          gcsService.createOrReplace(gcsfileName, options); 

        copy(stream, Channels.newOutputStream(outputChannel)); 

        res.sendRedirect("/"); 
       } 
      } 
     } catch (Exception ex) { 
      throw new ServletException(ex); 
     } 
    } 

    private void copy(InputStream input, OutputStream output) throws IOException { 
     try { 
      byte[] buffer = new byte[BUFFER_SIZE]; 
      int bytesRead = input.read(buffer); 
      while (bytesRead != -1) { 
      output.write(buffer, 0, bytesRead); 
      bytesRead = input.read(buffer); 
      } 
     } finally { 
      input.close(); 
      output.close(); 
     } 
     } 

} 

我试图也设定上传的使用upload.setMaxSize(-1)的MAXIMUMSIZE;或将BUFFER_SIZE从2 * 1024 * 1024更改为200 * 1024 * 1024,但问题仍然存在。更具体地讲,当载达到100%,我收到此消息的网页:

Error: Request Entity Too Large Your client issued a request that was too large. 

如何解决在使用JAVA和谷歌云存储客户端库的Java? (我不打算改变与其他编程语言的项目)

你能帮我找到解决方案吗?非常感谢!

+0

什么是文件的大小,你正在上传。你可以尝试一个小文件? – alex

+0

我尝试过所有类型的大小,它似乎只适用于小于32MB的文件。我想上传大于32MB的文件。 – Aerox

+0

请求大小限制为32Mb。看到我的答案替代品。 – alex

回答

6

App Engine的请求限制为32MB。这就是为什么当你发送一个大于32Mb的文件时,你的上传失败。 Checkout Quotas and Limits section

你有两个选择上传文件> 32Mb的:

或者您也可以使用谷歌云端硬盘,只存储文档的ID在数据存储:)

+0

https://developers.google。com/storage /此链接和其他人不会谈论Google Cloud Storage的请求限制,否则就没有任何解决方案来执行上传。我对吗? – Aerox

+0

这不是云存储的限制,而是App Engine *的*请求限制。有关限制的更多详细信息,请参阅以下网址:https://developers.google.com/appengine/docs/java/#Java_Quotas_and_limits – alex

+0

如果我要使用Blobstore,则会出现重复对象的问题(在Blobstore中和谷仓云存储内部)。正如“文档”所述,您可以使用BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); BlobKey blobKey = blobstoreService.createGsBlobKey( “/ gs /”+ fileName.getBucketName()+“/”+ fileName.getObjectName()); blobstoreService.serve(blobKey,resp);这不足以让它工作。他们错过了文档示例中的所有代码。请问你能帮帮我吗? – Aerox

0

我会建议你看看这个很好的例子:http://docs.oracle.com/javaee/6/tutorial/doc/glraq.html 一个好主意是监视数据流到服务器。

希望它可以帮助

+1

如果你不知道你在写什么,我建议看看http://stackoverflow.com/questions/21406878/uploading-files-with-primefaces-4-0-and-jsf-2-2-谷歌应用程序引擎,因为谷歌App Engine不支持Servlet 3.0。所以我非常感谢你以更具体的方式回答,因为我已经测试了这个代码3周,并且我有相当的文档记录。由于你发布的代码(我已经看到很多天),在我的情况下是无用的。您可以发布Google App Engine的工作代码吗?非常感谢你。 – Aerox

相关问题