J2ME手机本地存储文件的上传,核心代码如下:

Java代码  收藏代码
  1. package com.mopietek;  
  2.   
  3. import javax.microedition.midlet.MIDlet;  
  4. import javax.microedition.midlet.MIDletStateChangeException;  
  5.   
  6. import com.sun.lwuit.Display;  
  7.   
  8.   
  9. public class MainMIDlet extends MIDlet{  
  10.   
  11.       
  12.     private MainPanel panel = null;  
  13.       
  14.     protected void destroyApp(boolean unconditional)  
  15.             throws MIDletStateChangeException {  
  16.         // TODO Auto-generated method stub  
  17.           
  18.     }  
  19.   
  20.     protected void pauseApp() {  
  21.         // TODO Auto-generated method stub  
  22.           
  23.     }  
  24.   
  25.     protected void startApp() throws MIDletStateChangeException {  
  26.   
  27.         Display.init(this);  
  28.         panel = new MainPanel(this);  
  29.     }  
  30.       
  31.     public void exit(){  
  32.           
  33.         try{  
  34.         destroyApp(false);  
  35.         }catch(MIDletStateChangeException e){  
  36.             e.printStackTrace();  
  37.         }  
  38.         this.notifyDestroyed();  
  39.     }  
  40.   
  41. }  


