我在标题中遇到的这个问题与之前在这里提出的问题非常相似(Azure 存储:上传的文件大小为零字节),但它是针对 .NET 的,我的 Java 场景的上下文是我正在上传小尺寸 CSV文件(每个文件大约少于 5 Kb)。此外,API 代码使用我正在使用的最新版本的 Azure API,与另一个问题使用的 2010 版本形成对比。我不知道我错过了哪里,但另一种选择是在文件存储中进行,但当然,我的一些同行推荐了 blob 方法。到目前为止,我的代码主要基于将文件作为 blob 块上传到 Azure 示例 git [页面] 。我已经完成了容器设置和文件重命名步骤,这不是问题,但上传后,我的 Azure 域上的 blob 存储容器中的文件大小显示 0 字节。我尝试交替将文件转换为 FileInputStream 并将其作为流上传,但它仍然以相同的方式产生。fileName=event.getFilename(); //fileName is e.g eod1234.csvString tempdir = System.getProperty("java.io.tmpdir");file= new File(tempdir+File.separator+fileName); //try { PipedOutputStream pos = new PipedOutputStream(); stream= new PipedInputStream(pos); buffer = new byte[stream.available()]; stream.read(buffer); FileInputStream fils = new FileInputStream(file); int content = 0; while((content = fils.read()) != -1){ System.out.println((char)content); } //Outputstream was written as a test previously but didn't work OutputStream outStream = new FileOutputStream(file); outStream.write(buffer); outStream.close(); // container name is "testing1" CloudBlockBlob blob = container.getBlockBlobReference(fileName); if(fileName.length() > 0){ blob.upload(fils,file.length()); //this is testing with fileInputStream blob.uploadFromFile(fileName); //preferred, just upload from file }} 没有显示任何错误消息,只是我们知道该文件涉及 blob 存储并显示大小为 0 字节。这是一个仅上传 CSV 格式文件的单向过程。在 blob 容器中,它应该显示这些上传的文件,每个文件的大小为 1-5 KB。
3 回答

慕姐8265434
TA贡献1813条经验 获得超2个赞
而不是blob.uploadFromFile(fileName);
你应该使用blob.uploadFromFile(file.getAbsolutePath());
因为uploadFromFile
方法需要绝对路径。而且你不需要blob.upload(fils,file.length());
.

动漫人物
TA贡献1815条经验 获得超10个赞
是由于 Vaadin 中的上传组件的行为与平常不同 。CloudBlockBlob 或 BlobContainerUrl 方法均有效。
开箱即用的 Upload 组件需要手动将 FileOutputStream 实现为临时对象,这与随处可见的常见 servlet 对象不同。由于时间有限,我使用了他们的插件之一 EasyUpload,因为它已将 Viritin UploadFileHandler 合并到其中,而不是从头开始弄清楚如何流式传输对象。如果有更多时间,我肯定会在我的沙盒工作区中试用 MultiFileUpload 插件,它有更多有趣的东西。

慕丝7291255
TA贡献1859条经验 获得超6个赞
我在处理(从多部分文件复制的)文件时遇到了同样的问题,.png
我正在这样做:
File file = new File(multipartFile.getOriginalFilename());
Azure 上的 blob 是 0bytes 但是当我改成这个时:
File file = new File("C://uploads//"+multipartFile.getOriginalFilename());
它开始正确保存文件
添加回答
举报
0/150
提交
取消