我想将图像上传到谷歌云存储,这是我的 Spring Boot 代码。但问题是这根本不起作用,给我这样的错误:
2018-10-22 15:22:55.628 错误 6172 --- [nio-8080-exec-6] oaccC[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] 在上下文中路径[]抛出异常[请求处理失败;嵌套异常是 java.lang.IllegalArgumentException: Invoked method public abstract java.io.InputStream org.apache.commons.fileupload.FileItemStream.openStream() throws java.io.IOException is no accessor method!] 具有根本原因
请帮我。以下是我写的代码
private static Storage storage = null;
// [START init]
static {
storage = StorageOptions.getDefaultInstance().getService();
}
@SuppressWarnings("deprecation")
@RequestMapping(method = RequestMethod.POST, value = "/imageUpload")
public String uploadFile(FileItemStream fileStream)
throws IOException, ServletException {
String bucketName = "mcqimages";
checkFileExtension(fileStream.getName());
DateTimeFormatter dtf = DateTimeFormat.forPattern("-YYYY-MM-dd-HHmmssSSS");
DateTime dt = DateTime.now(DateTimeZone.UTC);
String dtString = dt.toString(dtf);
final String fileName = fileStream.getName() + dtString;
BlobInfo blobInfo =
storage.create(
BlobInfo
.newBuilder(bucketName, fileName)
.setAcl(new ArrayList<>(Arrays.asList(Acl.of(User.ofAllUsers(), Role.READER))))
.build(),
fileStream.openStream());
return blobInfo.getMediaLink();
}
private void checkFileExtension(String fileName) throws ServletException {
if (fileName != null && !fileName.isEmpty() && fileName.contains(".")) {
String[] allowedExt = {".jpg", ".jpeg", ".png", ".gif"};
for (String ext : allowedExt) {
if (fileName.endsWith(ext)) {
return;
}
}
throw new ServletException("file must be an image");
}
}
大话西游666
德玛西亚99
相关分类