2

我想从 http 服务器下载一个 .txt 文件并将其存储在设备内存中。我该怎么做。我是新手,所以任何帮助将不胜感激。提前致谢

4

2 回答 2

2

我不会为您编写代码,但我可以为您提供逻辑,因为我已经完成了此类工作。

您将需要HttpConnection,DataInutStream和ClassDataOutputStreamFileConnection实现相同的目的。

是一个示例的链接,它与您的问题要求相同,您需要自己研究并编写代码。

提示:如果你能弄明白的话,只需要对该代码进行微小的更改。

于 2012-05-18T13:57:43.283 回答
-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){
                    }
                }
            }
        }
    }


}
于 2012-05-18T14:08:04.107 回答