Android 连接上的FileNotFoundException.getInputStream();API级别上的行小于23
我正在使用URLConnection从服务器下载HTML文件,但我得到了FileNotFoundException。我的道路是正确的。使用相同的代码,相同的文件在API级别为25的android上下载,但在API级别上下载的文件少于未下载的文件。还有connection.getContentLength();总是给311的值。我没有得到任何答案来解决这个问题。我在此添加我的代码 我的类文件代码是Android 连接上的FileNotFoundException.getInputStream();API级别上的行小于23,android,filenotfoundexception,urlconnection,Android,Filenotfoundexception,Urlconnection,我正在使用URLConnection从服务器下载HTML文件,但我得到了FileNotFoundException。我的道路是正确的。使用相同的代码,相同的文件在API级别为25的android上下载,但在API级别上下载的文件少于未下载的文件。还有connection.getContentLength();总是给311的值。我没有得到任何答案来解决这个问题。我在此添加我的代码 我的类文件代码是 String HomeScreenResourcefilename = WebHomescreenR
String HomeScreenResourcefilename = WebHomescreenResObj.getString("FileName");
int ProductTargetID = WebHomescreenResObj.getInt("ProductTargetID");
String WebURL_Part1 = context.getResources().getString(R.string.FileDownloadRootPath)+"/ExhibitorData/";
String WebURL_Part2 = GlobalVariables.tradeShowName+" - "+GlobalVariables.exhibitorName+"/HTMLHomeScreen/"+ProductTargetID +"/"+HomeScreenResourcefilename;
String WebURL = WebURL_Part1 + WebURL_Part2;
try {
URL url = new URL(WebURL);
File file = new File(context.getFilesDir(), HomeScreenResourcefilename);
URLConnection connection = url.openConnection();
connection.connect();
FileOutputStream fileOutput = new FileOutputStream(file);
//Stream used for reading the data from the internet
InputStream inputStream = connection.getInputStream();
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ((bufferLength = inputStream.read(buffer)) > 0) {
fileOutput.write(buffer, 0, bufferLength);
}
fileOutput.close();
} catch (Exception e1) {
e1.printStackTrace();
LogE("error in 14 :"+e1);
}
使用HttpURLConnection,然后使用connection.getResponseCode()获取状态代码。如果大于400,这可能就是原因。
更新:使用url编码。您能尝试更改File File=new File(context.getFilesDir(),HomeScreenResourcefilename)吗;to File File=新文件(context.getFilesDir().getAbsolutePath(),HomeScreenResourcefilename);请告诉异常在哪一行抛出。InputStream InputStream=connection.getInputStream();正在引发异常。还使用相同的代码成功下载了来自不同位置的同一服务器的另一个文件。getAbsolutePath()也不工作。也有同样的例外。还请告诉我311内容大小。Whysame size始终引发异常。我正在使用URLConnection,因此无法使用connection.getResponseCode()。是否有其他方法检查响应代码??URLConnection没有这种方法。改为使用HttpURLConnection。在API 21上,我得到400作为响应代码,但在API级别24上,我得到200。如何解决文件存在的问题?我的回答是400:尝试编码url。看看这个