0

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

如何解决?让我知道需要任何细节。

代码:

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:

<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 显示一些额外字符,导致文件大小增加(以字节为单位)

4

2 回答 2

0

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

我不确定为什么它与“uploadFromFile”方法一起使用并且与 BlobClient 的“上传”方法有问题。下面是我使用的最终代码,它适用于不同的文件扩展名。任何人发现错误或对以下代码有建议,请告诉我,这对我有很大帮助。

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

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());
            }
        }
    }```
于 2021-10-20T12:38:28.980 回答
0

无需上传大文件,而是直接以 zip 文件或夹头上传它们

尝试使用此代码

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 线程

于 2021-10-14T12:18:04.073 回答