1

我正在使用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)
4

4 回答 4

5

尝试关闭 FileWriter:

writer.close();

我想象会发生什么: Tomcat 中的一个线程创建文件并终止但不关闭其句柄,因此从文件系统视图文件锁定未释放。另一个线程尝试打开它以进行写入,但操作系统仍然无法授予对该文件的写入访问权限,因为它已经有一个待处理。

这称为资源泄漏:Garbage Collector 不会释放程序员手动分配的资源(此处为 IO 句柄)

于 2013-04-07T19:56:25.610 回答
0

尝试查看此站点似乎就是您要查找的内容(向下滚动某些方式)

于 2012-10-20T22:54:55.140 回答
-1

在此过程中,请勿打开文件。尝试如下:

FileWriter writer = new FileWriter("C:/xyz.txt", true);
BufferedWriter out = new BufferedWriter(writer);
out.write("your text");
out.close();
于 2012-10-19T16:49:27.577 回答
-1

该异常表明是FileNotFound Exception。请先尝试制作新文件。尝试使用以下代码。

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);
        }
    }
于 2012-10-19T17:13:34.140 回答