-1
我想實現簡單的下載管理我的Android應用程序,我用簡單的Serivce
從互聯網上下載文件,當我用這個方法用於創建連接:的Android connection.getInputStream()返回FileNotFoundException異常錯誤
private HttpURLConnection createConnection(String urlStr) throws IOException {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setReadTimeout(timeout);
conn.setConnectTimeout(timeout);
return conn;
}
我沒有得到任何異常,此鏈接做工精細:
https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png
現在我想從連接getInputStream()
並分配變量:
in = new BufferedInputStream(connection.getInputStream(),BUFFER_SIZE);
,但我得到這個錯誤:
'java.io.FileNotFoundException' exception.
我可以得到文件長度罰款從連接,但我不能下載
new Thread(new Runnable() {
@Override
public void run() {
try {
prepareDownload(fileUrl, filePath, lastModified);
connection = createConnection(fileUrl);
fileDownloadStatus = downloadStatus.DOWNLOADING;
if (!startNewDownload) {
connection.setRequestProperty("Range", "bytes=" + downloadedFile.length() + "-");
}
if (onDownloadProcess != null)
onDownloadProcess.fileLength(fileLength);
in = new BufferedInputStream(connection.getInputStream(), BUFFER_SIZE);
long progressLength = 0;
if (!startNewDownload) {
progressLength += downloadedFile.length();
// append to exist downloadedFile
writer = new FileOutputStream(filePath, true);
} else {
writer = new FileOutputStream(filePath);
// save remote last modified data to local
lastModified = connection.getHeaderField("Last-Modified");
}
try {
byte[] buffer = new byte[BUFFER_SIZE];
int count;
while (getFileDownloadStatus() == downloadStatus.DOWNLOADING && (count = in.read(buffer)) != -1) {
progressLength += count;
writer.write(buffer, 0, count);
// progress....
if (onDownloadProcess != null)
onDownloadProcess.onProgress((int) (progressLength * 100/fileLength));
if (progressLength == fileLength) {
progressLength = 0;
setFileDownloadStatus(downloadStatus.COMPLETE);
}
}
...
}).start();
問題不解決,我得到這個錯誤爲每個http,https網址 –