gpt4 book ai didi

java - 本地镜像上传未显示在 Google App Engine 上

转载 作者:行者123 更新时间:2023-12-02 09:56:08 24 4
gpt4 key购买 nike

这是我在服务器端的上传功能:

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

这是我在客户端(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)
}
);
}

现在我使用此方法检索本地文件的公共(public) URL(App Engine Google Cloud Storage 模拟服务器上传并将其放置在本地空间中)

示例代码:

...
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);
...

基本上,我在函数的另一部分中所做的就是解析所有文件并过滤我感兴趣的文件。

它可以工作,我得到带有encoded_gs_key的URL,但什么也没有显示。我的上传有问题吗?即使 BlobStore 在指定位置找不到文件,它也会提供链接吗?另一件事是,即使我的文件夹和图像不同(0/cover/cover.jpg,例如 1/cover/cover.jpg 所以根文件夹不同,图像不同但文件名相同)

最佳答案

现在我明白发生了什么了。我有一个连接过滤器,让我只能通过这个路径 /api* 我已经删除了它,事情似乎工作得更好:

原始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

}

}

关于java - 本地镜像上传未显示在 Google App Engine 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55996253/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com