解決java.io.FileNotFoundException: D:\tempfile (拒絕訪問。)
阿新 • • 發佈:2019-02-09
問題: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(); } }