Eclipse工作区问题

时间:2011-06-13 16:40:00

标签: java eclipse

我正在编写一个servlet,它将帮助从html表单上传文件。我正在使用apache commons fileUpload库。

问题是当我上传文件时,虽然我已经授予了对所有文件夹的权限,但我获得了拒绝访问权限以写入Web内容中的文件夹。此外,我在同一个工作区中有另一个项目,其中正在对另一个文件夹进行类似的上传并执行。

java.io.FileNotFoundException: C:\Users\Veera\VirtualClassroom\IP_Project1.1\WebContent\Videos\Kannada\Jodha Akbar (Access is denied.)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:205)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:157)
    at org.apache.commons.fileupload.disk.DiskFileItem.write(DiskFileItem.java:426)
    at codes.UploadVideo.doPost(UploadVideo.java:81)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:811)

这是一段代码片段.doPost方法

protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        if (!(ServletFileUpload.isMultipartContent(request))) {

        } else {
            try {
                ServletFileUpload in = new ServletFileUpload(
                        new DiskFileItemFactory());
                System.out.println("hello");
                String song_name = null, movie = null, category = null;
                List inp = in.parseRequest(request);
                Iterator it = inp.iterator();
                FileItem fi = null;
                while (it.hasNext()) {
                    fi = (FileItem) it.next();
                    if (fi.isFormField()) {
                        if (fi.getFieldName().equals("song_name"))
                            song_name = fi.getString();
                        else if (fi.getFieldName().equals("film"))
                            movie = fi.getString();
                        else if (fi.getFieldName().equals("group"))
                            category = fi.getString();
                        System.out.println(song_name + movie + category);

                    } else {

                        String filename = fi.getName();

                        boolean status = new File(
                                "C:/Users/Veera/VirtualClassroom/IP_Project1.1/WebContent/Videos/"
                                        + category + "/" + movie).isDirectory();
                        System.out.println(status);
                        File fw = new File(
                                "C:/Users/Veera/VirtualClassroom/IP_Project1.1/WebContent/Videos/"
                                        + category + "/" + movie + "/"
                                        + filename);
                        fi.write(fw);

                    }

                }
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
                System.out.println(e.getMessage());
            }


        }

1 个答案:

答案 0 :(得分:0)

以下情况之一可能属实:

  • 为文件对象创建的路径不代表有效文件。这可能是因为忽略了设置为status方法返回值的isDirectory()标志。
  • 与上述相关,除文件名外,使用类别和电影名称构建路径。这两个参数都可能无效或为空,从而导致路径不正确。

在尝试创建文件之前,更正这些内容需要验证文件路径的所有片段是否有效。