写入servlet中的文件

时间:2012-10-19 16:37:48

标签: java file servlets

我正在使用 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 个答案:

答案 0 :(得分:4)

尝试关闭FileWriter:

writer.close();

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

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

答案 1 :(得分:0)

试着看看这个site似乎就是你要找的东西(向下滚动)

答案 2 :(得分:-1)

在此过程中,请勿打开文件。请按以下步骤操作:

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

答案 3 :(得分:-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);
        }
    }