2012-10-19 26 views
1

我在的servlet工作,有這樣的代碼:寫入文件在servlet

public void doPost(blah blah){ 

    response.setContentType("text/html"); 

    String datasent = request.getParameter("dataSent"); 
    System.out.println(datasent); 

    try{ 

     FileWriter writer = new FileWriter("C:/xyz.txt"); 
     writer.write("hello"); 


     System.out.println("I wrote"); 
    }catch(Exception ex){ 
     ex.printStackTrace(); 
    } 

    response.getWriter().write("I am from server"); 

} 

但每次它拋出一個錯誤說拒絕訪問.. 即使沒有鎖該文件並沒有名稱爲的文件C:/xyz.txt

我該怎麼辦? 。 ;(

java.io.FileNotFoundException: C:\xyz.txt (Access is denied) 
at java.io.FileOutputStream.open(Native Method) 
at java.io.FileOutputStream.<init>(FileOutputStream.java:212) 
at java.io.FileOutputStream.<init>(FileOutputStream.java:104) 
at java.io.FileWriter.<init>(FileWriter.java:63) 
at test.TestServlet.doPost(TestServlet.java:49) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164) 
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108) 
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:558) 
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379) 
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243) 
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:259) 
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:237) 
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:281) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) 
at java.lang.Thread.run(Thread.java:722) 
+0

你嘗試改變你的路徑: - 'd:/ xyz.txt'? –

+0

是的,我試過一切?每次它說拒絕訪問。但相同的代碼工作在覈心java –

+0

請顯示堆棧跟蹤。 – home

回答

-1

在這個過程中,不要打開file.Try如下:

FileWriter writer = new FileWriter("C:/xyz.txt", true); 
BufferedWriter out = new BufferedWriter(writer); 
out.write("your text"); 
out.close(); 
+0

沒有仍然訪問被拒絕 –

-1

例外表明,它是FileNotFound異常的請嘗試讓新的文件第一次。 嘗試用下面的代碼。

File file = File file("c:/xyz.txt"); 
if(!file.exists()){ // this will return boolean {true} if file exists. 
    file.createNewFile(); // create new empty file. 
} 
FileWriter writer = new FileWriter("C:/xyz.txt", true); 
BufferedWriter out = new BufferedWriter(writer); 
out.write("your text"); 
out.close(); 

否問題..嘗試使用不同的讀者和作家要弄清楚它。

String currentExecutablePath = System.getProperty("user.dir"); 
    String rootPath = currentExecutablePath + "xyz.txt"; 
    File file = new File(rootPath); 
    ServletOutputStream op = res.getOutputStream(); 
    if(file.exists()){ 
     int length = 0; 
     res.setContentType("application/octet-stream"); 
     res.setContentLength((int) file.length()); 

     byte[] bbuf = new byte[1000]; 
     DataInputStream in = new DataInputStream(new FileInputStream(file));    
     while ((in != null) && ((length = in.read(bbuf)) != -1)) { 
      op.write(bbuf, 0, length); 
     } 
    } 
+0

沒有成功............................. –

0

嘗試尋找這個site好像這就是你想找的(向下滾動某些方面)

+0

這是如何與我的問題有關..... .....這只是在瀏覽器上打印的東西.............就是這樣 –

4

嘗試關閉FileWriter的:

writer.close(); 

我想象會發生什麼:在Tomcat中的一個線程創建文件並終止但不關閉其句柄,因此從文件系統視圖文件鎖定不被釋放。另一個線程嘗試打開它進行寫入,但是操作系統仍然無法授予對該文件的寫入權限,因爲它已經有一個掛起。

這就是所謂的資源泄漏:垃圾回收器不會釋放由程序員手動分配的資源(這裏IO處理)