1

我正在开发一个黑莓应用程序,我需要在其中点击一个 url 创建一个连接并写入一个文件并将其保存到 SDcard。目前我正在关注这个特定的代码。但是在创建 FileOutputStream 对象时会抛出 CLassCastException。我正在为此苦苦挣扎。

public void run() {
            HttpConnection httpConnection = null;
            DataOutputStream httpDataOutput = null;
            InputStream httpInput = null;
            OutputStream  fos=null;
            int rc;
            try {
                httpConnection = new HttpConnectionFactory()
                .getHttpConnection("http://faultcode.techvalens.net/PDF/DrawingSample.PDF");
                rc = httpConnection.getResponseCode();
                if (rc != HttpConnection.HTTP_OK) {
                    throw new IOException("HTTP response code: " + rc);
                }
                httpInput = httpConnection.openInputStream();
                InputStream is = httpInput;

                FileConnection fconn=(FileConnection)Connector.open("file:///SDCard/Test.txt",
                        Connector.READ_WRITE);
                if(!fconn.exists())
                    fconn.create();
                System.out.println(fconn.exists());

                fos = new FileOutputStream( File.FILESYSTEM_PATRIOT, "Test.txt" );
            //  byte[] b = IOUtilities.streamToBytes(inp);

                byte[] buffer = new byte[702];
                    int len1 = 0;
                    while ((len1 = is.read(buffer)) != -1) {
                        fos.write(buffer, 0, len1);
                    }

            } catch (Exception ex) {
                System.out.println("URL Error........" + ex.getMessage());
            } finally {
                try {
                    if (httpInput != null)
                        httpInput.close();
                    if (httpDataOutput != null)
                        httpDataOutput.close();
                    if (httpConnection != null)
                        httpConnection.close();
                } catch (Exception e) {
                    e.printStackTrace();

                }
            }
        }

我正在使用的上述代码。请让我知道我的错误是什么。提前谢谢...!!!

4

1 回答 1

0

这项任务是基本的,但为我创造了一个难忘的概念。下面是我现在使用的代码,如果我需要从 URL 下载任何类型的文件,它是最好的。

public void run(){
        GetURLWebService _webService = new GetURLWebService(methodName,elecID,pdfType,performedBy,notes);
        String pdfURL = _webService.getWebServiceData();
        _pdfURL = pdfURL;

        System.out.println("Download the pdf...!!!");
        try {
            int chunkIndex = 0;
            int totalSize = 0;
            /*
             * File connection
             */
            FileConnection file =(FileConnection)Connector.open(localFile);
            if (!file.exists()) {
                file.create();
            }
            file.setWritable(true);
            OutputStream out = file.openOutputStream();
            /*
             * HTTP Connections
             */
            String currentFile = _pdfURL + connectionType();
            //log("Full URL: " + currentFile);
            HttpConnection conn;
            InputStream in;
            int rangeStart = 0;
            int rangeEnd = 0;
            while (true) {
                //  log("Opening Chunk: " + chunkIndex);
                conn = (HttpConnection) Connector.open(currentFile, 
                        Connector.READ_WRITE, true);
                rangeStart = chunkIndex * chunksize;
                rangeEnd = rangeStart + chunksize - 1;
                // log("Requesting Range: " + rangeStart + 
                //      "-" + rangeEnd);
                conn.setRequestProperty("Range", "bytes=" +
                        rangeStart + "-" + rangeEnd);
                int responseCode = conn.getResponseCode();
                if (responseCode != 200 && responseCode != 206)
                {
                    // log("Response Code = " + conn.getResponseCode());
                    break;
                }
                // log("Retreived Range: " + conn.getHeaderField("Content-Range"));
                in = conn.openInputStream();
                int length = -1;
                byte[] readBlock = new byte[256];
                int fileSize = 0;
                while ((length = in.read(readBlock)) != -1) {
                    out.write(readBlock, 0, length);
                    fileSize += length;
                    Thread.yield(); // Try not to get cut off
                }
                totalSize += fileSize;
                // log("Chunk Downloaded: " + fileSize + " Bytes");
                chunkIndex++; // index (range) increase
                in.close();
                conn.close();
                in = null;
                conn = null;
                /*
                 * Pause to allow connections to close and other Threads
                 * to run.
                 */
                Thread.sleep(1000);
            }
            // log("Full file downloaded: " + totalSize + " Bytes");
            out.close();
            file.close();
            // log("Wrote file to local storage");
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }
    private String connectionType() {
        switch (conn) {
        case 1:
            return ";deviceside=false";
        case 2:
            return ";deviceside=true;interface=wifi";
        default:
            return ";deviceside=true";
        }
    }
于 2012-06-19T13:18:01.183 回答