2012-05-31 85 views
0

我正在從一個黑莓應用程序中獲得一個pdf-url從web服務。我需要使用該URL來下載該PDF並將其保存到我的SD卡。從blackberry的url下載pdf文件?

請讓我知道,有沒有可能。

+0

您可以將PDF文件下載到字節數組中,然後將這些字節寫入擴展名爲.pdf的文件中。 – Rupak

+0

thanx建議Rupak,我也在想這個。我會盡力讓你的結果很快。 –

回答

0

我很抱歉很遲纔回復,我在這個任務中取得了成功。我知道這不是一個「非常努力的任務」,但這對我來說是個教訓。我爲此任務使用了以下線程代碼。

private class DownloadCombiner extends Thread { 
     private String remoteName; 
     private String localName; 
     private int connection; 
     private int chunksize; 
     public DownloadCombiner(String remoteName, 
       String localName, int connection, int chunksize) { 
      this.remoteName = remoteName; 
      this.localName = localName; 
      this.connection = connection; 
      this.chunksize = chunksize; 
     } 
     public void run() { 
      try { 
       int chunkIndex = 0; 
       int totalSize = 0; 
       /* 
       * File connection 
       */ 
       FileConnection file =(FileConnection)Connector.open(localName); 
       if (!file.exists()) { 
        file.create(); 
       } 
       file.setWritable(true); 
       OutputStream out = file.openOutputStream(); 
       /* 
        * HTTP Connections 
        */ 
       String currentFile = remoteName + 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 (connection) { 
      case 1: 
       return ";deviceside=false"; 
      case 2: 
       return ";deviceside=true;interface=wifi"; 
      default: 
       return ";deviceside=true"; 
      } 
     } 
    }