AntoniRyszard
asked on
Exception using Apache FileUpLoad?
Hello,
I constructed a basic html page to post several files to a java servlet. In the servlet I am using the apache tool fileupload to accept files, and write these to a folder.
In the code below the servlet loops through the posted files. I noticed the fileupload method (getName()) returns the complete path of the file on the users hard-drive. For example
c:\windows\desktop\tree.jp g
And I am using to write the files to tomcat\files\
When it comes to writing the file an exception is thrown.
Does anyone think I should be parsen the getName() value to recieve just the file name (tree.jpg) and not the full path c:\windows\desktop\tree.jp g.
I hope this makes sense.
Thanks
Iterator iter = items.iterator();
while(iter.hasNext()){
FileItem item = (FileItem) iter.next();
File uploadedFile = new File("/tomcat/files/" + item.getName());
//item.write(uploadedFile) ;
}
I constructed a basic html page to post several files to a java servlet. In the servlet I am using the apache tool fileupload to accept files, and write these to a folder.
In the code below the servlet loops through the posted files. I noticed the fileupload method (getName()) returns the complete path of the file on the users hard-drive. For example
c:\windows\desktop\tree.jp
And I am using to write the files to tomcat\files\
When it comes to writing the file an exception is thrown.
Does anyone think I should be parsen the getName() value to recieve just the file name (tree.jpg) and not the full path c:\windows\desktop\tree.jp
I hope this makes sense.
Thanks
Iterator iter = items.iterator();
while(iter.hasNext()){
FileItem item = (FileItem) iter.next();
File uploadedFile = new File("/tomcat/files/" + item.getName());
//item.write(uploadedFile)
}
ASKER
The problem is the exception is displayed in the tomcat java frame. And it's not being written to the tomcat log files.
When I comment out the line item.write(uploadedFile);
There is not exception.
Could it be the write cannot find the tomca/files folder to write to?
I wondered also since the getName() method returns:
c:\windows\desktop\tree.jp g.
I thought this line below would return a file with the path
File uploadedFile = new File("/tomcat/files/" + item.getName());
\tomcat\files\c:\windows\d esktop\tre e.jpg
Does this seem correct?
Thanks
When I comment out the line item.write(uploadedFile);
There is not exception.
Could it be the write cannot find the tomca/files folder to write to?
I wondered also since the getName() method returns:
c:\windows\desktop\tree.jp
I thought this line below would return a file with the path
File uploadedFile = new File("/tomcat/files/" + item.getName());
\tomcat\files\c:\windows\d
Does this seem correct?
Thanks
Does:
/tomcat/files
exist?
And can the UID that runs tomcat write into it?
/tomcat/files
exist?
And can the UID that runs tomcat write into it?
The getName() thing (afaik) should be ok :-)
ASKER
The files folder path is:
c:\tomcat\files
Does this seem correct?
c:\tomcat\files
Does this seem correct?
errr... what? the file you select with the <input type="file"?
That doesn't matter
That doesn't matter
ASKER
In the html I using the input type file.
For the momen I am building the web app on my pc, so tomcat is located at the folder c:\tomcat.
And I created a folder in tomcat to hold the files posted to the servlet.
The servlet will write the files to c:\tomcat\files\
For the momen I am building the web app on my pc, so tomcat is located at the folder c:\tomcat.
And I created a folder in tomcat to hold the files posted to the servlet.
The servlet will write the files to c:\tomcat\files\
I think you're missing something. Do you have the <form enctype="multipart/form-da ta"> set? What are you using to retrieve the file? Are you using a MultipartFormDataRequest? It's unfortunatly not as easy as just copying the filename into the appropriate location. I can't really tell what's going on but I'm thinking you're not focusing on the right thing. Post the code where you get the file from the request object.
ASKER
Thanks
In my html page the form tag is using enctype="multipart/form-da ta".
I posted below the main parts of the servlet, I am using the apache fileipload tool. The servlet compiles without an error, and only throws an exception when this line is uncommented item.write(uploadedFile). When I comment out this line there's not exception.
At the moment tomcat is installed on my pc at c:\tomcat, so for testing when I write back the files posted to the servlet. I am trying to write the posted files to c:\tomcat\files\
Thanks
public class TestServlet extends HttpServlet{
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException{
try{
boolean isMultipart = FileUpload.isMultipartCont ent(req);
if(isMultipart){
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory) ;
List items = upload.parseRequest(req);
Iterator iter = items.iterator();
while(iter.hasNext()){
FileItem item = (FileItem) iter.next();
File uploadedFile = new File("/tomcat/files/" + item.getName());
//item.write(uploadedFile) ;
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
In my html page the form tag is using enctype="multipart/form-da
I posted below the main parts of the servlet, I am using the apache fileipload tool. The servlet compiles without an error, and only throws an exception when this line is uncommented item.write(uploadedFile). When I comment out this line there's not exception.
At the moment tomcat is installed on my pc at c:\tomcat, so for testing when I write back the files posted to the servlet. I am trying to write the posted files to c:\tomcat\files\
Thanks
public class TestServlet extends HttpServlet{
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException{
try{
boolean isMultipart = FileUpload.isMultipartCont
if(isMultipart){
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory)
List items = upload.parseRequest(req);
Iterator iter = items.iterator();
while(iter.hasNext()){
FileItem item = (FileItem) iter.next();
File uploadedFile = new File("/tomcat/files/" + item.getName());
//item.write(uploadedFile)
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
That last line would create file in the files folder within your web app root.
ASKER
Thanks
Would the loop in my servlet be changed to something like this?
Iterator iter = items.iterator();
while(iter.hasNext()){
FileItem item = (FileItem) iter.next();
String filePath = item.getName();
String FileName = (parsen filePath)
File uploadedFile = new File(application.getRealPa th("/files /") + FileName)
item.write(uploadedFile);
}
I was not sure what the (application) variable is?
Thanks
Would the loop in my servlet be changed to something like this?
Iterator iter = items.iterator();
while(iter.hasNext()){
FileItem item = (FileItem) iter.next();
String filePath = item.getName();
String FileName = (parsen filePath)
File uploadedFile = new File(application.getRealPa
item.write(uploadedFile);
}
I was not sure what the (application) variable is?
Thanks
ASKER
Do you believe there are any good approaches to parsen the String filePath = item.getName(); value.
Thanks
Thanks
Just put the following line in a JSP.
<%=application.getRealPath ("/")%>
application is a reference to a ServletContext instance.
>Do you believe there are any good approaches to parsen the String filePath = item.getName(); value.
Maybe use String methods to find last file path separator.
if(-1 != filePath.lastIndexOf("\\")
FileName = filePath.substring(filePat h.lastInde xOf("\\")) ; (for windows only)
<%=application.getRealPath
application is a reference to a ServletContext instance.
>Do you believe there are any good approaches to parsen the String filePath = item.getName(); value.
Maybe use String methods to find last file path separator.
if(-1 != filePath.lastIndexOf("\\")
FileName = filePath.substring(filePat
ASKER
In the user guide to the fileupload tool.
They only used item.write() to write there files
http://jakarta.apache.org/commons/fileupload/using.html
Do you think there are better tools available to capture files from the user and store these on the server?
They only used item.write() to write there files
http://jakarta.apache.org/commons/fileupload/using.html
Do you think there are better tools available to capture files from the user and store these on the server?
>Do you think there are better tools available to capture files from the user and store these on the server?
Apache commons fileupload is very popular.
Are you able to upload to temp file ?
File uploadedFile = new File(application.getRealPa th("/files /temp.jpg" ) )
item.write(uploadedFile);
The path to the uploaded file is then
tomcat/webapps/yourContext Directory/ files/temp .jpg
Apache commons fileupload is very popular.
Are you able to upload to temp file ?
File uploadedFile = new File(application.getRealPa
item.write(uploadedFile);
The path to the uploaded file is then
tomcat/webapps/yourContext
ASKER
Thanks
I found another example on web which uses the apache fileupload tool. To access the filename for example temp.txt they used in a servlet:
//iter loops through files.
FileItem item = (FileItem) iter.next();
String fileName = item.getName();
fileName = (new File(fileName)).getName();
And this seems to return the temp.txt, and not the path to the file on the users computer.
I found another example on web which uses the apache fileupload tool. To access the filename for example temp.txt they used in a servlet:
//iter loops through files.
FileItem item = (FileItem) iter.next();
String fileName = item.getName();
fileName = (new File(fileName)).getName();
And this seems to return the temp.txt, and not the path to the file on the users computer.
What exception?