春季启动图像上传到谷歌云存储桶不起作用

我想将图像上传到谷歌云存储,这是我的 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");

        }

    }


素胚勾勒不出你
浏览 102回答 2
2回答

大话西游666

最后我想出了这个代码:)。工作得很好。需要凭据才能将文件上传到 GCP 存储。您也可以从 JSON 格式生成凭据。https://cloud.google.com/docs/authentication/production&nbsp; &nbsp; &nbsp;Credentials credentials = GoogleCredentials.fromStream(new FileInputStream("C:\\Users\\sachinthah\\Downloads\\MCQ project -1f959c1fc3a4.json"));Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public CloudStorageHelper() throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @SuppressWarnings("deprecation")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @RequestMapping(method = RequestMethod.POST, value = "/imageUpload112")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public String uploadFile(@RequestParam("fileseee")MultipartFile fileStream)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throws IOException, ServletException {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String bucketName = "mcqimages";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkFileExtension(fileStream.getName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DateTimeFormatter dtf = DateTimeFormat.forPattern("-YYYY-MM-dd-HHmmssSSS");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DateTime dt = DateTime.now(DateTimeZone.UTC);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String dtString = dt.toString(dtf);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final String fileName = fileStream.getName() + dtString;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; File file = convertMultiPartToFile( fileStream );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BlobInfo blobInfo =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; storage.create(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BlobInfo&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .newBuilder(bucketName, fileName)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setAcl(new ArrayList<>(Arrays.asList(Acl.of(User.ofAllUsers(), Role.READER))))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build()&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;file.openStream()&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(blobInfo.getMediaLink());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return blobInfo.getMediaLink();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; private File convertMultiPartToFile(MultipartFile file ) throws IOException&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; File convFile = new File( file.getOriginalFilename() );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileOutputStream fos = new FileOutputStream( convFile );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fos.write( file.getBytes() );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fos.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return convFile;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; private void checkFileExtension(String fileName) throws ServletException {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (fileName != null && !fileName.isEmpty() && fileName.contains(".")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] allowedExt = {".jpg", ".jpeg", ".png", ".gif"};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (String ext : allowedExt) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (fileName.endsWith(ext)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new ServletException("file must be an image");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

德玛西亚99

我会尝试上传文件:public String uploadFile(@RequestParam("file") MultipartFile file) {&nbsp; &nbsp; if (file.isEmpty()) {&nbsp; &nbsp; &nbsp; &nbsp; //Set error message&nbsp; &nbsp; }&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String extension = FilenameUtils.getExtension(file.getOriginalFilename()); //Commons IO&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Get the file&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte[] bytes = file.getBytes();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ....&nbsp; &nbsp; }上传文件的一个很好的例子在这里:https : //www.baeldung.com/spring-file-upload
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java