这个异常抛出的两种情况:
(1)拒绝访问
(2)系统找不到指定的路径
在构造一个File对象时,指定的文件路径是什么都可以,就算不存在也能够构造File对象。
但是现在你要对文件进行输入和输出操作的时候,也就是InputStream和OutputStream操作时,如果填写的路径不存在,那么就会报系统找不到指定路径,如果指定的是目录时,就会报拒绝访问异常。
我的问题出现的原因是所在的路径的文件不存在,所以就报了异常。根据文件的位置,修正了路径,因此异常就解决了。
文件代码如下:
String path=savePath+"/"+uuid+"."+ext;
File file = new File(path);
if(!file.exists()){
//不存在
request.setAttribute("name", name);
return "download_error";//返回下载文件不存在
}
if(!inOnLineExt(ext)){
response.setContentType("application/octet-stream");
}
// 根据不同浏览器 设置response的Header
String userAgent = request.getHeader("User-Agent").toLowerCase();
if(userAgent.indexOf("msie")!=-1||userAgent.indexOf("trident")!=-1){
//ie浏览器
//System.out.println("ie浏览器");
response.addHeader("Content-Disposition", "inline;filename=" + URLEncoder.encode(name,"utf-8"));
}else{
response.addHeader("Content-Disposition", "inline;filename=" + new String(name.getBytes("utf-8"),"ISO8859-1"));
}
response.addHeader("Content-Length", ""+file.length());
//以流的形式下载文件
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
toClient = new BufferedOutputStream(response.getOutputStream());
toClient.write(buffer);
toClient.flush();
return null;
}catch (Exception e) {
e.printStackTrace();
response.reset();
return "exception";//返回异常页面
}finally{
if(toClient!=null){
try {
toClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}