Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

problem with uploading files using apache commons

843842Dec 21 2008 — edited Dec 26 2008
Hi
I am using WebLogic 10, Windows XP SP2, apache commons-fileupload-1.2.1.jar. I have a form which allows me to choose a file:
<html>
</head>
<body class="bodyClass">
<form enctype="multipart/form-data" method='post' action='getexcel.jsp'>
<table width="90%" align="center" cellpadding="4">
<tr>
<td colspan="2" nowrap class="title">    Locate the file you need to upload and press upload</td>
</tr>
<tr>
<td  valign="middle" nowrap width="20"><input type='file' name="file1" id="file1"></td>
<td align="left"><input type='submit' name="submit" value='Upload'></td>
</tr>
</table>
</form>
</body>
</html>
and the jsp which is supposed to read the file:
<?xml version="1.0"?>
<%@page import="org.apache.commons.fileupload.**, org.apache.commons.fileupload.servlet.ServletFileUpload, org.apache.commons.fileupload.disk.DiskFileItemFactory, org.apache.commons.io.FilenameUtils, java.util.*, java.io.File, java.lang.Exception"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>File Upload Example</title>
</head>
<body>
<h1>Data Received at the Server</h1>

<p>
<%

if (ServletFileUpload.isMultipartContent(request)) {
ServletFileUpload servletFileUpload = new ServletFileUpload(new DiskFileItemFactory());
List fileItemsList = servletFileUpload.parseRequest(request);
String optionalFileName = "";
FileItem fileItem = null;
Iterator it = fileItemsList.iterator();
while (it.hasNext()) {
FileItem fileItemTemp = (FileItem) it.next();
if (fileItemTemp.isFormField()) {
%>
<b>Name-value Info:</b>
Field name:<br />
<%= fileItemTemp.getFieldName() %>  
<%
if (fileItemTemp.getFieldName().equals("filename"))
optionalFileName = fileItemTemp.getString();
} else
fileItem = fileItemTemp;
}
if (fileItem != null) {
String fileName = fileItem.getName();
%>
<b>Uploaded File Info:</b>
Field name:
<%= fileItem.getFieldName() %>  
File name:<br />
<%= fileName %>
File size:
<%= fileItem.getSize() %>

<%
/* Save the uploaded file if its size is greater than 0. */<br />
if (fileItem.getSize() > 0) {
if (optionalFileName.trim().equals(""))
fileName = FilenameUtils.getName(fileName);
else
fileName = optionalFileName;
String dirName = "/file_uploads/";
File saveTo = new File(dirName + fileName);
try {
fileItem.write(saveTo);
%>
<b>The uploaded file has been saved successfully.</b>
<%} catch (Exception e) {%>
<b>An error occurred when we tried to save the uploaded file.</b>
<%
}
}
}
}
%>
</p>
</body>
</html>
The problem is that it doesn't even seem to detect the data that I am trying to upload. When running the lines:
    Iterator it = fileItemsList.iterator();
it.hasNext() will return a false. If you have any idea please help.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 23 2009
Added on Dec 21 2008
8 comments
449 views