在下载之前,从Blob URL路径读取和附加数据到文件

问题描述 投票:0回答:1

这是我第一次在项目中使用Java Spring启动,因为我主要使用C#,我需要从blob URL路径读取文件并将一些字符串数据(如密钥)附加到同一个文件中。在我的API下载文件之前流。

以下是我尝试过的方法:

  1. FileOutputStream / InputStream:抛出FileNotfoundException,因为它无法解析blob路径。
  2. URLConnection:这让我到了某个地方,我能够成功下载文件但是当我在下载之前尝试写入/附加一些值时,我失败了。 我一直在做的代码。 //EXTERNAL_FILE_PATH is the azure storage path ending with for e.g. *.txt URL urlPath = new URL(EXTERNAL_FILE_PATH); URLConnection connection = urlPath.openConnection(); connection.setDoOutput(true); //I am doing this as I need to append some data and the docs mention to set this flag to true. OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream()); out.write("I have added this"); out.close(); //this is where the issues exists as the error throws saying it cannot read data as the output is set to true and it can only write and no read operation is allowed. So, I get a 405, Method not allowed... inputStream = connection.getInputStream();

我不确定框架是否允许我修改URL路径中的某个文件并同时读取并下载相同的文件。

如果这里有更好的方法,请帮助我理解。

java spring-boot java-io spring-restcontroller urlconnection
1个回答
0
投票

从逻辑的角度来看,您不会将数据附加到URL的文件中。您需要创建新文件,写入一些数据,然后从URL文件中追加内容。算法可能如下所示:

  1. 在磁盘上创建新的File,也许在TMP文件夹中。
  2. 将一些数据写入文件。
  3. URL下载文件并将其附加到磁盘上的文件中。

一些好的文章可以从中开始:

  1. Download a File From an URL in Java
  2. How to download and save a file from Internet using Java?
  3. How to append text to an existing file in Java
  4. How to write data with FileOutputStream without losing old data?
© www.soinside.com 2019 - 2024. All rights reserved.