输入流数据的索引越界异常
Index out of bound exception with inputstream data
我试图刷新本地计算机文件中的数据以响应。但在某些时候我得到一个 IndexOutOfBoundsException
.
FileInputStream inputStream = new FileInputStream(downloadFile);
OutputStream outStream = response.getOutputStream();
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer, 0, 4096)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
inputStream.close();
outStream.close();
上面的代码是我正在尝试的。给定的 downloadFile
路径是正确的,它一直工作到 while
循环。但是随后 IndexOutOfBoundsException
发生了。我用 inputStream.read(buffer)
试过了,但没有用。
提供的代码运行良好;由于没有给出关于 response
对象的信息,我已将 OutputStream
修改为 FileOutputStream
;只是为了测试。
下面的代码段运行良好。
public class Test
{
public static void main(String args[]) throws IOException
{
FileInputStream inputStream = new FileInputStream("C:\readme.txt");
FileOutputStream outputStream = new FileOutputStream("D:\readme1.txt");
//OutputStream outStream = response.getOutputStream();
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer, 0, 4096)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
inputStream.close();
outputStream.close();
}
}
我试图刷新本地计算机文件中的数据以响应。但在某些时候我得到一个 IndexOutOfBoundsException
.
FileInputStream inputStream = new FileInputStream(downloadFile);
OutputStream outStream = response.getOutputStream();
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer, 0, 4096)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
inputStream.close();
outStream.close();
上面的代码是我正在尝试的。给定的 downloadFile
路径是正确的,它一直工作到 while
循环。但是随后 IndexOutOfBoundsException
发生了。我用 inputStream.read(buffer)
试过了,但没有用。
提供的代码运行良好;由于没有给出关于 response
对象的信息,我已将 OutputStream
修改为 FileOutputStream
;只是为了测试。
下面的代码段运行良好。
public class Test
{
public static void main(String args[]) throws IOException
{
FileInputStream inputStream = new FileInputStream("C:\readme.txt");
FileOutputStream outputStream = new FileOutputStream("D:\readme1.txt");
//OutputStream outStream = response.getOutputStream();
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer, 0, 4096)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
inputStream.close();
outputStream.close();
}
}