-1

I want to set up the REST API to support file downloads via Java (The java part is not needed at the moment -- I am saying it in here so you can make your answer more specific for my problem).

How would I do that?

For example, I have this file in a folder (./java.jar), how can I stream it in such a way for it to be downloadable by a Java client?

I forgot to say that this, is for some paid-content. My app should be able to do this

Client: Post to server with username,pass.

Rest: Respond accordingly to what user has bought (so if it has bought that file, download it)

Client: Download file and put it in x folder.

I thought of encoding a file in base64 and then posting the encoded result into the usual .json (maybe with a nice name -- useful for the java application, and with the code inside -- though I would not know how I should rebuild the file at this point). <- Is this plausible? Or is there an easier way?

Also, please do not downvote if unnecessary, although there is no code in the question, that doesn't mean I haven't researched it, it just means that I found nothing suitable for my situation.

Thanks.

Community
  • 1
  • 1
IndieDev
  • 175
  • 1
  • 1
  • 12
  • Why would you need to encode anything. Just stream the file contents, and specify the appropriate Content-Type header. REST doesn't mean that the content needs to be JSON. – JB Nizet Nov 26 '17 at 20:43

2 Answers2

0

What you need is a regular file streaming, using a valid URL.

Below code is an excerpt from here

import java.net.*;
import java.io.*;

public class URLReader {
    public static void main(String[] args) throws Exception {

        URL oracle = new URL("http://www.oracle.com/");
        BufferedReader in = new BufferedReader(
        new InputStreamReader(oracle.openStream()));

        String inputLine;
        while ((inputLine = in.readLine()) != null)
            System.out.println(inputLine);
        in.close();
    }
}
Alp
  • 3,027
  • 1
  • 13
  • 28
  • This does indeed work, but it doesn't apply in my context. I have an application with paid content (that should not be accessed in any way whatsoever to the ones that did not pay). Also, the client is completely dynamic (It doesn't know what to download, what it should do is ask the server with the user log in info, and based on that it should update/download content) – IndieDev Nov 26 '17 at 20:47
  • 1
    just move the source under a password protected area. That's enough for protection. As for dynamic content, after user logs in, server can pass the correct URL to the client. No big deal. Above example still applies. – Alp Nov 26 '17 at 21:01
  • Depends on your server. Ask a different question, if you have any around this subject. Either me, or any other fellow people here will gladly answer it. – Alp Nov 26 '17 at 21:07
  • @Alp it does apply to your context. The client sends whatever request it needs to send to the server, and the server responds with a response with the appropriate content type, and the content of the file, (i.e. its bytes, without any transformation) as the body. Whether the file is directly accessible from the outside, whether it's actually a file or just a blob in the database doesn't matter. All you need is to send back the bytes of the file with the appropriate content type. – JB Nizet Nov 27 '17 at 07:38
  • @JBNizet We are on the same page. I mentioned file because question is asked around a concept of downloading a file. If file is behind password protected area (in this case, I assumed users are registered only if they are paid customers). Furthermore, fine grained control is still possible. – Alp Nov 27 '17 at 14:52
  • @Alp sorry, my previous comment was in fact for the OP. – JB Nizet Nov 27 '17 at 15:21
0

For your needs, based on your updated comments on the above answer, you could call your REST endpoint after user logs in(with Auth and other headers/body you wish to receive) and proceed to the download.

Convert your jar/downloadable content to bytes. More on this Java Convert File to Byte Array and visa versa

Later, in case if you dont want regular streaming as aforementioned in previous answers, you can put the byte content in the body as Base64 String. You can encode to Base64 from your byte array using something like below.

Base64.encodeToString(byte[],  Base64.NO_WRAP + Base64.URL_SAFE);

Reference from here: How to send byte[] and strings to a restful webservice and retrieve this information in the web method implementation

Again, there are many ways to do this, this is one of the ways you can probably do using REST.

St1id3r
  • 313
  • 5
  • 12