2

http サーバーから .txt ファイルをダウンロードしてデバイスのメモリに保存したいのですが、どうすればよいですか。前もって感謝します

4

2 に答える 2

2

私はあなたのためにコーディングするつもりはありませんが、私はすでにこの種の作業を行っているので、そのためのロジックを提供することができます.

同じ目的でHttpConnectionDataInutStreamDataOutputStreamおよびClassが必要になります。FileConnection

ここに例のリンクがあります。これは質問の要件と同じです。それを調べて自分でコーディングする必要があります。

ヒント: そのコードを理解できる場合は、マイナーな変更のみが必要です。

于 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 に答える