利用J2ME中提供的可选包JSR75提供的功能,浏览手机的文件夹,并且将用户的照片上传到服务器,实现相册的功能。
完全实现了浏览手机的文件系统的功能。但是在浏览文件系统的时候,需要用户确认。我在使用Nokia6233的手机测试的时候,由于每次访问文件,都需要确认,所以很烦人。不知道大家有没有什么好的方法。如果方便告诉我的话,请用cuilichen@hotmail.com与我联系,多谢。
import javax.microedition.lcdui.*;import java.util.Enumeration;import javax.microedition.io.file.FileSystemRegistry;import javax.microedition.io.file.FileConnection;import javax.microedition.io.Connector;import java.util.Vector;import java.io.*;import javax.microedition.io.HttpConnection;
/** * <p>Title: </p> * * <p>Description: </p> * * <p>Copyright: Copyright (c) 2005</p> * * <p>Company: </p> * * @author cuilichen * @version 1.0 */public class Brower implements Runnable, CommandListener { /** * 进入的历史记录 * 其中的成员每三个为一组 */ Vector history; /** * 用来显示当前的文件夹下面的内容的List */ List folder; /** * 当前的路径 */ String cPath; /** * 文件夹中各个对象的URL */ String[] children; /** * 文件夹中的各个对象是否是子文件夹 */ byte[] isDir; /** * 在List中,表示该对象是文件夹还是文件的图标 */ Image[] logos; /** * 图片文件的数据 */ byte[] data;
/** * 构造函数 */ public Brower() { history = new Vector(); }
/** * 初始化,显示根目录 */ public void init() { int roots = this.getRoots(); if (roots > 0) { //得到根目录列表 logos = new Image[2]; //准备图标 logos[0] = Core.getImage("logoF");//文件的图标 logos[1] = Core.getImage("logoD");//文件夹的图标 if (prepare2show()) { //显示列表成功 showList(); return; } } Core.setDisplay(Core.core); Core.received.addElement("<show>调用手机的浏览器失败"); }
/** * 得到根目录列表数组 * @return int */ private int getRoots() { Enumeration rootEnum = FileSystemRegistry.listRoots(); Vector v = new Vector(); while (rootEnum.hasMoreElements()) { String root = (String) rootEnum.nextElement(); v.addElement(root); } if (v.size() == 0) { return -1; } else { children = new String[v.size()]; v.copyInto(children); cPath = ""; return children.length; } }
/** * 得到当前目录下的内容,查看各项内容是文件夹还是文件 * @return boolean 是否成功 */ private boolean prepare2show() { isDir = new byte[children.length]; FileConnection fc = null; try { for (int i = 0; i < children.length; i++) { fc = (FileConnection) Connector.open("file:///" + cPath + children[i]); if (fc.isDirectory()) { isDir[i] = 1; for (int j = 0; j < i; j++) { //处理,保证先排文件夹,后排文件 if (isDir[j] == 0) { String temp = children[i]; for (int k = i; k > j; k--) { children[k] = children[k - 1]; } children[j] = temp; isDir[i] = 0; isDir[j] = 1; break; } } } else { isDir[i] = 0; } fc.close(); fc = null; } history.addElement(children); history.addElement(isDir); history.addElement(cPath); return true; } catch (Exception ex) { ex.printStackTrace(); return false; } finally { if (fc != null) { try { fc.close(); } catch (IOException ex1) { } } } }
/** * 显示文件夹里面的内容的列表 */ private void showList() { folder = new List(cPath, List.IMPLICIT); if (cPath.length() == 0) { folder.setTitle("我的手机"); } if (children.length > 0) { for (int i = 0; i < children.length; i++) { folder.append(children[i], logos[isDir[i]]); } folder.addCommand(new Command("确定", Command.OK, 1)); } else { folder.append("文件夹是空的", null); } if (history.size() > 3) { folder.addCommand(new Command("返回", Command.BACK, 1)); } folder.setCommandListener(this); Core.setDisplay(folder); }
/** * 得到指定文件夹中的各个对象的URL * @return int 对象的数量 */ private int getChildren() { FileConnection fc = null; try { System.out.println("file:///" + cPath); fc = (FileConnection) Connector.open("file:///" + cPath); Vector v = new Vector(); Enumeration child = fc.list(); while (child.hasMoreElements()) { String fileName = (String) child.nextElement(); v.addElement(fileName); } if (v.size() > 0) { children = new String[v.size()]; v.copyInto(children); return children.length; } else { children = new String[0]; return 0; } } catch (Exception ex) { return -1; } finally { if (fc != null) { try { fc.close(); } catch (IOException ex1) { } } } }
/** * 显示“返回”之后的内容 * @param father boolean 是否是返回上一层 */ private void getBack(boolean father) { if (father) { for (int i = 0; i < 3; i++) { history.removeElementAt(history.size() - 1); } } children = (String[]) history.elementAt(history.size() - 3); //里面的对象的名称 isDir = (byte[]) history.elementAt(history.size() - 2); //里面的对象是不是文件夹 cPath = (String) history.elementAt(history.size() - 1); //文件夹的绝对路径 showList(); }
/** * 得到文件的数据 * @param fileName String * @return boolean 读取的操作是否成功 */ private boolean getData(String fileName) { FileConnection fc = null; InputStream is = null; try { fc = (FileConnection) Connector.open("file:///" + fileName); is = fc.openInputStream(); data = new byte[is.available()]; is.read(data); return true; } catch (Exception ex) { data = null; return false; } finally { if (fc != null) { try { fc.close(); } catch (IOException ex1) { } } if (is != null) { try { is.close(); } catch (IOException ex2) { } } } }
/** * implements CommandListener * @param c Command * @param d Displayable */ public void commandAction(Command c, Displayable d) { if (d == folder) { if ("返回".equals(c.getLabel())) { //返回 getBack(true); } else { //确定 int index = folder.getSelectedIndex(); if (index >= isDir.length) { return; } if (isDir[index] == 0) { String fileName = cPath + folder.getString(index); if (fileName.endsWith(".jpg") || fileName.endsWith(".JPG") || fileName.endsWith(".png") || fileName.endsWith(".PNG")) { if (getData(fileName)) { new Thread(this).start(); } else { System.out.println("读取文件失败"); } } } else { cPath += folder.getString(index); int flag = getChildren(); if (flag >= 0) { //得到下层内容成功 if (prepare2show()) { showList(); return; } } getBack(false); //得到下层的内容失败,那么显示原来的内容 } } } }
/** * implement Runnable * 向服务器发送照片 * 服务器是使用tomcat和servlet实现的 */ public void run() { String server = "http://218.200.0.1:8080/Photo/receiver"; HttpConnection conn = null; OutputStream os = null; try { conn = (HttpConnection) Connector.open(server, 3, true); conn.setRequestProperty("Content-Length", "" + data.length); os = conn.openOutputStream(); os.write(data); os.close(); } catch (Exception ex) { ex.printStackTrace(); } finally { try { if (os != null) { os.close(); } } catch (Exception ex) {} try { if (conn != null) { conn.close(); } } catch (Exception ex) {} }
}}
