1

Reading a HTTP remote file and save it to the local disk is as easy as:

org.apache.commons.io.FileUtils.copyURLToFile(new URL("http://path/to.file"), new File("localfile.txt"));

But I'd like to read a file without having to save it to the disk, just keep it in memory and read from there.

Is that possible?

chrisw
  • 3,076
  • 2
  • 22
  • 33
membersound
  • 66,525
  • 139
  • 452
  • 886
  • Apache HttpClient has an example to do this: http://hc.apache.org/httpclient-3.x/tutorial.html – sanastasiadis Jan 20 '16 at 09:10
  • 1
    Please also refer to this community wiki answer: http://stackoverflow.com/questions/2793150/using-java-net-urlconnection-to-fire-and-handle-http-requests/2793153#2793153 – sanastasiadis Jan 20 '16 at 09:11

4 Answers4

4

You can use java.net.URL#openStream()

InputStream input = new URL("http://pat/to.tile").openStream();
// ...
Gianni B.
  • 2,551
  • 15
  • 30
2

Nice to see that you're using apache commons, you can do the following: -

final URL url = new URL("http://pat/to.tile");
final String content = IOUtils.toString(url.openStream(), "UTF-8"); // or your preferred encoding

Alternatively, you can just access the stream and do as you want with it. You don't need to use apache commons to get a String if you use an InputStreamReader, but there's no reason not to since you're already using commons-io

As others have mentioned, if you just want it in memory without processing the stream into String, just url.openStream()

chrisw
  • 3,076
  • 2
  • 22
  • 33
0

You can do this like:

  public static String getContent(String url) throws Exception {
        URL website = new URL(url);
        URLConnection connection = website.openConnection();
        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                    connection.getInputStream()));

        StringBuilder response = new StringBuilder();
        String inputLine;

        while ((inputLine = in.readLine()) != null) 
            response.append(inputLine);

        in.close();

        return response.toString();
    }
Joel Geiser
  • 762
  • 8
  • 23
0

If you are happy to introduce 3rd party libraries then OkHttp has an example that does this.

OkHttpClient client = new OkHttpClient();

String url = "http://pat/to.tile";
Request request = new Request.Builder()
  .url(url)
  .build();

Response response = client.newCall(request).execute();
String urlContent = response.body().string();

The library would also parse and make available all HTTP protocol related headers etc.

Mark
  • 27,389
  • 7
  • 55
  • 88