I wan’t to post a CSV file to a web service using the Jersey Client without having to buffer the csv content in memory.
So I started of with some code similar to this:
String csvContent = [the buffered CSV content];
Client c = Client.create();
WebResource r = c.resouce("http://example.com/services/service");
r.type("text/csv").post(csvContent);
I would like to avoid buffering the entire CSV content in memory before sending to the server, I am aware that I can send a File object using the client and jersey will deal with loading and sending the file, however in this case the CSV content will be generated automatically so what I would really like to do is just simply write it to an OutputStream which goes directly to the server rather than into memory… is there a way I can do this using the Jersey client?
It seems Jersey doesn’t support writing direct to the OutputStream however after much digging and merging of a few different thinks I learned along the way I managed to come up with this class.
Jersey supports passing an InputStream into the post method on WebResource which is read and the content written to the request body.
I am producing a CSV file from a ResultSet so I wrote a class that extends InputStream and when read() is called it gets a record from the ResultSet and builds a line of the CSV file and returns just one character from the line. Each time read() is called the next character is returned until the whole line is returned at which point the next record is read from the database and the process repeated until there are no records left.
I am using a library called OpenCSV to build the lines of the CSV file