简体   繁体   English

本地图片上传未显示在Google App Engine上

[英]local image upload not showing on Google App Engine

Here is my upload function on server side: 这是我在服务器端的上传功能:

private static final int BUFFER_SIZE = 2 * 1024 * 1024;
public void processRequest(HttpServletRequest request)
        throws ServletException, IOException {

    // multipart request
    final Part filePart = request.getPart("file");
    // some function to get the filename from part
    final String name = getFileName(filePart);
    // some function to convert the filename to GcsFilename
    final GcsFilename fileName = getFileNameGCS(name);
    //method to copy file taken from official documentation
    GcsFileOptions instance = GcsFileOptions.getDefaultInstance();
    GcsOutputChannel outputChannel = gcsService.createOrReplace(fileName, instance);
    copy(filePart.getInputStream(), Channels.newOutputStream(outputChannel));

}

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

Here is my post function on client side (Angular2) album.component.ts: 这是我在客户端(Angular2)album.component.ts上的发布函数:

  uploadAlbum(index:number){
    const formData = new FormData();
    formData.append('file', this.targets[index].file, '/gcs/my-bucket/' + index +'/cover/cover.jpg');
    this.http.post('/api/photos',formData, {responseType: 'text'}).subscribe(
      (data: any) => {
        console.log(data)
      },
       err => {
         console.log(err)
        }
      );
  }

now I use this method to retrieve the public url to my local file (App Engine Google Cloud Storage simulates a server upload and place it in a local space) 现在,我使用此方法来检索本地文件的公共网址(App Engine Google Cloud Storage模拟服务器上传并将其放置在本地空间中)

sample code: 样例代码:

...
BlobKey blobKey = blobStore.createGsBlobKey("/gs/" + bucket + "/" + listFilenames.get(i));
log.info("/gs/" + bucket + "/" + listFilenames.get(i));
ImagesService imagesService = ImagesServiceFactory.getImagesService();
String url = imagesService.getServingUrl(ServingUrlOptions.Builder.withBlobKey(blobKey));
jsonObj.put("path", url);
json.put(jsonObj);
...

basically what I do in the other part of the function is parse through all the files and filter the one that I have interest in. 基本上,我在该函数另一部分所做的工作是分析所有文件并筛选出我感兴趣的文件。

And it works I get the URL with the encoded_gs_key but nothing is showing at all. 它可以正常工作,但是我获得了带有encoded_gs_key的URL,但是什么都没有显示。 Is there a problem with my upload? 我的上传有问题吗? Do BlobStore serves a link even when it don't find a file at the specified location? 即使在指定位置找不到文件,BlobStore也会提供链接吗? One more thing is that I always get the same encoded_gs_key link even though my folders and images are different ( 0/cover/cover.jpg and for example 1/cover/cover.jpg so different root folders, different images but same filename) 还有一件事是,即使我的文件夹和图像不同,我也总是得到相同的encoded_gs_key链接( 0/cover/cover.jpg ,例如1/cover/cover.jpg所以根文件夹,图像和文件名相同)

Now I understand what happened. 现在我明白发生了什么。 I had a connection filter that letted me getting only through this path /api* I've deleted it and things seems to work better: 我有一个连接过滤器,该过滤器只允许我通过以下路径/api*我已将其删除,并且看起来工作得更好:

original connexionFilter class: 原始的connexionFilter类:

public class connexionFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // TODO Auto-generated method stub

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        String path = ((HttpServletRequest) request).getRequestURI();

        if(path.startsWith("/api"))
        {
            chain.doFilter(req, resp);
        }
    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM