Welcome, Guest. Please login or register.

Login with username, password and session length
August 20, 2008, 04:31:02 AM
Pages: [1]
  Print  
Topic: [check this code]send file from mobile to PC  (Read 208 times)
« on: July 04, 2008, 04:33:54 AM »
c33
Newbie

View Profile
*
Karma: +0/-0
Posts: 2



===============================================================================================
File : SendFile.java (Mobile)
===============================================================================================
public void run() {
                        try{
                           //parent.chatMessage(null, "file://"+directory+fileName);
                            FileConnection conn = (FileConnection)Connector.open("file://"+directory+fileName, Connector.READ_WRITE);
//                            parent.chatMessage(null, String.valueOf(conn.fileSize()));
                            InputStream file = conn.openInputStream();
                            fileSize = (int)conn.fileSize();
                            imageData = new byte[fileSize];
                            file.read(imageData);
                            file.close();
                           
                           
                            String port = "9";
                            String adr = "btgoep://"+btAddress+":" + port; // no lookup code here … put your phone's bt address here!
                            connection = Connector.open(adr);
                            ClientSession cs = (ClientSession)connection;
                            HeaderSet hs = cs.createHeaderSet();
                            cs.connect(hs);
                            hs.setHeader (HeaderSet.NAME, fileName);
                            hs.setHeader (HeaderSet.TYPE, "image/jpeg");
                            hs.setHeader(HeaderSet.LENGTH, new Long(imageData.length));
//                            hs.setHeader(0x49, text); // if everything fits inside a packet, the data can be packed in the PUT command
                            Operation po = cs.put(hs);
                           
                             outputStream = po.openOutputStream();
                             outputStream.write(imageData);
                             
                             outputStream.close();
                             po.close();
                                 

                                    /*OutputStream outputStream = putOperation.openOutputStream();
                                    outputStream.write(file);*/
                           
                           
                            System.out.println("put....");
//                            po.openOutputStream().write(text);
                            cs.disconnect(null);
                            connection.close();
                            Alert alert1 = new Alert("Informasi", "File berhasil terkirim", null, null);
                            alert1.setTimeout(Alert.FOREVER);
                            alert1.setType(AlertType.CONFIRMATION);
                            ui.setCurrent(alert1);
                            //System.out.println("closed...");
                         } catch (Throwable e)
                         {
                           
                         }

===============================================================================================


===============================================================================================
File : ReceiveFile.java (PC)
===============================================================================================
public void run(){
      //UUID uuid = new UUID("9106", true);
      
      //String url = "btgoep://localhost:" + uuid + ";name=o;authenticate=false;master=false;encrypt=false";
      try {
      LocalDevice device = LocalDevice.getLocalDevice();
           device.setDiscoverable(DiscoveryAgent.GIAC);
                String url = "btgoep://localhost:"+Config.UUID_OBEX+";name="+Config.SERVICE_NAME_OBEX+";authenticate=false;master=false;encrypt=false";
           System.out.println("iki");
           SessionNotifier server = (SessionNotifier) OBEXConnector.open(url);
         System.out.println("Server sudah berjalan");
         System.out.println(url);
         
         
            
            server.acceptAndOpen(this);
         
      }catch(IOException ioe){
         System.err.println("[server] "+ioe+" aaaaaaa");
      }
   }
   

   public int onPut(Operation operation){
      System.out.println("[server] File datang, bentar lom siap");
      HeaderSet headers = null;
        String imageName = null;
        int imageLength = 0;
        int responseCode;

        try {
            inputStream = operation.openInputStream();
            headers = operation.getReceivedHeaders();
            imageName = (String) headers.getHeader(HeaderSet.NAME);
            imageLength = (int) ((Long)headers.getHeader(HeaderSet.LENGTH)).longValue();

            //responseCode = ResponseCodes.OBEX_HTTP_OK;
            //ui.infoScreen.showInfo("File Transfer", "File "+imageName+" length " +imageLength);
            System.out.println("Nama File : " + imageName);
            System.out.println("Ukuran File : " + imageLength);
            byte[] image = new byte[imageLength];
           
            File f = new File(imageName);
            FileOutputStream fos = new FileOutputStream (f);
            byte b[] = new byte[1000];
            int len;
           
            while (inputStream.available() > 0 && (len = inputStream.read(b)) > 0) {
                fos.write (b, 0, len);
            }
           
            fos.close();
            inputStream.close();
        } catch (IOException ioe) {
           
                responseCode = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
                System.out.println("aaaaaa");
        }
        return ResponseCodes.OBEX_HTTP_OK;
   }
   
   
    }

     public int onConnect(HeaderSet request, HeaderSet reply) {
        return ResponseCodes.OBEX_HTTP_OK;
    }
===============================================================================================
question :

why my PC cannot receive file if send file from my apllication???
but if i sending file from galery on my Nokia phone (standar aplikasi in Nokia), my PC can receive file from mobile...

thanks for sharing
Logged
 
Pages: [1]
  Print  
 
Jump to:  

Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC