java - Eclipse 工作区问题

标签 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());
            }


        }

最佳答案

以下情况之一可能是正确的:

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

纠正这些问题需要在尝试创建文件之前验证文件路径的所有片段均有效。

关于java - Eclipse 工作区问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6333449/

相关文章:

java - 为什么它说 "Cannot refer to a non-final variable i inside an inner class defined in a different method"?

java - 质数问题

java - 从操作栏菜单项捕获用户输入

java - Eclipse IDE 和导入问题

java - 如何修复错误 org.testng.eclipse.maven.MavenTestNGLaunchConfigurationProvider.getClasspath(Lorg/eclipse/debug/core/ILaunchConfiguration;)

java - 在eclipse中提取方法似乎不起作用

java - 哪种传输 JSON 数据的方式更优化?

java - Android 弹出式 Activity,位于任何其他应用之上

java - 如何在基于arm处理器32位的Linux中安装eclipse产品(没有gui,作为服务)

java - eclipse中类对象的键盘快捷键