[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2148
  • Last Modified:

Easy question: from InputStream to File (FileOutputStream)

Hi,
   Can anyone help with this problem:

I have an InputStream coming over from HttpClient (from an HTTP GET request), and wish to write the data coming in from the InputStream object to a file (say, "raw.xml"); I can't seem to find any information online on how to connect InputStream to FileOutputStream or something similar...

Any help will be appreciated!
0
NGInterface
Asked:
NGInterface
  • 5
2 Solutions
 
CEHJCommented:
method.getResponseAsStream()

and copy it
0
 
CEHJCommented:
Sorry, it's

method.getResponseBodyAsStream()
0
 
CEHJCommented:
You could also do

FileOutputStream out = new FileOutputStream("x.html");
out.write(method.getResponseBody());
out.close();
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
NGInterfaceAuthor Commented:
Oh, I mean, I already got method.getResponseBodyAsStream() -- I just don't know how to turn that into a file...the amount of information coming over HTTP is very large, so responseBody() may not be the right approach...
0
 
CEHJCommented:
FileOutputStream out = new FileOutputStream("x.html");
final int BUFFER_SIZE = 1 << 10 << 3; //8KiB buffer
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = -1;
InputStream in = method.getResponseBodyAsStream();
while ((bytesRead = in.read(buffer)) > -1) {
      out.write(buffer, 0, bytesRead);
}
in.close();
out.close();
0
 
objectsCommented:
> InputStream in = method.getResponseBodyAsStream();

better off using a buffered stream

InputStream in = new BufferedInputStream(method.getResponseBodyAsStream());
0
 
CEHJCommented:
:-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now