将文件从HTML表单通过Servlet上传到Google Cloud Storage(使用Google Cloud Storage Client Library for Java) [英] Upload file from HTML form through Servlet to Google Cloud Storage (using Google Cloud Storage Client Library for Java)

查看:175
本文介绍了将文件从HTML表单通过Servlet上传到Google Cloud Storage(使用Google Cloud Storage Client Library for Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目是由GAE Plugin for Eclipse(没有Maven)创建的,而我是goint来发布我的代码:



home.jsp

 <!DOCTYPE html PUBLIC -  // W3C // DTD HTML 4.01 Transitional // ENhttp://www.w3 .ORG / TR / HTML4 / loose.dtd> 
< html>
< head>
< title>上传测试< / title>
< / head>
< body>
< form action =/ uploadmethod =postname =putFileid =putFile
enctype =multipart / form-data>
< input type =filename =myFileid =fileName>
< input type =submitvalue =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;

/ **以下用于确定要读取的卡盘的大小。应为> 1kb和< 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上传;
FileItemIterator迭代器;
FileItemStream项;
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(有一个表单字段:+ item.getFieldName());
} else {
log.warning(上传的文件:+ 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)设置UploadSize的maximumSize。或将BUFFER_SIZE从2 * 1024 * 1024更改为200 * 1024 * 1024,但发生问题。更具体地说,当上传达到100%时,我在网页上收到此消息:

 错误:请求实体太大您的客户发出的请求太大。 

如何使用JAVA和Google Cloud Storage Client Library修复Java? (我不会用其他编程语言大幅度改变项目)



请问你能帮我找一个解决方案吗?非常感谢!

解决方案

App Engine请求限制为32Mb。这就是为什么当您发送文件> 32Mb时,您的上传失败。 结帐配额和限额部分



您有两个上传文件> 32Mb的选项:





或者你可以使用Google云端硬盘并仅存储数据存储中的文档ID:)


My project has been created by GAE Plugin for Eclipse (without Maven) and i'm goint to post my code composed by:

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();
        }
      }

}

I tried also to set the maximumSize of the Upload using upload.setMaxSize(-1); or changing the BUFFER_SIZE from 2*1024*1024 into 200*1024*1024, but the issue stil occur. To be more specific, when the uploading reach the 100% I receive this message on the webpage:

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

How can i fix that using JAVA and Google Cloud Storage Client Library for Java? (I'm not going to change drastically the Project with other Programming Languages)

Could you please help me to find a solution? Thank you so much!

解决方案

App Engine request limit is 32Mb. That's why your uploads are failing when you send a file > 32Mb. Checkout Quotas and Limits section.

You have two options for uploading files > 32Mb:

Or you could just use Google Drive and store only doc IDs in the datastore :)

这篇关于将文件从HTML表单通过Servlet上传到Google Cloud Storage(使用Google Cloud Storage Client Library for Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