[英]local image upload not showing on Google App Engine
這是我在服務器端的上傳功能:
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)
}
);
}
現在,我使用此方法來檢索本地文件的公共網址(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也會提供鏈接嗎? 還有一件事是,即使我的文件夾和圖像不同,我也總是得到相同的encoded_gs_key
鏈接( 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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.