2012-01-09 57 views
1

我在我的項目中使用Dropbox從Dropbox獲取小型網址,就像http://www.db.tt/xyzabc從HTTP連接下載文件重定向到HTTPS連接

當我嘗試下載HTC My touch中的文件時,我的代碼工作正常,但是如果我嘗試在Motorola Atrix中,它將引發exception unknown host db.tt

其實首先我有網址像http://www.db.tt/xyzabc這是HTTP網址我打開它比我得到exception和異常我得到實際的網址文件,其中包含文件和HTTPS網址在例外。我開始在這裏下載文件是我的代碼爲我工作:

public static void fileUrl(String fAddress, String localFileName, 
     String destinationDir) { 
    OutputStream outStream = null; 
    URLConnection uCon = null; 

    InputStream is = null; 
    try { 
     URL url; 
     byte[] buf; 
     int ByteRead, ByteWritten = 0; 
     url = new URL(fAddress); 
     outStream = new BufferedOutputStream(new FileOutputStream(
       destinationDir + localFileName)); 

     try { 
      // Here i have "http://www.db.tt/xyzabc" 
         // after i hit url i get exception and in exception that 
         // FileNotFoundException at https://www.dropbox.com/abcxyz 
        // i get actual actual url i parse that exception and 
        //retrive https://www.dropbox.com/xyzabc(actual url) 
         // but in motorolla atrix instead of that url i get 
        // unknownhost exception "db.tt" 




      uCon = url.openConnection(); 
     // uCon.connect(); 

      is = uCon.getInputStream(); 
     } catch (Exception e) { 
      url = new URL(e.getMessage().substring(
        e.getMessage().indexOf("https"), 
        e.getMessage().length())); 
      outStream = new BufferedOutputStream(new FileOutputStream(
        destinationDir + localFileName)); 

      uCon = url.openConnection(); 
      is = uCon.getInputStream(); 
     } 

     buf = new byte[size]; 
     while ((ByteRead = is.read(buf)) != -1) { 
      outStream.write(buf, 0, ByteRead); 
      ByteWritten += ByteRead; 
     } 
     System.out.println("Downloaded Successfully."); 
     System.out.println("File name:\"" + localFileName 
       + "\"\nNo ofbytes :" + ByteWritten); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      is.close(); 
      outStream.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

回答

1

確定後我做到了解決我的自我,這裏很少嘗試的解決方案將是有益的,如果有人得到了同樣的問題,它需要一些錯誤處理和修改根據需要

看到連接的類層次結構後,我發現,HttpsURLConnection的是HttpURLConnection類和HttpURLConnection的是URLConnection的所以II中使用的HttpConnection代替的URLConnection和HttpsURLConnection的兒童是混凝土的孩子HttpsURLConnection的它解決了我的問題 我繼續迭代,直到重定向後我得到Https url

public static void fileUrl(String fAddress, String localFileName, 
     String destinationDir) { 
    OutputStream outStream = null; 
    URLConnection uCon = null; 
    HttpURLConnection mHttpCon; 

    InputStream is = null; 
    try { 

     URL url; 
     byte[] buf; 
     int ByteRead, ByteWritten = 0; 
     url = new URL(fAddress); 
     outStream = new BufferedOutputStream(new FileOutputStream(
       destinationDir + localFileName)); 

     try { 

      mHttpCon = (HttpURLConnection) url.openConnection(); 

      while (!url.toString().startsWith("https")) { 
       mHttpCon.getResponseCode(); 
       url = mHttpCon.getURL(); 
       mHttpCon = (HttpURLConnection) url.openConnection(); 

      } 

      is = mHttpCon.getInputStream(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      // url = new URL(e.getMessage().substring(
      // e.getMessage().indexOf("https"), 
      // e.getMessage().length())); 
      // outStream = new BufferedOutputStream(new FileOutputStream(
      // destinationDir + localFileName)); 
      // 
      // uCon = url.openConnection(); 
      // is = uCon.getInputStream(); 
     } 

     buf = new byte[size]; 
     while ((ByteRead = is.read(buf)) != -1) { 
      outStream.write(buf, 0, ByteRead); 
      ByteWritten += ByteRead; 
     } 
     System.out.println("Downloaded Successfully."); 
     System.out.println("File name:\"" + localFileName 
       + "\"\nNo ofbytes :" + ByteWritten); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      is.close(); 
      outStream.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

public static void fileDownload(String fAddress, String destinationDir) { 

    int slashIndex = fAddress.lastIndexOf('/'); 
    int periodIndex = fAddress.lastIndexOf('.'); 

    String fileName = fAddress.substring(slashIndex + 1); 

    if (periodIndex >= 1 && slashIndex >= 0 
      && slashIndex < fAddress.length() - 1) { 
     fileUrl(fAddress, fileName, destinationDir); 
    } else { 
     System.err.println("path or file name."); 
    } 
} 
1

此答案在某種程度上有效。我有類似的解決方案here

Atrix上的Dropbox短超鏈接仍然存在問題。他們從http重定向到https,但不是所需的文件,而是從Dropbox內部獲得大量html。

+0

我在一段時間內只留下了這個問題。最終看起來,一些簡短的超鏈接[在我的案例中的Dropbox音頻]實際上重定向到HTML下載屏幕。 這是沒用的什麼鏈接應該做恕我直言。 – loser114491 2013-03-03 10:51:17