3 Replies Latest reply on Feb 3, 2006 8:38 PM by 807590

    java.io.File - pathnames on tomcat

    807590
      Hi,

      I am trying to implement a file upload feature on my website using Flash 8 and jakarta commons FileUpload.

      I'm running into problems with the java.io.File class. I want to save uploaded files in a folder not relative to the calling page's context.

      When I use the following three lines, the files get saved correctly (in an "/uploads" folder in the current context):
      File fullFile = new File(item.getName());
      File savedFile = new File(getServletContext().getRealPath("/uploads"), fullFile.getName());
      item.write(savedFile);

      However, when I use the following instead of the second line, I get an http 500 error:
      File savedFile = new File("/absolute/path/to/another/context/uploads", fullFile.getName());

      What's strange is when I put in a println, I see that
      getServletContext().getRealPath("/uploads") = /usr/local/tomcat/webapps/mywebapp/uploads

      However, when I substitute the string in the new File(String parent, String child) method, I also get a http 500 error:

      File savedFile = new File("/usr/local/tomcat/webapps/mywebapp/uploads", fullFile.getName());

      This is the stack trace:

      2006-02-03 12:12:05 StandardWrapperValve[jsp]: Servlet.service() for servlet jsp
      threw exception
      org.apache.jasper.JasperException: /upload.jsp(11,2) Unterminated <% tag
      at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorH
      andler.java:39)
      at org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.j
      ava:409)
      at org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.j
      ava:134)
      at org.apache.jasper.compiler.Parser.parseScriptlet(Parser.java:796)
      ...


      Am I understanding the methods incorrectly?

      Any help would be greatly appreciated! I am completely stuck
      Thank you.
        • 1. Re: java.io.File - pathnames on tomcat
          807590
          I think, though am not certain, that in order to do this you would need to first create a second web application, then in the first servlet create a ServletContext that references the second. And use that when you call getRealPath().

          "There is one context per "web application" per Java Virtual Machine."

          Have a closer look at the ServletContext API.

          I think, yet again, that when you're hard coding you're absolute path (not a good idea) the resource you're referring to is not in the current Servlets context, hence is unavailable.

          I'm probably all wrong though.

          The servlets forum is the better place to post this question.

          -S.
          • 2. Re: java.io.File - pathnames on tomcat
            DrClap
            That's nothing to do with java.io.File or pathnames or any of the things your post was talking about.
            2006-02-03 12:12:05 StandardWrapperValve[jsp]: Servlet.service() for servlet jsp
            threw exception
            org.apache.jasper.JasperException: /upload.jsp(11,2) Unterminated <% tag
            at org.apache.jasper.compiler.DefaultErrorHandler.jspErro r(DefaultErrorH andler.java:39)
            at org.apache.jasper.compiler.ErrorDispatcher.dispatch(Er rorDispatcher.j ava:409)
            Look closely at the stack trace: Unterminated <% tag is the message. org.apache.jasper.compiler is the package of the class with the problem. That means your JSP doesn't compile because of a syntax error. Most likely you are missing one of the two " characters that delimit a string, so your %> closing tag looks like part of the string.

            This is just one of the many reasons you shouldn't be putting code like this into JSPs.
            • 3. Re: java.io.File - pathnames on tomcat
              807590
              Thanks. I must have deleted the "%>" by accident while I was fiddling with the code (this is what happens when you look at the same code for too long!!). That explains why File savedFile = new File("/usr/local/tomcat/webapps/mywebapp/uploads", fullFile.getName()); didn't work earlier. Now it does.
              However, the following still does not work:

              ile savedFile = new File("/absolute/path/to/another/context/uploads", fullFile.getName());

              This is the stack trace:



              Generated servlet error:
              /usr/local/jakarta-tomcat-5.0.28/work/Catalina/www.jupiterhotelvideo.com/_/org/a
              pache/jsp/upload_jsp.java:39: 'try' without 'catch' or 'finally'
              try {
              ^


              Generated servlet error:
              /usr/local/jakarta-tomcat-5.0.28/work/Catalina/www.jupiterhotelvideo.com/_/org/a
              pache/jsp/upload_jsp.java:109: '}' expected
              ^
              3 errors



              at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErro
              rHandler.java:84)
              at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher
              .java:332)
              at org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:412)
              at org.apache.jasper.compiler.Compiler.compile(Compiler.java:472)
              ...

              Do you know what the problem could be?

              I will try sroot's suggestions to reference another servletcontext...though the place where I'd like to put the "/uploads" folder isn't a web application, but just a directory on my server....

              Thanks for your help; online forums are a life-saver!!!
              That means your JSP
              doesn't compile because of a syntax error. Most
              likely you are missing one of the two " characters
              that delimit a string, so your %> closing tag looks
              like part of the string.