- package lab.sodino.img;
- import java.io.IOException;
- import java.io.InputStream;
- import javax.microedition.io.Connector;
- import javax.microedition.io.file.FileConnection;
- import javax.microedition.midlet.MIDlet;
- import javax.microedition.midlet.MIDletStateChangeException;
-
- public class ImgType extends MIDlet {
- public ImgType() {
- }
- protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
- }
- protected void pauseApp() {
- }
- protected void startApp() throws MIDletStateChangeException {
- String prefix = "file:///root1/";
-
-
- testFile(prefix + "img.png");
- }
- public void testFile(String url) {
- try {
- int length = 10;
- FileConnection fc = (FileConnection) Connector.open(url);
- InputStream is = fc.openInputStream();
- byte[] data = new byte[length];
- is.read(data);
- String type = getType(data);
- System.out.println(url + " is " + type);
- is.close();
- fc.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public String getType(byte[] data) {
- String type = null;
-
- if (data[1] == 'P' && data[2] == 'N' && data[3] == 'G') {
- type = "PNG";
- return type;
- }
-
- if (data[0] == 'G' && data[1] == 'I' && data[2] == 'F') {
- type = "GIF";
- return type;
- }
-
- if (data[6] == 'J' && data[7] == 'F' && data[8] == 'I'
- && data[9] == 'F') {
- type = "JPG";
- return type;
- }
- return type;
- }
- }
posted on 2010-03-19 23:11
鹏凌 阅读(1854)
评论(0) 编辑 收藏 所属分类:
Java --j2ee