I'm trying to upload files to CS using the CS Web Services by means of streams and not passing all the content in a byte.
I have looked at the developer Guide and used their sample code to upload a file to CS successfully. The only problem is that with their example they use a byte that contains the whole contents of the specific file. We need to upload really big files and obviously we are getting java.lang.OutOfMemoryError exceptions...
The example code states that one needs to Put the contents of the file with the following API call :
I noticed that their is also a HTTPConnection.Put(String, HttpOutputStream)
I tried using HttpOutputStream, but I'm still geting OutOfMemoryError. My code is as follows:
HttpOutputStream httpOutputStream = new HttpOutputStream();
HTTPResponse response = httpConnection.Put(url.getFile(), httpOutputStream);
//Write contents of input stream on the http output stream...
long numWritten = 0;
byte buffer = new byte;
while((numRead = inputStream.read(buffer)) != -1)
httpOutputStream.write(buffer, 0, numRead);
numWritten += numRead;
System.out.println("Written " + numWritten + "bytes so far...");
//Close the streams...
What am I doing wrong? Please, any help will be much appreciated...
Thanks in advance.
I have solved the problem - for future references or for anybody else having the same problem read below:
The JavaDoc for HttpOutputStream states the following: "There are two constructors for this class, one taking a length parameter, and one without any parameters. If the stream is created with a length then the request will be sent with the corresponding Content-length header and anything written to the stream will be written on the socket immediately. This is the preferred way. If the stream is created without a length then one of two things will happen: if, at the time of the request, the server is known to understand HTTP/1.1 then each write() will send the data immediately using the chunked encoding. If, however, either the server version is unknown (because this is first request to that server) or the server only understands HTTP/1.0 then all data will be written to a buffer first, and only when the stream is closed will the request be sent."
Constructing the HttpOutputStream with the size of the content to upload solved the problem. I guess the HTTP version was unknown or Oracle only understands HTTP/1.0 (anybody that can clear this up?), causing everything to be buffered (in memory) only to be send when the streams was closed, which in the case of a big file caused OutOfMemeryError.