0

This is my first hands on using Java Spring boot in a project, as I have mostly used C# and I have a requirement of reading a file from a blob URL path and appending some string data(like a key) to the same file in the stream before my API downloads the file.

Here are the ways that I have tried to do it:

  1. FileOutputStream/InputStream: This throws a FileNotfoundException as it is not able to resolve the blob path.
  2. URLConnection: This got me somewhere and I was able to download the file successfully but when I tried to write/append some value to the file before I download, I failed.

    the code I have been doing.

        //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(); 
    

I am not sure if the framework allows me to modify some file in the URL path and read it simultaneously and download the same.

Please help me in understanding if they is a better way possible here.

James Z
  • 11,838
  • 10
  • 25
  • 41
Sandeep
  • 258
  • 1
  • 6
  • 21

1 Answers1

0

From logical point of view you are not appending data to the file from URL. You need to create new file, write some data and after that append content from file from URL. Algorithm could look like below:

  1. Create new File on the disk, maybe in TMP folder.
  2. Write some data to the file.
  3. Download file from the URL and append it to file on the disk.

Some good articles from which you can start:

  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?
Michał Ziober
  • 31,576
  • 17
  • 81
  • 124
  • Thanks for the response. I create a docker file on deployment. Does the temp folder thus created in the root directory have access to the same at runtime? – Sandeep Feb 22 '19 at 06:35
  • @Sandeep I'm not sure I understand your question. I do not know how it works on `Docker`. You need to check in documentation. – Michał Ziober Feb 22 '19 at 07:29