java.io.FileNotFoundException异常的问题

这个异常抛出的两种情况:
(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();
						}
		            }
				}
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

草莓味少女vv

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值