Java 如何将dat文件从网络链接下载到我的Android应用程序SDcard中的路径中?
在我开发的应用程序中,我必须从Web服务器链接下载一个dat文件和一个xml文件。 下载xml时没有问题,但在下载dat文件时引发异常。我如何克服这种状态。谢谢大家 例外情况Java 如何将dat文件从网络链接下载到我的Android应用程序SDcard中的路径中?,java,android,http,android-intent,Java,Android,Http,Android Intent,在我开发的应用程序中,我必须从Web服务器链接下载一个dat文件和一个xml文件。 下载xml时没有问题,但在下载dat文件时引发异常。我如何克服这种状态。谢谢大家 例外情况 02-01 16:33:34.713: W/System.err(1404): java.io.FileNotFoundException: http://www.myingage.com/bkpdrctry/dontwo.dat 02-01 16:33:34.723: W/System.err(1404)
02-01 16:33:34.713: W/System.err(1404): java.io.FileNotFoundException: http://www.myingage.com/bkpdrctry/dontwo.dat
02-01 16:33:34.723: W/System.err(1404): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:1162)
实际代码如下
protected static void downloadFile(String stringUrl, String path){
try {
URL url = new URL(stringUrl);
DebugLog.LOGD("URL " +url );
//create the new connection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//set up some things on the connection
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
//and connect!
urlConnection.connect();
File SDCardRoot = Environment.getExternalStorageDirectory();
//create a new file, specifying the path, and the filename
//which we want to save the file as.
File file = new File(SDCardRoot,path);
DebugLog.LOGD("download to sd card " +url );
//this will be used to write the downloaded data into the file we created
FileOutputStream fileOutput = new FileOutputStream(file);
DebugLog.LOGD("File written successfully in " +path );
//this will be used in reading the data from the internet
InputStream inputStream = urlConnection.getInputStream();
//this is the total size of the file
int totalSize = urlConnection.getContentLength();
//variable to store total downloaded bytes
int downloadedSize = 0;
//create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0; //used to store a temporary size of the buffer
//now, read through the input buffer and write the contents to the file
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
//add the data in the buffer to the file in the file output stream (the file on the sd card
fileOutput.write(buffer, 0, bufferLength);
//add up the size so we know how much is downloaded
downloadedSize += bufferLength;
//this is where you would do something to report the prgress, like this maybe
//updateProgress(downloadedSize, totalSize);
}
//close the output stream when done
fileOutput.close();
//catch some possible errors...
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
此处调用上述函数:
downloadFile("http://www.myingage.com/bkpdrctry/dontwo.xml","/Test/dontwo.xml");
downloadFile("http://www.myingage.com/bkpdrctry/dontwo.dat","/Test/dontwo.dat");
打开
http://www.myingage.com/bkpdrctry/dontwo.dat
在您的浏览器上
抛出
HTTP 404
您的web服务器上没有dat文件;)嘿Dawid.我在网络服务器上有.dat文件。如果您尝试在web浏览器中打开该文件,显然它将无法打开:)因为该文件是一个压缩文件,浏览器不支持该文件。android应用程序能够识别.dat文件。你能建议我如何将dat文件下载到我的android应用程序中吗?马塞洛:首先,谢谢你的回复。dontwo.dat是一个被android应用程序识别的压缩文件。你无法在浏览器中打开dat文件,因为浏览器无法识别.dat文件。谢谢你的回复。dontwo.dat是一个被android应用程序识别。您将无法在浏览器中打开dat文件,因为浏览器无法识别.dat文件。我想知道如何从web服务器将.dat文件下载到我的android应用程序中。我还检查了网站的web主机管理器,文件存在。嘿,伙计们,找出哪里出了问题。godaddy服务器是网站的托管服务器,它无法识别dat文件。我将.dat文件重命名为dontwo.dat.jpg,它正常工作了。我可以下载。:)谢谢你们的贡献