J2ME 文件上傳 實例(FileConnector鏈接文件系統,非RMS)

客戶端:

import java.io.InputStream;
import java.io.OutputStream;
import java.io.ByteArrayOutputStream;
import javax.microedition.io.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.io.file.*;

public class HttpConnect extends MIDlet {

    public HttpConnect() {
        // TODO Auto-generated constructor stub
    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        // TODO Auto-generated method stub

    }

    protected void pauseApp() {
        // TODO Auto-generated method stub

    }

    protected void startApp() throws MIDletStateChangeException{
        String url = "http://192.168.0.114:8080/EngineService/sync";
        try{
            byte[] data = null;
            if( System.getProperty("microedition.io.file.FileConnection.version" ) != null ){
               
                try {
                    FileConnection fc = (FileConnection)Connector.open("file:///root1/test.png", Connector.READ_WRITE);
                    //fc.create();
                    InputStream in = fc.openInputStream();
                    fc.close();
                    ByteArrayOutputStream out = new ByteArrayOutputStream(4096); 
                                 byte[] tmp = new byte[4096]; 
                                 int n; 
                                 while ((n = in.read(tmp)) != -1) { 
                                     out.write(tmp, 0, n);
                                     out.flush();
                                 } 
                                 in.close(); 
                                 out.close(); 
                                 data = out.toByteArray(); 
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
           
            HttpConnection sc = (HttpConnection)Connector.open( url, Connector.READ_WRITE, true );
            sc.setRequestMethod( HttpConnection.POST );
            sc.setRequestProperty("Content-Type", "application/octet-stream");
            sc.setRequestProperty("Content-Length", String.valueOf(data.length));

            OutputStream output = sc.openOutputStream();
            output.write(data);
            output.flush();
            output.close();       
       
        }catch(Exception e){
            System.out.println("Error:"+e);
        }
    }

}

服務器端:

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
    {
        InputStream in = req.getInputStream();
       
        byte[] tmp = new byte[4096];
        int size=0;
        File f = new File("c://",System.currentTimeMillis()+".png");
        DataOutputStream o = new DataOutputStream(new FileOutputStream(f));
       
        int len=0;
        while((len = in.read(tmp))!= -1){
            o.write(tmp,0,len);
            o.flush();
            size+=len;
        }
        o.close();
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章