问题描述
第一次遇到这个问题的时候,完全不知道是怎么回事?
它是我操作文件的时候报错的,和一个C#对接时发生的问题。
不管是百度还是谷歌,都把我引向错误的方向,说是对应文件地址没有权限???
解决方案
请好好debug看看你的InputStream流拿到没有?其实就是InputStream流空指针问题!它只是换了一种异常说法,通常你的代码是通过url获取流,如下代码:
public static byte[] getHtmlByteArray(final String url) throws IOException {
URL htmlUrl = null;
InputStream inStream = null;
htmlUrl = new URL(url);
URLConnection connection = htmlUrl.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) connection;
httpConnection.setConnectTimeout(5 * 1000);
httpConnection.setReadTimeout(5 * 1000);
int responseCode = httpConnection.getResponseCode();
if (responseCode == 200) {
inStream = httpConnection.getInputStream();
}
return inputStreamToByte(inStream);
}
这个代码中规中矩,问题出现在
if (responseCode == 200) {
这一行,你必须增加http状态
if (responseCode == 200 || responseCode == 206) {
否则,你无法拿到InputStream流。