2012-05-18 61 views
1

我想下載從http服務器.txt文件,並將其存儲在設備上memory.How我可以做它。我是新來的就那麼任何幫助,將其保存到設備將不勝感激。提前如何從服務器上下載文件,使用及使用BlackBerry API

+0

紅寶石你好,根據你的問題,我想從服務器上下載圖像文件和需要將其保存在設備。你能幫我解決這個問題嗎?由於我是黑莓開發新手,無法找到代碼。 –

回答

2

感謝我不會爲你的代碼,但我可以給你的邏輯吧,因爲我已經做了這方面的工作。

您將需要HttpConnectionDataInutStream,DataOutputStreamFileConnection類爲相同的目的。

Here是一個例子的一個環節,它是同你的問題的要求,需要進行研究,並代碼爲你的自我。

提示:只有輕微的變化需要的代碼,如果你自己看着辦吧。

+0

感謝路西法,我已經解決了我的問題我自己。但感謝您的幫助 –

+0

歡迎Ruby :) – Lucifer

-2

使用下面的代碼

package com.neel.java.rim.api.net; 

import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import javax.microedition.io.Connector; 
import javax.microedition.io.HttpConnection; 
import javax.microedition.io.file.FileConnection; 
import net.rim.device.api.ui.component.Dialog; 

public class FileDownloader implements Runnable { 
    // Holds the URL to download the file 
    StringBuffer url = null; 

    //holds the instance of the delegate screen 
    protected Object delegate; 

    public FileDownloader(String url) { 
     // image URL 
     this.url = new StringBuffer(); 
     this.url.append(url.toString()); 
    } 

    //taking the instance of the delegate 
    //this is the object of the active screen from where the request is made 
    public Object getDelegate() { 
     return delegate; 
    } 

    public void setDelegate(Object delegate) { 
     this.delegate = delegate; 
    } 

    // Thread starts the execution 
    public void run() { 
     byte[] dataArray; 
     InputStream input; 
     //url.append(updateConnSuffix()); // ad connection suffix for the data usage 

     HttpConnection httpConn = null; 
     try { 
      httpConn = (HttpConnection) Connector.open(url.toString()); 

      input = httpConn.openInputStream(); 

      dataArray = net.rim.device.api.io.IOUtilities.streamToBytes(input); 

      writeFile(dataArray); 


     } catch (IOException e) { 
      e.printStackTrace(); 
      Dialog.alert("Eoor in downloading image"); 
     }  
    } 


    public void writeFile(byte[] data){ 
     FileConnection fc = null; 

     // to save in SD Card 
     String pFilePath = "SDCard/BlackBerry/pictures/text.txt"; 

     /*use below path for saving in Device Memory*/ 
     //String pFilePath = "store/home/user/pictures/text.txt"; 

     OutputStream lStream = null; 
     String time = new String(); 
     if (pFilePath != null) { 
      try { 
       fc = (FileConnection)Connector.open("file:///" + pFilePath ,Connector.READ_WRITE); 
       if(null == fc || fc.exists() == false){ 
        fc.create(); 
       } 
       lStream = fc.openOutputStream(fc.fileSize()); 

       lStream.write(data); 

      } catch (Exception ioex) { 
       ioex.printStackTrace(); 
      } finally { 
       if (lStream != null) { 
        try { 
         lStream.close(); 
         lStream = null; 
        } catch (Exception ioex){ 
        } 
       } 
       if (fc != null) { 
        try { 
         fc.close(); 
         fc = null; 
        } catch (Exception ioex){ 
        } 
       } 
      } 
     } 
    } 


} 
相關問題