Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Stream back two or more files  RSS feed

 
michael yue
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have a servlet that accepts some parameters and then return back a xml and a file in bytes back to the user. I used the following
to get the files
BufferedReader xmlvalue = new BufferedReader(new InputStreamReader(is));
String input = (String) xmlvalue.readLine();

Problem is I have to stream back two files, one in byte format and another in xml format. Is it possible to stream back two files at one time. Or is there any other suggestions.

Thanks
 
Paul Clapham
Sheriff
Posts: 22493
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have a protocol agreed between your program and whatever program will be receiving those files as to how you'll package them? If not, then that's the first thing you need to do. Just sending one followed by the other isn't going to work, because the receiver won't be able to separate the two.

But if you did something like sending the number of bytes (in a prearranged format) followed by the bytes, followed by the XML, a receiver could be written to handle that protocol. Or you could just put the two files into a ZIP archive. There are many ways to do it, you just have to design one yourself.
 
michael yue
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am looking into putting a separator in the bytes between the xml and the file. Is this possible? Now I am trying to research on ways to put and then detect this separator between the xml and file. Just want some opinion whether this is feasible.

Thanks.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Separators are a little treacherous. Mime multi-part upload uses them, so it's certainly possible. You have to make them easy to detect, yet unlikely to appear in real data.

I liked the idea of headers better. You could make them look like HTTP headers to make future readers more comfortable, like:

FileType=xxxxxx\nLength=nnnnnn\n\ntherealdatafollows

Back before XML, with plain old text data I had a lot of luck with delimiters. I scanned the data to find a character that was not in the data and put that first. There was some risk I wouldn't find one!

|record1|record2|record3|

You can nest these with different delimiters. Here are people with name and address fields, and address is broken into three fields. Wow, very COBOL-like.

|!name!/street/city/zip/!|!name!/street/city/zip/!|

And of course you could get all your data into XML maybe with base64 encoding on binary data that might include special characters.

Any of that sound right?
[ February 13, 2006: Message edited by: Stan James ]
 
michael yue
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been researching. And I cant seem to find a way to determine a way to determine separator to differentiate between files in bytes. The cosest I get is the StreamTokenizer but there is none for bytes. Is it good to convert the bytes to stream before getting the files?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!