Link to home
Start Free TrialLog in
Avatar of stefarg
stefarg

asked on

JSP using Apache Commons FileUpload: Access Denied

Hi,

I am having a problem using the the apache commons fileupload libraries to upload a file to my tomcat server.  Every time I try to upload a file I get the error:

HTTP Status 404 - C:\Progra~1\Apache~1\Tomcat~1.1\webapps\testjsps\uploads (Access is denied)
type Status report
message C:\Progra~1\Apache~1\Tomcat~1.1\webapps\testjsps\uploads (Access is denied)
description The requested resource (C:\Progra~1\Apache~1\Tomcat~1.1\webapps\testjsps\uploads (Access is denied)) is not available.
Apache Tomcat/4.1.31

The html page that contains the form is as follows:
<html>
<body>
<form name="myform" action="fileuploaddemo.jsp" method="post" enctype="multipart/form-data">
      Specify your name:<br />
      <input type="text" name="name" size="15"/><br />
      Specify your File:<br /> <input type="file" name="myfile"><br /><br />
      <input type="submit" name="Submit" value="Submit your files"/
</body>
</html>

and the JSP is as follows:
<%@ page import="org.apache.commons.fileupload.DiskFileUpload"%>
<%@ page import="org.apache.commons.fileupload.FileItem"%>
<%@ page import="org.apache.commons.fileupload.FileUpload"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.Iterator"%>
<%@ page import="java.io.File"%>
<%@ page import="java.io.FileOutputStream"%>
<html>
<body>
<%
boolean isMultipart = FileUpload.isMultipartContent(request);
DiskFileUpload upload = new DiskFileUpload();
List items = upload.parseRequest(request);
Iterator itr = items.iterator();
while(itr.hasNext())
{
      FileItem item = (FileItem) itr.next();
          if(item.isFormField())
      {
              String fieldName = item.getFieldName();
            if(fieldName.equals("name"))
            {
                  request.setAttribute("msg", "Thank You: " + item.getString());
            }
      }            
      else
      {
            //Test if write permissions are ok with JSP by creating a directory and then a file
            //File fullFile  = new File(item.getName());
            //File testDir = new File("C:\\Progra~1\\Apache~1\\Tomcat~1.1\\webapps\\testjsps\\uploads");
            //testDir.mkdir();
            //File testFile = new File("C:\\Progra~1\\Apache~1\\Tomcat~1.1\\webapps\\testjsps\\uploads\\test.file");
            //FileOutputStream fos = new FileOutputStream(testFile);
            //fos.write("test data".getBytes());
            //fos.close();
            out.println("<h1>"+fullFile.getName()+"</h1>");  
            File savedFile = new File("C:\\Progra~1\\Apache~1\\Tomcat~1.1\\webapps\\testjsps\\uploads\\"+fullFile.getName());
            out.println("<h1>"+savedFile.getAbsolutePath()+"</h1>");
            item.write(savedFile);
      }
}
%>
<h1>done!</h1>
</body>
</html>


I thought it might be that the JSP did not have access to write to that directory so I commented out the " item.write(savedFile);" and uncommented the lines that create the directory and the file and that worked fine i.e. it present me with the following page:

sample1.doc
C:\Progra~1\Apache~1\Tomcat~1.1\webapps\testjsps\uploads\sample1.doc
done!

also I checked and the "upload" directory was created and the file test.file also existed and contained the text "test data".

If anyone can help me with this I'd be really grateful,
Thanks,
Stef


SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It's not that you haven't initialised fullFile is it?

e.g.

 out.println("<h1>"+fullFile.getName()+"</h1>"); //File fullFile  = new File(item.getName()); is commented out
Actually...I re-read your question...and that is odd behaviour...

:-/
Bloodredsun is right...that JSP will not run...can you post the real JSP code that doesn't work?
an alternative to both Tim and my earlier answer is that if fullFile.getName() returns an empty string, you will be trying to write over the directoty with a file, which will throw this error.
Avatar of stefarg
stefarg

ASKER

Hi,

Thanks for the respones, the actual jsp is :

<%@ page import="org.apache.commons.fileupload.DiskFileUpload"%>
<%@ page import="org.apache.commons.fileupload.FileItem"%>
<%@ page import="org.apache.commons.fileupload.FileUpload"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.Iterator"%>
<%@ page import="java.io.File"%>
<%@ page import="java.io.FileOutputStream"%>
<html>
<body>
<%
boolean isMultipart = FileUpload.isMultipartContent(request);
DiskFileUpload upload = new DiskFileUpload();
List items = upload.parseRequest(request);
Iterator itr = items.iterator();
while(itr.hasNext())
{
      FileItem item = (FileItem) itr.next();
          if(item.isFormField())
      {
              String fieldName = item.getFieldName();
            if(fieldName.equals("name"))
            {
                  request.setAttribute("msg", "Thank You: " + item.getString());
            }
      }            
      else
      {

            File fullFile  = new File(item.getName());
            File testDir = new File("C:\\Progra~1\\Apache~1\\Tomcat~1.1\\webapps\\testjsps\\uploads");
            testDir.mkdir();
            File testFile = new File("C:\\Progra~1\\Apache~1\\Tomcat~1.1\\webapps\\testjsps\\uploads\\test.file");
            FileOutputStream fos = new FileOutputStream(testFile);
            fos.write("test data".getBytes());
            fos.close();
            out.println("<h1>"+fullFile.getName()+"</h1>");  
            File savedFile = new File("C:\\Progra~1\\Apache~1\\Tomcat~1.1\\webapps\\testjsps\\uploads\\"+fullFile.getName());
            out.println("<h1>"+savedFile.getAbsolutePath()+"</h1>");
            item.write(savedFile);
      }
}
%>
<h1>done!</h1>
</body>
</html>

Sorry I had mixed up the different commenting in/out of code.  This jsp fails repeatedly for me.

Thanks,
Stef
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of stefarg

ASKER

Hi Again,

Thanks for all your, the problem turned out to be my configuration, I had the server.xml set to redirect standard connections to SSL.  I don't know why this blocked uploading but when I removed this it worked.  Anyone any ideas why this might cause the problem?

Thanks,
Stef
Avatar of stefarg

ASKER

Hi Folks,

None of the answers were exactly correct for the problem, but I found them useful none the less.  I think bloodredsun was closest I will award him the majority of the points but in the interests of fairness I will split the points.

Thanks folks,
Stef