spring-boot - Spring Boot + Azure SDK,复制到 Azure 存储帐户时文件末尾的额外字符
问题描述
将文件上传到存储帐户后,文件末尾会添加一些额外的字符。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 显示一些额外字符,导致文件大小增加(以字节为单位)
解决方案
感谢@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());
}
}
}```