将 xml 文件上传到 Google Cloud Storage 时出错

我想将文件上传到 Google Cloud Storage,但是出现如下错误:


java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkNotNull

我要上传到 Google Cloud Storage 的 xml 文件的格式:


<set xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">

文件和谷歌存储桶,我想将文件上传到存储桶。


字符串文件名 = “data.xml”


String fileBucket = "上传文件";


 public static void uploadFile(String fileName, String fileBucket)

            throws IOException {

        final GcsService gcsService = GcsServiceFactory.createGcsService(new RetryParams.Builder()

                .initialRetryDelayMillis(10)

                .retryMaxAttempts(10)

                .totalRetryPeriodMillis(50000)//15000

                .build());

        gcsService.createOrReplace(

                new GcsFilename(fileBucket, fileName),

                new GcsFileOptions.Builder().mimeType("application/xml")

                        .acl("public-read")

                        .cacheControl("public, max-age=0").build());

    }


至尊宝的传说
浏览 124回答 1
1回答

跃然一笑

要将文件上传到 Google Cloud Storage,您需要这些StorageOptions服务。您可以查看有关Uploading Objects的文档。这会将Hello, Cloud Storage!字符串上传到blob_name存储桶中名为bucket. 您只需根据项目的需要更改名称。上传您的本地文件之一。创建一个函数,该函数将读取文件的数据并将它们返回到将数据上传到存储桶的主函数。我自己和以下代码做了一些编码,成功上传了包含您上面提到的数据的文件。读取文件的函数:它将从本地存储(例如 Cloud Shell)中读取文件并返回所有数据。private String readFile(){&nbsp; &nbsp; &nbsp; // The name of the file to open.&nbsp; &nbsp; &nbsp; &nbsp; String fileName = "PATH/TO/THE/FILE/THAT/IS/GOING/TO/BE/UPLOADED/FILE_NAME/xml";&nbsp; &nbsp; &nbsp; &nbsp; // This will reference one line at a time&nbsp; &nbsp; &nbsp; &nbsp; String line = null;&nbsp; &nbsp; &nbsp; &nbsp; // This will be the full file after reading&nbsp; &nbsp; &nbsp; &nbsp; String output = "";&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // FileReader reads text files in the default encoding.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileReader fileReader =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new FileReader(fileName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Always wrap FileReader in BufferedReader.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BufferedReader bufferedReader =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new BufferedReader(fileReader);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while((line = bufferedReader.readLine()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(line);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output = output + line;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Always close files.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bufferedReader.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch(FileNotFoundException ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output = output + "Unable to open file '" + fileName + "'";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch(IOException ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output = output + "Error reading file '" + fileName + "'";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; return output;&nbsp; }上传功能:它将使用将从文件中读取的所有数据并将它们上传到存储桶中的新文件。文档代码之间的区别在于...readFile().getBytes(UTF_8)...调用位置。我们添加了将返回所有数据以供上传的函数,而不是字符串。public String uploadFile(){&nbsp; &nbsp; &nbsp; &nbsp; String bucket_name = "BUCKET_NAME";&nbsp; &nbsp; &nbsp; &nbsp; String file_name = "PATH/TO/WHERE/THE/FILE/WILL/BE/UPLOADED/FILE_NAME.xml"&nbsp; &nbsp; &nbsp; &nbsp; Storage storage = StorageOptions.getDefaultInstance().getService();&nbsp; &nbsp; &nbsp; &nbsp; BlobId blobId = BlobId.of(bucket_name, file_name);&nbsp; &nbsp; &nbsp; &nbsp; BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();&nbsp; &nbsp; &nbsp; &nbsp; Blob blob = storage.create(blobInfo, readFile().getBytes(UTF_8));}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java