解决java.io.FileNotFoundException: D:\tempfile (拒绝访问。)

时间:2022-05-25 08:32:51

问题:java.io.FileNotFoundException: D:\tempfile (拒绝访问。)at java.io.FileOutputStream.open(Native Method)at java.io.FileOutputStream.(FileOutputStream.java:221)at java.io.FileOutputStream.(FileOutputStream.java:171)at com.sh.xy.servlet.UploadServlet.doPost(UploadServlet.java:55)at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)...

解决办法:在填写文件的路径时一定要具体到文件

源码:

public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("已经接收到请求");
		
		//从request当中获取流信息
		InputStream fileSource = request.getInputStream();
		String tempFileName = "D:/tempfile.png";
		
		//tempFile指向临时文件
		File tempFile = new File(tempFileName);

		//outputStream文件输出流指向这个临时文件
		FileOutputStream outputStream = new FileOutputStream(tempFile);
		byte  b[] = new byte[1024];
		int n;
		while((n=fileSource.read(b)) != -1){
			outputStream.write(b, 0, n);
		}
		
		//关闭输入输出流
		outputStream.close();
		fileSource.close();
	}

}