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