2012-06-07 70 views
1

我正在研究一個黑莓應用程序,我需要點擊一個url創建一個連接並寫入一個文件並將其保存到SDcard。目前我正在關注這個特定的代碼。但是,當創建FileOutputStream對象時,它會拋出CLassCastException。我正在爲此而苦苦掙扎。從黑莓的給定網址下載pdf文件

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(); 

       } 
      } 
     } 

上述代碼我正在使用。 請讓我知道我的錯誤是什麼。 THanx提前... !!!

+1

我真的不明白你爲什麼需要FileOutputStream。只要調用fconn.openOutputStream()並根據需要寫入字節即可。另外不要忘記在最後關閉文件連接和流!關於FileOutputStream - 在javadoc中有兩個,我沒有看到在這裏投射,但可能在RIM框架中發生了一些事情。 javadoc也提到了一些關於iDEN文件系統的內容。我不知道這個文件系統,但iDEN設備是Sprint設備的一部分,根本不覆蓋所有的BlackBerry設備。 –

回答

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"; 
     } 
    } 
+0

讓我知道,如果我還需要改善這一點。因爲我需要成爲最好的:) –