Name: pa48320 Date: 02/19/2002
The guessContentTypeFromStream(InputStream istrm) on the java.net.URLConnection class returns null for some jpg images. These images are images read from Digital Cameras. Todays cameras all return images that have a magic code of 0xFF, 0xD8, 0xFF, 0xE1 which is not tested for in the code. Frankly that code could be made a bit more extensible. The following code demonstrates the problem. Call it with a jpg downloaded from a camera:
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class URLConnectionTest
{
public URLConnectionTest (String filename)
{
FileInputStream inStream = null;
try
{
inStream = new FileInputStream(filename);
}
catch (FileNotFoundException e)
{
System.out.println("File " + filename + " not found.");
}
try
{
InputStream bufferedStream = new java.io.BufferedInputStream(inStream);
System.out.println(" ContentType="+(java.net.URLConnection.guessContentTypeFromStream(bufferedStream)));
bufferedStream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] argv)
{
String filename;
if (argv.length > 0)
{
filename = argv[0];
URLConnectionTest failedTest = new URLConnectionTest(filename);
}
System.exit(0);
}
}
======================================================================