Java Reference
In-Depth Information
catch(IOException ioEx)
{
ioEx.printStackTrace();
}
catch(ClassNotFoundException cnfEx)
{
cnfEx.printStackTrace();
}
}
private static void getFile(
ObjectInputStream inStream, String fi leType)
throws IOException, ClassNotFoundException
{
//Steps 3 and 4…
//(Note the unusual appearance of the typecast!)
byte[] byteArray = (byte[])inStream.readObject();
FileOutputStream mediaStream;
if (fi leType.equals("IMAGE"))
//Step 5…
mediaStream =
new FileOutputStream("image.gif");
else
//Must be a sound fi le…
//Step 5…
mediaStream =
new FileOutputStream("sound.au");
//Step 6…
mediaStream.write(byteArray);
}
}
If the request were for an image fi le and we wanted to display that image in our
application (without saving it to fi le), then we could create an ImageIcon object
holding the image by using an overloaded form of the ImageIcon constructor that
takes a byte array as its single argument:
ImageIcon image = new ImageIcon(byteArray);
Since ways of displaying image fi les once they have been downloaded have already
been covered, these methods will not be repeated here. However, we have not yet
looked at how sound fi les may be played. The next section deals with this issue.
As might be expected, the above method also allows us to send and receive video
fi les. The playing of such fi les, however, is not possible with the core Java SE and
will be covered in Sect. 11.4 .
Search WWH ::




Custom Search