亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

春季啟動圖像上傳到谷歌云存儲桶不起作用

春季啟動圖像上傳到谷歌云存儲桶不起作用

素胚勾勒不出你 2021-12-01 16:23:37
我想將圖像上傳到谷歌云存儲,這是我的 Spring Boot 代碼。但問題是這根本不起作用,給我這樣的錯誤:2018-10-22 15:22:55.628 錯誤 6172 --- [nio-8080-exec-6] oaccC[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] 在上下文中路徑[]拋出異常[請求處理失?。磺短桩惓J?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");        }    }
查看完整描述

2 回答

?
大話西游666

TA貢獻1817條經驗 獲得超14個贊

最后我想出了這個代碼:)。工作得很好。需要憑據才能將文件上傳到 GCP 存儲。您也可以從 JSON 格式生成憑據。


https://cloud.google.com/docs/authentication/production


     Credentials credentials = GoogleCredentials.fromStream(new FileInputStream("C:\\Users\\sachinthah\\Downloads\\MCQ project -1f959c1fc3a4.json"));


Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();


            public CloudStorageHelper() throws IOException {

            }



            @SuppressWarnings("deprecation")

            @RequestMapping(method = RequestMethod.POST, value = "/imageUpload112")

            public String uploadFile(@RequestParam("fileseee")MultipartFile 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;


                File file = convertMultiPartToFile( fileStream );


                BlobInfo blobInfo =

                        storage.create(

                                BlobInfo

                                        .newBuilder(bucketName, fileName)

                                        .setAcl(new ArrayList<>(Arrays.asList(Acl.of(User.ofAllUsers(), Role.READER))))

                                        .build()

        //                     file.openStream() 

        );

                System.out.println(blobInfo.getMediaLink());

                return blobInfo.getMediaLink();

            }



            private File convertMultiPartToFile(MultipartFile file ) throws IOException

            {

                File convFile = new File( file.getOriginalFilename() );

                FileOutputStream fos = new FileOutputStream( convFile );

                fos.write( file.getBytes() );

                fos.close();

                return convFile;

            }



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

                }

            }


查看完整回答
反對 回復 2021-12-01
?
德瑪西亞99

TA貢獻1770條經驗 獲得超3個贊

我會嘗試上傳文件:


public String uploadFile(@RequestParam("file") MultipartFile file) {

    if (file.isEmpty()) {

        //Set error message

    }

    else {

        try {

            String extension = FilenameUtils.getExtension(file.getOriginalFilename()); //Commons IO


            // Get the file 

            byte[] bytes = file.getBytes();

            ....

    }

上傳文件的一個很好的例子在這里:https : //www.baeldung.com/spring-file-upload


查看完整回答
反對 回復 2021-12-01
  • 2 回答
  • 0 關注
  • 120 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號