首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring Boot + Azure SDK,复制到Azure存储帐户时文件末尾的额外字符

Spring Boot + Azure SDK,复制到Azure存储帐户时文件末尾的额外字符
EN

Stack Overflow用户
提问于 2021-10-14 03:05:37
回答 2查看 98关注 0票数 0

将文件上传到存储帐号后,文件末尾会添加一些额外的字符。1.33gb文件没有问题,观察到2.22gb文件的大小差异。下面是代码片段和pom.xml细节。

如何解决?如果需要任何细节,请告诉我。

代码:

代码语言:javascript
复制
private boolean upload(final MultipartFile file) throws IOException {           
BlobClientBuilder blobClientBuilder = new BlobClientBuilder();
blobClientBuilder.endpoint(STORAGE_URL).connectionString(storageConnectionString);          blobClientBuilder.containerName(CONTAINER_NAME);
BlobClient blobClient = blobClientBuilder.blobName(file.getOriginalFilename()).buildClient();
blobClient.upload(file.getInputStream(), file.getSize());
boolean uploadStatus = blobClient.exists();

pom.xml:

代码语言:javascript
复制
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.2</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
<dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-core</artifactId>
            <version>1.18.0</version>
        </dependency>
        <dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-storage-blob</artifactId>
            <version>12.12.0</version>
            <exclusions>
                <exclusion>
                    <groupId>io.projectreactor</groupId>
                    <artifactId>reactor-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.projectreactor/reactor-core -->
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-core</artifactId>
            <version>3.4.8</version>
            <!--$NO-MVN-MAN-VER$ -->
            <!-- Please don't remove/degrade the version, possible for compatibility 
                issues -->
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.projectreactor.netty/reactor-netty -->
        <dependency>
            <groupId>io.projectreactor.netty</groupId>
            <artifactId>reactor-netty</artifactId>
            <version>1.0.9</version>
            <!--$NO-MVN-MAN-VER$ -->
            <!-- Please don't remove/degrade the version, possible for compatibility 
                issues -->
        </dependency>

1.33gb文件上传正确,但2.22gb显示一些额外字符,导致文件字节大小增加

EN

回答 2

Stack Overflow用户

发布于 2021-10-14 12:18:04

不是直接上传大文件,而是以zip文件或chucks格式上传

尝试使用此代码

代码语言:javascript
复制
public static void uploadFilesByChunk() {
                String connString = "<conn str>";
                String containerName = "<container name>";
                String blobName = "UploadOne.zip";
                String filePath = "D:/temp/" + blobName;

                BlobServiceClient client = new BlobServiceClientBuilder().connectionString(connString).buildClient();
                BlobClient blobClient = client.getBlobContainerClient(containerName).getBlobClient(blobName);
                long blockSize = 2 * 1024 * 1024; //2MB
                ParallelTransferOptions parallelTransferOptions = new ParallelTransferOptions()
                                .setBlockSizeLong(blockSize).setMaxConcurrency(2)
                                .setProgressReceiver(new ProgressReceiver() {
                                        @Override
                                        public void reportProgress(long bytesTransferred) {
                                                System.out.println("uploaded:" + bytesTransferred);
                                        }
                                });

                BlobHttpHeaders headers = new BlobHttpHeaders().setContentLanguage("en-US").setContentType("binary");

                blobClient.uploadFromFile(filePath, parallelTransferOptions, headers, null, AccessTier.HOT,
                                new BlobRequestConditions(), Duration.ofMinutes(30));
        }

有关更多详细信息,请参阅此SO Thread

票数 0
EN

Stack Overflow用户

发布于 2021-10-20 12:38:29

感谢@ShrutiJoshi-MT的代码片段。

我不确定为什么它使用BlobClient的'uploadFromFile‘方法和'upload’方法有问题。下面是我正在使用的最终代码,它适用于不同的文件扩展名。任何人发现错误或对以下代码有建议请让我知道,它对我有很大帮助。

首先将Multipartfile复制到本地文件,然后提供路径。

代码语言:javascript
复制
public boolean uploadWithFile(final MultipartFile multipartFile) throws Exception {
        logger.info("uploadWithFile started");
        File file = null;
        try {
            String fileName = multipartFile.getOriginalFilename();
            file = new File(fileName);
            logger.info("uploadWithFile fileName: {}", fileName);
            Path path = Paths.get(fileName);
            logger.debug("Copying from MultipartFile to file");
            try (InputStream inputStream = multipartFile.getInputStream()) {
                Files.copy(inputStream, path, StandardCopyOption.REPLACE_EXISTING);
            }
            logger.debug("Copied from MultipartFile to file");
            String filePath = file.getPath();
            logger.debug("Copied file name: {}", file.getName());
            logger.debug("Copied file Path: {}", filePath);
            logger.debug("Copied file length: {}", file.length());
            
            String containerName = "temp";
            String storageConnectionString = "<primarykey> or <secondarykey>";
            BlobClientBuilder blobClientBuilder = new BlobClientBuilder();
            blobClientBuilder.endpoint(STORAGE_URL).connectionString(storageConnectionString);
            blobClientBuilder.containerName(containerName);
            BlobClient blobClient = blobClientBuilder.blobName(fileName).buildClient();
            logger.debug("uploading to storage account");
            blobClient.uploadFromFile(filePath);
            logger.debug("uploaded to storage account");
            boolean uploadStatus = blobClient.exists();
            logger.debug("uploaded status : {}", uploadStatus);
            logger.info("uploadWithFile ended");
            return uploadStatus;
        } catch (Exception exception) {
            logger.error("uploadWithFile upload failed: {}", exception);
            throw exception;
        } finally {
            if (Objects.nonNull(file) && file.exists()) {
                logger.debug("delete file: {}", file.getName());
                file.delete();
                logger.debug("deleted file: {}", file.getName());
            }
        }
    }```
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69564618

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档