Java代码  收藏代码
  1. package com.mopietek;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.io.OutputStream;  
  7. import java.util.Enumeration;  
  8.   
  9. import javax.microedition.io.Connector;  
  10. import javax.microedition.io.HttpConnection;  
  11. import javax.microedition.io.file.FileConnection;  
  12. import javax.microedition.io.file.FileSystemRegistry;  
  13.   
  14. import com.sun.lwuit.Command;  
  15. import com.sun.lwuit.Component;  
  16. import com.sun.lwuit.Container;  
  17. import com.sun.lwuit.Dialog;  
  18. import com.sun.lwuit.Form;  
  19. import com.sun.lwuit.Label;  
  20. import com.sun.lwuit.List;  
  21. import com.sun.lwuit.TextArea;  
  22. import com.sun.lwuit.events.ActionEvent;  
  23. import com.sun.lwuit.events.ActionListener;  
  24. import com.sun.lwuit.layouts.BorderLayout;  
  25. import com.sun.lwuit.list.ListCellRenderer;  
  26. import com.sun.lwuit.plaf.Border;  
  27.   
  28. public class MainPanel implements ActionListener{  
  29.   
  30.   
  31.     private final String UP_DIR = "..";  
  32.     private final String SEPS_STR = "/";  
  33.     private final int SEPS_CHAR = '/';  
  34.     private final String ROOT = "/";  
  35.     private final String RES_PREFIX = "file://";  
  36.       
  37.     private final int BLOCK_SIZE = 256;  
  38.       
  39.     private final String TEXT_EXT[] = {".TXT",".H",".HPP",".C",".CPPC",".INC"};  
  40.     private final String IMAGE_EXT[] = {".JPG",".JPEG",".PNG",".GIF"};  
  41.     private final String WEB_EXT[] = {".HTM",".HTML",".XML",".CSS"};  
  42.       
  43.     private final String CHARACTER_CODE = "UTF-8";  
  44.       
  45.     private MainMIDlet let = null;  
  46.     //主界面  
  47.     private Form listForm = null;  
  48.     //内容列表  
  49.     private List contentList = null;  
  50.     //显示文件用窗体  
  51.     private Form textForm = null;  
  52.       
  53.     private TextArea text = null;  
  54.       
  55.     private Form imageForm = null;  
  56.       
  57.     private Command cmdExit = null;  
  58.     private Command cmdView = null//浏览目录/文件  
  59.     private Command cmdDone = null//退出浏览文件内容  
  60.     private Command cmdUpload = null//上传文件  
  61.       
  62.     private IconHelper helper = null;  
  63.       
  64.     private String currentDir = null;  
  65.       
  66.     public MainPanel(MainMIDlet mainMIDlet) {  
  67.   
  68.         let = mainMIDlet;  
  69.         currentDir = ROOT;  
  70.         helper = new IconHelper();  
  71.         listForm = new Form();  
  72.         listForm.setLayout(new BorderLayout());  
  73.           
  74.         cmdExit = new Command("退出");  
  75.         cmdView = new Command("浏览");  
  76.         cmdDone = new Command("返回");  
  77.           
  78.         listForm.addCommand(cmdView);  
  79.         listForm.addCommand(cmdExit);  
  80.         listForm.addCommandListener(this);  
  81.           
  82.         new BrowseThread(currentDir,true).start();  
  83.           
  84.       
  85.     }  
  86.   
  87.     public void actionPerformed(ActionEvent evt) {  
  88.   
  89.         Command c = evt.getCommand();  
  90.         if(c == cmdExit){  
  91.             let.exit();  
  92.         }else if(c == cmdView){  
  93.             if(contentList.size() == 0){  
  94.                 new BrowseThread(ROOT,true).start();  
  95.             }else{  
  96.                 final String url = ((MyContainer) contentList.getSelectedItem()).name.getText();  
  97.                 if(url.endsWith(SEPS_STR) == true){  
  98.                     new BrowseThread(currentDir + url,true).start();  
  99.                 }else if(url.endsWith(UP_DIR) == true){  
  100.                     backward();  
  101.                 }else{  
  102.                     new BrowseThread(currentDir + url,false).start();  
  103.                 }  
  104.             }  
  105.         }else if(c == cmdDone){  
  106.             new BrowseThread(currentDir,true).start();  
  107.         }  
  108.           
  109.     }  
  110.       
  111.     private void backward(){  
  112.         int pos = currentDir.lastIndexOf(SEPS_CHAR, currentDir.length() -2);  
  113.         if(pos != -1){  
  114.             currentDir = currentDir.substring(0, pos +1);  
  115.         }else{  
  116.             currentDir = ROOT;  
  117.         }  
  118.         new BrowseThread(currentDir,true).start();  
  119.     }  
  120.       
  121.     class MyContainer extends Container{  
  122.           
  123.         private Label name;  
  124.         public MyContainer(String source){  
  125.             name = new Label(source);  
  126.             name.getStyle().setBgTransparency(0);  
  127.             addComponent(name);  
  128.         }  
  129.           
  130.     }  
  131.       
  132.       
  133.     class MyRenderer implements ListCellRenderer{  
  134.   
  135.           
  136.         private Label focus;  
  137.           
  138.         public MyRenderer(){  
  139.               
  140.             focus = new Label();  
  141.             Border b = Border.createLineBorder(10xff0000);  
  142.             focus.getStyle().setBorder(b);  
  143.         }  
  144.           
  145.         public Component getListCellRendererComponent(List list, Object value,  
  146.                 int index, boolean selected) {  
  147.             return (MyContainer) value;  
  148.         }  
  149.   
  150.         public Component getListFocusComponent(List list) {  
  151.             return focus;  
  152.         }  
  153.           
  154.     }  
  155.       
  156.       
  157.     class BrowseThread extends Thread{  
  158.           
  159.         private String url = null;  
  160.         private boolean isDirectory = false;  
  161.           
  162.         public BrowseThread(final String _url,final boolean _isDirectory){  
  163.             url = _url;  
  164.             isDirectory = _isDirectory;  
  165.         }  
  166.           
  167.         public void run(){  
  168.               
  169.             if(isDirectory){  
  170.                 showDir(url);  
  171.             }  
  172.             else {  
  173.                 showFile(url);  
  174.             }  
  175.         }  
  176.           
  177.         public void showDir(final String dir){  
  178.           
  179.             Enumeration em = null;  
  180.             FileConnection fc = null;  
  181.               
  182.             currentDir = dir;  
  183.               
  184.             listForm.removeAll();  
  185.             contentList = new List();  
  186.             contentList.setListCellRenderer(new MyRenderer());  
  187.             listForm.addComponent(BorderLayout.CENTER,contentList);  
  188.               
  189.             try{  
  190.                 if(dir.equals(ROOT) == true){  
  191.                     //枚举根目录列表  
  192.                     em = FileSystemRegistry.listRoots();  
  193.                 }else{  
  194.                     //非根目录  
  195.                     fc = (FileConnection) Connector.open(RES_PREFIX + dir);  
  196.                     em = fc.list();  
  197.                     MyContainer up = new MyContainer(UP_DIR);  
  198.                     up.name.setIcon(helper.getIconByExt("/"));  
  199.                     contentList.addItem(up);  
  200.                 }  
  201.                   
  202.                 while(em.hasMoreElements()){  
  203.                       
  204.                     String fileName = (String) em.nextElement();  
  205.                     if(fileName.endsWith(SEPS_STR)){  
  206.                         //如果为目录  
  207.                         MyContainer c = new MyContainer(fileName);  
  208.                         c.name.setIcon(helper.getIconByExt("/"));  
  209.                         contentList.addItem(c);  
  210.                         System.out.println("filName------>"+fileName);  
  211.                     }else{  
  212.                         //非目录(文件)  
  213.                         MyContainer c = new MyContainer(fileName);  
  214.                         c.name.setIcon(helper.getIconByExt(extractExt(fileName)));  
  215.                         contentList.addItem(c);  
  216.                     }  
  217.                 }  
  218.                   
  219.                 listForm.revalidate();  
  220.                 contentList.setSelectedIndex(0,true);  
  221.                 if(fc != null){  
  222.                     fc.close();  
  223.                 }  
  224.             }catch(Exception ex){  
  225.                 ex.printStackTrace();  
  226.             }  
  227.             listForm.show();  
  228.         }  
  229.           
  230.         //文件上传  
  231.         public void showFile(final String fileName){  
  232.               
  233.             String url = "http://dev.mopietek.net:8080/waptest_1.0/httpup";  
  234.             String tempFileName = fileName.toUpperCase();  
  235.               
  236.             byte[] data = null;  
  237.               
  238.             try{  
  239.                 FileConnection fc = (FileConnection) Connector.open(RES_PREFIX + fileName,Connector.READ);  
  240.                 if(!fc.exists()){  
  241.                     Dialog.show("Exception""未找到文件""ok","cancel");  
  242.                 }  
  243.                   
  244.                 if(isEndWithIn(tempFileName,IMAGE_EXT) == true){  
  245.                       
  246.                     InputStream is = fc.openInputStream();  
  247.                     ByteArrayOutputStream out = new ByteArrayOutputStream(4096);  
  248.                     byte[] tmp = new byte[4096];  
  249.                     int n;  
  250.                       
  251.                     while((n = is.read(tmp)) != -1){  
  252.                         out.write(tmp, 0, n);  
  253.                         out.flush();  
  254.                     }  
  255.                       
  256.                     is.close();  
  257.                     out.close();  
  258.                     data = out.toByteArray();   
  259.                       
  260.                 }  
  261.                   
  262.                 if(fc != null){  
  263.                     fc.close();  
  264.                 }  
  265.                   
  266.             }catch(Exception ex){  
  267.                 ex.printStackTrace();  
  268.             }  
  269.               
  270.             try {  
  271.                 HttpConnection sc = (HttpConnection) Connector.open(url, Connector.READ, true);  
  272.                 sc.setRequestMethod(HttpConnection.POST);  
  273.                 sc.setRequestProperty("Content-Type""application/octet-stream");  
  274.                 sc.setRequestProperty("Content-Length", String.valueOf(data.length));  
  275.                 OutputStream output = sc.openOutputStream();  
  276.                 output.write(data);  
  277.                 output.flush();  
  278.                 output.close();  
  279.                   
  280.             } catch (IOException e) {  
  281.                 e.printStackTrace();  
  282.             }  
  283.               
  284.               
  285.         }  
  286.           
  287. //      private String getRelativeName(final String fileName){  
  288. //          int pos = fileName.lastIndexOf(SEPS_CHAR);  
  289. //          if(pos == -1){  
  290. //              return ("");  
  291. //          }  
  292. //          return (fileName.substring(pos + 1, fileName.length()));  
  293. //      }  
  294.           
  295.         private boolean isEndWithIn(final String fileName,final String[] extArray){  
  296.             for(int i=0;i<extArray.length;i++){  
  297.                 if(fileName.endsWith(extArray[i]) == true){  
  298.                     return true;  
  299.                 }  
  300.             }  
  301.             return false;  
  302.         }  
  303.           
  304.           
  305.         //获取扩展名(不包括'.'符号)  
  306.         private String extractExt(final String fileName){  
  307.             int pos = fileName.lastIndexOf('.');  
  308.             return (fileName.substring(pos + 1, fileName.length()).toLowerCase());  
  309.         }  
  310.           
  311.           
  312.     }  
  313.       
  314.   
  315. }  



