从黑莓中的给定网址下载pdf文件

时间:2012-06-07 14:07:54

标签: blackberry download

我正在开发一个黑莓应用程序,我需要在其中创建连接并写入文件并将其保存到SD卡。目前我正在遵循这个特定的代码。但是在创建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();

                }
            }
        }

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

1 个答案:

答案 0 :(得分: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";
        }
    }