开发者

Eclipse workspace issue

开发者 https://www.devze.com 2023-03-12 08:11 出处:网络
I am writing a servlet that will help upload files from a html form.I am using apache commons fileUpload library.

I am writing a servlet that will help upload files from a html form.I am using apache commons fileUpload library.

The issue is when I upload a file, I get an Access Denied permission to write to a folder in the web content although i have granted permissions to all folders. Moreover, I have another project in the same workspace where a similar upload is being done to another folder and it executes.

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)

Here's a code snippet.The doPost method

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(
                            开发者_如何学JAVA    "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());
            }


        }


It is likely that one of the following is true:

  • The path created for the file object does not represent a valid file. This might be because the status flag set to the return value of the isDirectory() method is being ignored.
  • Related to the above, the path is constructed using the category and movie name, apart from the file name. Either of the parameters might be invalid or null, resulting in an incorrect path.

Correcting these would require verification that all fragments of your file path are valid, before attempting any creation of the file.

0

精彩评论

暂无评论...
验证码 换一张
取 消