尝试使用startCopy()将文档从公共网址上传到Blob时,无法上传文件的内容
使用uploadFromFile(filePath)上载相同的URL
无效代码
URL url =new URL ("http://irpages2.equitystory.com/download/companies/douglasgmbh/Pres_web/6M_FY2018-19_InvestorUpdate.pdf");
newBlobReference.startCopy(new URI(url.toString()));
工作代码
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
try (ReadableByteChannel rbc = Channels.newChannel(conn.getInputStream());
final FileOutputStream fos = new FileOutputStream(filePath)) {
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
}
newBlobReference.uploadFromFile(filePath);
想要上传文档而不必先在本地系统上下载文档,因为这需要花费大量时间来存储大量文档。 其次,即使容器中存在Blob,也无法读取使用startCopy()下载的URL。
答案 0 :(得分:0)
我尝试使用startCopy
并获得了与您相同的结果,该文件没有内容。而且我不知道原因,所以我使用BlobOutputStream
上传了文件,并且可以正常工作。下面是我的代码,您可以尝试一下。
String connectionString = String.format("DefaultEndpointsProtocol=http;AccountName=%s;AccountKey=%s", ACCOUNT_NAME, ACCOUNT_KEY);
CloudStorageAccount account = CloudStorageAccount.parse(connectionString);
CloudBlobContainer container = null;
CloudBlobClient client = account.createCloudBlobClient();
container = client.getContainerReference("blobcontainer");
CloudBlockBlob blob = container.getBlockBlobReference("6M_FY2018-19_InvestorUpdate.pdf");
URL uri =new URL("http://irpages2.equitystory.com/download/companies/douglasgmbh/Pres_web/6M_FY2018-19_InvestorUpdate.pdf");
InputStream is=uri.openStream();
BlobOutputStream blobOutputStream = blob.openOutputStream();
int next = is.read();
while (next != -1) {
blobOutputStream.write(next);
next = is.read();
}
blobOutputStream.close();