Java 输入流不存在';t阅读URL连接的全部答案
我正在与未完成的文件下载作斗争 例如,我在github上上传了一些数据: 在pasteBin上: 我想检索它们并将它们保存到文件“filename”中。 我在互联网上看了很多例子,它一定很有效 代码如下:Java 输入流不存在';t阅读URL连接的全部答案,java,android,url,inputstream,Java,Android,Url,Inputstream,我正在与未完成的文件下载作斗争 例如,我在github上上传了一些数据: 在pasteBin上: 我想检索它们并将它们保存到文件“filename”中。 我在互联网上看了很多例子,它一定很有效 代码如下: private void download(final URL myUrl){ new Thread(new Runnable() { //InputStream is = null; //FileOutputStream fos = null;
private void download(final URL myUrl){
new Thread(new Runnable() {
//InputStream is = null;
//FileOutputStream fos = null;
public void run() {
try {
URLConnection connection = myURLL.openConnection();
//HttpURLConnection connection = (HttpURLConnection) myUrl.openConnection();
//connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
connection.setConnectTimeout(10000);
connection.connect();
//is = myUrl.openStream();
is = connection.getInputStream();
File file = new File(context.getFilesDir(),fileName);
file.delete();
file = new File(context.getFilesDir(),fileName);
fos = new FileOutputStream(file);
byte data[] = new byte[1024];
String str ="";
int count = 0;
while ((count = is.read(data)) != -1) {
fos.write(data, 0, count);
}
is.close();
fos.close();
}
catch (Exception e) {
downloadedFileCallback.onError(e);
Log.e("DownloadedFile", "Unable to download : " + e.getMessage() + " cause :" + e.getCause());
return;
}
downloadedFileCallback.onDownloadedFinished();
readFile(context);
}
}).start();
}
public void readFile(Context context){
// read
try {
BufferedReader br = new BufferedReader(new FileReader(new File(context.getFilesDir(),fileName)));
String line;
while ((line = br.readLine()) != null) {
Log.v("DL", line);
}
br.close();
}
catch (Exception e) {
Log.e("DownloadedFile", "Unable to read : " + e.getMessage() + " cause :" + e.getCause());
}
//Log.v("DownloadedFile", text.toString());
}
其中myURL被称为like
URL myURL = new URL("http://pastebin.com/raw/FcVfLf5b");
在Log.v中,我可以看到我只下载了文件的一部分,而这一部分是不同的(可能是整个文件,一半,四分之一,我们不知道…)
可能是inputStream连接关闭得太快了。但是为什么呢?
最后一个问题,而不是使用Log.v检查文件是否正确下载。我在哪里可以在手机上找到它?我搜索了很多文件夹,但从未见过我的文件
谢谢你的建议
编辑:这里似乎是一样的,但没有人回答