Java代码  收藏代码
  1. package com.mopietek;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.Hashtable;  
  5.   
  6. import com.sun.lwuit.Image;  
  7.   
  8. public class IconHelper {  
  9.   
  10.       
  11.     //图标资源  
  12.     private Image imgFolder;  
  13.     private Image unknownImage;  
  14.     private Image textImage;  
  15.     //Audio  
  16.     private Image audioImage;  
  17.     //Picture  
  18.     private Image picImage;  
  19.     private Image jpgImage;  
  20.     //Video  
  21.     private Image sgpImage;  
  22.     private Image aviImage;  
  23.     private Image wmvImage;  
  24.     private Image mpgImage;  
  25.     //Web  
  26.     private Image zipImage;  
  27.     private Image htmlImage;  
  28.     private Image xmlImage;  
  29.     //Source  
  30.     private Image cImage;  
  31.     private Image cppImage;  
  32.     private Image headerImage;  
  33.     //图标管理容器  
  34.     private Hashtable iconTable;  
  35.       
  36.     public IconHelper(){  
  37.         iconTable = new Hashtable();  
  38.         try{  
  39.             //载入图标资源  
  40.             imgFolder = Image.createImage("/filetype/folder.png");  
  41.             unknownImage = Image.createImage("/filetype/unknown.png");  
  42.             textImage = Image.createImage("/filetype/text.png");  
  43.             //Audio  
  44.             audioImage = Image.createImage("/filetype/audio.png");  
  45.             //Picture  
  46.             picImage = Image.createImage("/filetype/pic.png");  
  47.             jpgImage = Image.createImage("/filetype/jpg.png");  
  48.             //Video  
  49.             sgpImage = Image.createImage("/filetype/3gp.png");  
  50.             aviImage = Image.createImage("/filetype/avi.png");  
  51.             wmvImage = Image.createImage("/filetype/wmv.png");  
  52.             mpgImage = Image.createImage("/filetype/mpg.png");  
  53.             //Web  
  54.             zipImage = Image.createImage("/filetype/zip.png");  
  55.             htmlImage = Image.createImage("/filetype/html.png");  
  56.             xmlImage = Image.createImage("/filetype/xml.png");  
  57.             //Source  
  58.             cImage = Image.createImage("/filetype/c.png");  
  59.             cppImage = Image.createImage("/filetype/cpp.png");  
  60.             headerImage = Image.createImage("/filetype/header.png");  
  61.         }catch(IOException e){  
  62.             //图标资源  
  63.             imgFolder = null;     
  64.             unknownImage = null;     
  65.             textImage = null;     
  66.             //Audio     
  67.             audioImage = null;     
  68.             //Picture     
  69.             picImage = null;     
  70.             jpgImage = null;     
  71.             //Video     
  72.             sgpImage = null;     
  73.             aviImage = null;     
  74.             wmvImage = null;     
  75.             mpgImage = null;     
  76.             //Web     
  77.             zipImage = null;     
  78.             htmlImage = null;     
  79.             xmlImage = null;     
  80.             //Source     
  81.             cImage = null;     
  82.             cppImage = null;     
  83.             headerImage = null;     
  84.             e.printStackTrace();    
  85.   
  86.         }  
  87.         initTable();  
  88.           
  89.     }  
  90.       
  91.       
  92.     private void initTable() {  
  93.        //Key为扩展名(不包括'.'符号),值为content type  
  94.         iconTable.put("/", imgFolder);  
  95.         iconTable.put("", unknownImage);  
  96.         iconTable.put("txt", textImage);  
  97.         //Source  
  98.         iconTable.put("c", cImage);  
  99.         iconTable.put("cpp", cppImage);  
  100.         iconTable.put("h", headerImage);  
  101.         //Web  
  102.         iconTable.put("html", htmlImage);  
  103.         iconTable.put("htm", htmlImage);  
  104.         iconTable.put("xml", xmlImage);  
  105.         iconTable.put("zip", zipImage);  
  106.         iconTable.put("jar", zipImage);  
  107.         //audio  
  108.         iconTable.put("mp3", audioImage);  
  109.         iconTable.put("wma", audioImage);  
  110.         iconTable.put("mid", audioImage);  
  111.         iconTable.put("wav", audioImage);  
  112.         //Picture  
  113.         iconTable.put("gif", picImage);  
  114.         iconTable.put("png", picImage);  
  115.         iconTable.put("jpg", jpgImage);  
  116.         iconTable.put("jepg", jpgImage);  
  117.         //Video  
  118.         iconTable.put("3gp", sgpImage);  
  119.         iconTable.put("avi", aviImage);  
  120.         iconTable.put("wmv", wmvImage);  
  121.         iconTable.put("mpeg", mpgImage);  
  122.         iconTable.put("mpg", mpgImage);  
  123.         iconTable.put("mp4", mpgImage);  
  124.           
  125.     }  
  126.   
  127.     //按照扩展名(不包括'.'符号) 获取文件的图标对象  
  128.     public Image getIconByExt(final String extension) {  
  129.   
  130.         Image temp = (Image) iconTable.get(extension);  
  131.         //是否为空,则使用默认图标替代  
  132.         return ((temp == null) ? unknownImage : temp);  
  133.           
  134.     }  
  135.   
  136. }  



由于是在昨天的lwuit版手机本地文件读取上修改,所以代码中用到的资源文件在上一篇中可以下载,程序中只是写实现了图片的上传的代码,其它类型文件跟图片文件上传相同。

posted on 2013-03-06 13:48  爱哎唉  阅读(143)  评论(0)    收藏  举报