wood burning stoves 2.0*
The moose likes Servlets and the fly likes Streaming bytes to and from a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Streaming bytes to and from a servlet" Watch "Streaming bytes to and from a servlet" New topic
Author

Streaming bytes to and from a servlet

Bartosz Weselak
Greenhorn

Joined: Feb 03, 2011
Posts: 5
Hi,
it's my first post here, though I've been browsing in for solutions for couple of days.

I stomped on a problem I can't handle.
Namely I wrote a Java Web Start application that reads and writes to XML files
and those files are to be located on a server so that people using the app were all using the same database.

In order to do this I wrote two servlets - one to write and the other to read from files on the server
and I run them both on a Sun Java Application Server 8.2.
From my app I open an URLconnection to the servlets and the servlets rewrite form
- FileInputStream to response.getOutputStream (for reading)
- request.getInputStream to FileOutputStream (for writing)

The code goes like this:
- on the reading servlet:


- and reading from the servlet stream in the app:


And reading works.

But when I want to write to a file using following code...:
- on the writing servlet:


- and on the app:


... it doesn't work...
I did play around with sending serialized objects and that way I could write files to the server,
but I decided to use that method as the whole XML operations take place in the app,
and servlets only serve for streaming the files.

I usually don't post my problems, as i like to find solution on my own,
but I've been sitting on this one for a few days now and it's starting to get on my nerves...

Never played with Servlets and neither with the web apps before
and this is my first attempt, but it's getting discouraging =P

Could anyone pinpoint me my mistakes?


Thank you in advance,
MrError
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
... it doesn't work...
Can you explain in more detail? See the link ItDoesntWorkIsUseless
Bartosz Weselak
Greenhorn

Joined: Feb 03, 2011
Posts: 5
Well ummm it's just it - it doesn't work.
The XML doesn't get saved on the disk.

The code is seemingly the same with the exception that it's reading stream
from the applet and writing it to the FileOutputStream.
The other way around works perfectly,
but with writing there're no errors, no nothing.
The file just isn't written to the server.

When I used only one servlet for reading and writing, and one URLConnection,
it erased the contents of the files leaving them empty,
so I presume that opening the FileOutputStream works.
Maybe it has something to do with that transform function,
that is used to write to stream...
Bartosz Weselak
Greenhorn

Joined: Feb 03, 2011
Posts: 5
I tried this time to send entire source and transform it to result in the servlet:

- on the application side



- on the servlet side:



But I still don't get the output file...
Strangely enough the same piece of code worked for sending String objects,
but for the DOMSource it does not...


[UPDATE] It doesn't seem like the request reaches the servlet.
I've been trying to create other files just to try that and nothing happened.
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
Here's a guess. You have "localhost:8080/DefendersServlet/Writer". Try adding "http://" to the beginning of that. Note that it works when you type "localhost:8080/DefendersServlet/Writer" into a browser because the browser is user friendly enough to add it for you.
Bartosz Weselak
Greenhorn

Joined: Feb 03, 2011
Posts: 5
well actually it's a remote server.
I just replaced the URL with "localhost"...

But I know why my second approach didn't work - the DOMSource in not serializable...

[UPDATE]
I transformed the Result into String object which can be send through
ObjectOutputStream, so there are no more exceptions in the application.
Still the data doesn't get written to file on the server.
It looks like the Writing servlet does not respond to requests.
Bartosz Weselak
Greenhorn

Joined: Feb 03, 2011
Posts: 5
Ok.
I finally did it!
But I don't actually know why it should be this way.
Namely after writing to the servlet the application needed a response.
It doesn't matter what kind of response.
I just added a part for the application to wait for a respond and all of the sudden
it started correctly writing to file! ;)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Streaming bytes to and from a servlet
 
Similar Threads
Using Apache Commons Codec
creating jar file programatically
How to Actually UpLoad ?
file transfer over TCP problem
JarInputStream