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\Tomca t~1.1\weba pps\testjs ps\uploads (Access is denied)
type Status report
message C:\Progra~1\Apache~1\Tomca t~1.1\weba pps\testjs ps\uploads (Access is denied)
description The requested resource (C:\Progra~1\Apache~1\Tomc at~1.1\web apps\testj sps\upload s (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-da ta">
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 .fileuploa d.DiskFile Upload"%>
<%@ page import="org.apache.commons .fileuploa d.FileItem "%>
<%@ page import="org.apache.commons .fileuploa d.FileUplo ad"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.Iterator "%>
<%@ page import="java.io.File"%>
<%@ page import="java.io.FileOutput Stream"%>
<html>
<body>
<%
boolean isMultipart = FileUpload.isMultipartCont ent(reques t);
DiskFileUpload upload = new DiskFileUpload();
List items = upload.parseRequest(reques t);
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\\weba pps\\testj sps\\uploa ds");
//testDir.mkdir();
//File testFile = new File("C:\\Progra~1\\Apache ~1\\Tomcat ~1.1\\weba pps\\testj sps\\uploa ds\\test.f ile");
//FileOutputStream fos = new FileOutputStream(testFile) ;
//fos.write("test data".getBytes());
//fos.close();
out.println("<h1>"+fullFil e.getName( )+"</h1>") ;
File savedFile = new File("C:\\Progra~1\\Apache ~1\\Tomcat ~1.1\\weba pps\\testj sps\\uploa ds\\"+full File.getNa me());
out.println("<h1>"+savedFi le.getAbso lutePath() +"</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\Tomca t~1.1\weba pps\testjs ps\uploads \sample1.d oc
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
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\Tomca
type Status report
message C:\Progra~1\Apache~1\Tomca
description The requested resource (C:\Progra~1\Apache~1\Tomc
Apache Tomcat/4.1.31
The html page that contains the form is as follows:
<html>
<body>
<form name="myform" action="fileuploaddemo.jsp
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
<%@ page import="org.apache.commons
<%@ page import="org.apache.commons
<%@ page import="java.util.List"%>
<%@ page import="java.util.Iterator
<%@ page import="java.io.File"%>
<%@ page import="java.io.FileOutput
<html>
<body>
<%
boolean isMultipart = FileUpload.isMultipartCont
DiskFileUpload upload = new DiskFileUpload();
List items = upload.parseRequest(reques
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"
}
}
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
//testDir.mkdir();
//File testFile = new File("C:\\Progra~1\\Apache
//FileOutputStream fos = new FileOutputStream(testFile)
//fos.write("test data".getBytes());
//fos.close();
out.println("<h1>"+fullFil
File savedFile = new File("C:\\Progra~1\\Apache
out.println("<h1>"+savedFi
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\Tomca
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
ASKER
Hi,
Thanks for the respones, the actual jsp is :
<%@ page import="org.apache.commons .fileuploa d.DiskFile Upload"%>
<%@ page import="org.apache.commons .fileuploa d.FileItem "%>
<%@ page import="org.apache.commons .fileuploa d.FileUplo ad"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.Iterator "%>
<%@ page import="java.io.File"%>
<%@ page import="java.io.FileOutput Stream"%>
<html>
<body>
<%
boolean isMultipart = FileUpload.isMultipartCont ent(reques t);
DiskFileUpload upload = new DiskFileUpload();
List items = upload.parseRequest(reques t);
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\\weba pps\\testj sps\\uploa ds");
testDir.mkdir();
File testFile = new File("C:\\Progra~1\\Apache ~1\\Tomcat ~1.1\\weba pps\\testj sps\\uploa ds\\test.f ile");
FileOutputStream fos = new FileOutputStream(testFile) ;
fos.write("test data".getBytes());
fos.close();
out.println("<h1>"+fullFil e.getName( )+"</h1>") ;
File savedFile = new File("C:\\Progra~1\\Apache ~1\\Tomcat ~1.1\\weba pps\\testj sps\\uploa ds\\"+full File.getNa me());
out.println("<h1>"+savedFi le.getAbso lutePath() +"</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
Thanks for the respones, the actual jsp is :
<%@ page import="org.apache.commons
<%@ page import="org.apache.commons
<%@ page import="org.apache.commons
<%@ page import="java.util.List"%>
<%@ page import="java.util.Iterator
<%@ page import="java.io.File"%>
<%@ page import="java.io.FileOutput
<html>
<body>
<%
boolean isMultipart = FileUpload.isMultipartCont
DiskFileUpload upload = new DiskFileUpload();
List items = upload.parseRequest(reques
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"
}
}
else
{
File fullFile = new File(item.getName());
File testDir = new File("C:\\Progra~1\\Apache
testDir.mkdir();
File testFile = new File("C:\\Progra~1\\Apache
FileOutputStream fos = new FileOutputStream(testFile)
fos.write("test data".getBytes());
fos.close();
out.println("<h1>"+fullFil
File savedFile = new File("C:\\Progra~1\\Apache
out.println("<h1>"+savedFi
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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
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
e.g.
out.println("<h1>"+fullFil