将图片上传到Google Cloud Store,但一旦将其部署到Google Cloud,就会出现以下错误: - 错误:服务器错误 - 服务器遇到错误,无法完成您的请求。 - 请在30秒后重试。 谢谢你的帮助。
public class UploadfilesServlet extends HttpServlet {
public static final String BUCKETNAME = "bucket_endpoints";
public static final String FILENAME = "test.jpg";
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
URL res = getClass().getResource("transform_before.jpg");
File file = new File(res.getFile());
//Create bFile with byte array
byte[] bFile = new byte[(int) file.length()];
FileInputStream fileInputStream=null;
//convert file into array of bytes
fileInputStream = new FileInputStream(file);
fileInputStream.read(bFile);
fileInputStream.close();
FileService fileService = FileServiceFactory.getFileService();
GSFileOptionsBuilder optionsBuilder = new GSFileOptionsBuilder()
.setBucket(BUCKETNAME)
.setKey(FILENAME)
.setMimeType("image/jpeg")
.setAcl("public-read")
.addUserMetadata("myfield1", "my field value");
AppEngineFile writableFile = fileService.createNewGSFile(optionsBuilder.build());
boolean lock = true;
FileWriteChannel writeChannel = fileService.openWriteChannel(writableFile, lock);
writeChannel = fileService.openWriteChannel(writableFile, lock);
writeChannel.write(ByteBuffer.wrap(bFile));
// Now finalize
writeChannel.closeFinally();
resp.getWriter().println("Done writing...");
}
}