问题: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();
}
}
版权声明:本文为CSDN博主「TC_sun」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/sinat_37812248/article/details/77600460