写入servlet中的文件 [英] Writing to a file in a servlet

查看:35
本文介绍了写入servlet中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 servlet 中工作,并具有以下代码:

I am working in a servlet and has this code :

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

But everytime it is throwing an error saying Access Denied.. Even when there is no lock on that file and there is no file whose name is 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)

推荐答案

尝试关闭FileWriter:

Try close FileWriter:

writer.close();

我想象会发生什么:Tomcat中的一个线程创建了文件并终止但没有关闭其句柄,因此不会从文件系统视图释放文件锁.另一个线程试图打开该文件以进行写入,但是OS仍无法授予对该文件的写入权限,因为它已经有一个挂起的文件.

I imagine what happens: One thread in Tomcat creates the file and terminates but not closes its handle, so from file system view file lock is not released. Another thread tries to open it for write but OS still cannot grant write access to the file, because it already has one pending.

这称为资源泄漏:垃圾收集器不会释放程序员手动分配的资源(此处为IO句柄)

This is called resource leak: Garbage Collector does not release resources allocated by programmer manually (here IO handle)

这篇关于写入servlet中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