2012-01-09 6 views
1

私のプロジェクトでDropboxを使用して、dropboxから小さなURLを取得しています。これはhttp://www.db.tt/xyzabcのようです。HTTP接続からHTTPS接続にリダイレクトするファイルをダウンロードする

HTC My touchでファイルをダウンロードしようとすると、私のコードは正常に動作しますが、試してみるとMotorola Atrixexception unknown host db.ttを投げます。

実際に最初に私はhttp://www.db.tt/xyzabcのようなURLを持っています。これは、私がexceptionを得るよりもオープンしています。例外として、ファイルを含む実際のURLを取得し、例外的にHTTPS URLです。私はここに、ファイルのダウンロードを開始することは私のために働く私のコードです:

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

[OK]を私はそれがここに私の自己を解決して作られたいくつかの試みが、誰かが同じ問題を抱えている場合ソリューションが参考になっなりした後には、いくつかのエラー処理や修正が必要

を必要に応じて接続のクラス階層構造を見た後、私はHttpsURLConnectionのは、HttpURLConnectionのの子であることを発見し、HttpURLConnectionのURLConnectionのの子であるので、IIは、代わりのURLConnectionのでHTTPConnectionを使用して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 audio in my case]が実際にhtml DOWNLOAD画面にリダイレクトされるように見えます。 これは、リンクがIMHOを行うべきでは役に立たない。 – loser114491

関連する問題