I want to upload a file to a servlet from .net application. File is uploading to the first servlet using WebClient.UploadFile on client side and Commons FileUpload on server side. I need to pass the file content to a second servlet which will then upload the file contents to a server. I have converted the bytes within the first servlet to string but not sure how to go about it from here... file content type needs to be multipart/form-based so do i need to create a file object and pass this to the second servlet...??

Then, from your second servlet, call getInputStream on the request object and read the data in.

Of course:
- if the two servlets are running in the same JVM, they can just "see" each other directly-- just use a standard producer-consumer model for one servlet to push the data to the other;
- if your network environment permits it, server 2 is free to open any old listening socket, and for server 1 to connect to bypass HTTP altogther and connect directly to that socket.

06-04-2009, 04:56 AM

Steve11235

Sounds like ASP.NET and a Java WAS. What Neil suggested will work if the two servers are allowed to communicate directly. If that is not the case, you can take the content you uploaded and run it through an HTML-safe encoding process (CODE64?) and put the content inside a text field in a form in a small HTML page. Use Javascript to immediately post the form to the second server (this avoids redirect issues at the browser).

06-04-2009, 05:15 AM

neilcoffey

Sorry, read too quickly and missed the .NET bit -- so is the "second Servlet" not actually a servlet, then? But either way, presumably .NET has a way to just "pull down the bytes" that are posted to it, no?

06-04-2009, 10:44 AM

hofsoc

The second servlet is a servlet - in the first servlet I will be completing some validation on the file contents (hence the reason I am converting to string). Once the validation passes I will convert string back to bytes and stream this on to the second servlet which will upload to a server. The response from the first servlet will be returned in byte format to the .NET app which is fine!

06-04-2009, 04:31 PM

neilcoffey

OK, then I think you can just do what I mentioned. N.B. I corrected my above code slightly-- I forgot the call to setDoOutput(true).

Be very very careful of conveting to a String and then back again. If I got a dollar for every time somebody posted about a problem that occurred because they'd done this, I'd have at least enough for a glass of Merlot. By all means convert to a string for the purposes of validation, but keep on to the original bytes and send those, unless you're super-sure that that's not what you need to do, and you fully understand character conversion etc.

06-04-2009, 11:09 PM

hofsoc

Great thanks - good to know about converting back and forth between string and bytes... ;)