Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

transferring images

Status
Not open for further replies.

lowbk

Technical User
Nov 26, 2001
162
SG
hello, i need some advice on this.
currently, my program can transfer textual xml via http ie URLConnection, getOutputStream() and getInputStream()

i want to transfer image as well, is it easier to embed it inside the xml? if so, how? or is it easier to put a link in the xml stating the location of the image? but how to send the image over in the first place?

any kind soul pls advise
 
You could easily base64 encode the image file into the XML document and then on the other side you could base64 decode the file. There are a bunch of packages out there with base64 encoders and it is not that hard to write your own if you wanted to.

Another alternate is to treat the image as a Java object and just serialize/deserialize it.
 
You can transfer image using DataOutputStream/DataInputStream. IPO_z@cmmail.com
Garbage in,Garbage out
 
wushutwist and ipoz, thank you for your suggestion.

wushutwist, how do i serialize/deserialize it? what/where are the tools for doing so?

actually i have a vector of images, i want to serialize/deseralize the vector. guess that would be much easier than processing each image within the vector.
 
i suddenly remember java vector got a tostring(), so that solves the serializing, but how to convert it back to vector? any suggestion?

the vector contains images, and image name.
 
The following is some code snippet from our project,Maybe it will not help you however i hope it will give you some suggestion:

----------MyServlet.java
...
CachedRowSet crs = (CachedRowSet)ret;
while (crs.next()) {
byte[] pic = crs.getBytes("flat_pic"); //flat_pic is a field containing a flat picture
DataOutputStream out = new DataOutputStream(resp.getOutputStream());
out.write(pic);
out.close();
}
...

----------MyApplet.java
...
java.net.URLConnection con = url.openConnection();
DataInputStream in = new DataInputStream(con.getInputStream());
byte[] buf = new byte [100 * 1024];
ByteArrayOutputStream buffer = new ByteArrayOutputStream(100 * 1024);
int bytesRead = 0 ;
while((bytesRead = in.read(buf)) != -1) {
buffer.write(buf, 0, bytesRead);
}
in.close();

Toolkit tool = getToolkit();
Image image1 = tool.createImage(buffer.toByteArray());
...
I donot think it is a good code but i works .

Best Regards! IPO_z@cmmail.com
Garbage in,Garbage out
 
wushutwist and ipoz, i got my solution already.
i would like to share how it is done

on the sending side,
Vector v //assuming containing images, data etc
ByteArrayOutputStream tmpbyte = new ByteArrayOutputStream();
ObjectOutputStream tmpobjstream = new ObjectOutputStream(tmpbyte);
tmpobjstream.writeObject(v);
tmpobjstream.flush();


on the receiving side,
ByteArrayInputStream tmpbytestream = new ByteArrayInputStream....//read in and form the buffer
ObjectInputStream tmpobjstream = new ObjectInputStream(tmpbytestream);
Vector v = (Vector)tmpobjstream.readObject();




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top