FileNotFoundExcption on connection.getInputStream(); 在 API 级



我正在使用URLConnection从服务器下载HTML文件,但我得到FileNotFoundException。我的道路是正确的。使用相同的代码,在 API 级别 25 的 android 上下载相同的文件,但在 API 级别下载的文件少于未下载的文件。还有connection.getContentLength((;总是给311值。我没有得到任何解决这个问题的答案。我特此添加我的代码。

我的类文件代码是

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 编码。

最新更新