Hey guys,
I am pretty new to HttpComponents and just wondered if it is possible to stream
data from input stream of unknown length to an upload servlet.
I've already tried to get it working with ClientChunkEncodedPost example
bundled with HttpComponents 4.2.3 for the client side as well as
ServletFileUpload for the server side.
However, HttpClient.execute waits until EOF which will never be reached at this
stage.
This is what I have:
1. Client:
pout = new PipedOutputStream();
pin = new PipedInputStream(pout, 8192);
writer = new OutputStreamWriter(pout, "UTF-8");
csvWriter = new CsvListWriter(writer,
CsvPreference.EXCEL_PREFERENCE);
csvWriter.writeHeader(header);
client = new DefaultHttpClient();
client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
HttpVersion.HTTP_1_1);
post = new HttpPost("http://localhost/Foo/Servlet");
MultipartEntity entity = new
MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
/* csvWriter is not written to at this stage, so is pin */
entity.addPart("file", new InputStreamBody(pin, "text/csv",
fileName));
post.setEntity(entity);
/* here it blocks until EOF */
HttpResponse response = client.execute(post);
2. Server:
protected void processRequest(HttpServletRequest httpRequest,
HttpServletResponse httpResponse, Map<RequestParam, String> params)
throws Exception {
InputStream in = null;
try {
if(!ServletFileUpload.isMultipartContent(httpRequest)) {
throw new Exception("Not multipart");
}
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator it = upload.getItemIterator(httpRequest);
while(it.hasNext()) {
FileItemStream item = it.next();
System.out.println(item.getFieldName());
in = item.openStream();
if(item.isFormField()) {
/* ... */
} else {
/* Write upload to database */
}
}
finally {
IOUtils.closeQuietly(in);
}
}
Any help would be appreciated. Thanks!
Br, Stefan