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!

Jakarta Commons FileUpload ; Internet Explorer Problem

843838Aug 12 2005 — edited Aug 12 2005
Hi all,

Environment:
Tomcat 5 ;Apache 2; JDK 1.5.0; Jakarta Commons Fileupload 1.0
OS: Windoze XP

Previously I've used jakarta commons fileupload package to succussfully to upload a file.

However, I am trying to check the content type of the file and throw an exception if its not a jpeg file. The following code works great when I use firefox. But it fails when I use Internet Explorer!

When I supply an <b>existing</b> jpg file on my desktop as the input to the HTML form, the code works fine. However if I enter a <b>non-existing</b> jpg filename, I get a "HTTP 500 Internal Server Error"! I expect to get the "Wrong content type!" message (which my JSP throws as an exception and should be caught by the error page). This problem happens only with Internet Explorer. With firefox, I get the "Wrong Content Type" message as expected.

What could be the problem? Please advise.

Thanks
Joe.

Code follows......



/************** file-upload.html *************/
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>File Upload</title>
<script type="text/javascript" language="JavaScript">
<!--
function fileTypeCheck() {
var fileName = document.uploadForm.pic.value;
if (fileName == "") {
alert ("Please select a file to upload!");
return false;
}
var indexOfExt = fileName.lastIndexOf (".");
if (indexOfExt < 0) {
alert('You can only upload a .jpg/.jpeg/.gif file!');
return false;
}
var ext = fileName.substring(indexOfExt);
ext = ext.toLowerCase();
if (ext != '.jpg' && ext != 'jpeg') {
alert('You selected a ' + ext + ' file; Please select a .jpg/.jpeg file instead!');
return false;
}
return true;
}
//--></script>
</head>
<form action="uploadPhoto.jsp" enctype="multipart/form-data" method="post" name="uploadForm" onSubmit="return fileTypeCheck();">
<input type="file" accept="image/jpeg,image/gif" name="pic" size="50" />

<input type="submit" value="Send" />
</form>
<body>
</body>
</html>


/*************** photoUpload.jsp **************/

<%@ page language="java" session="false" import="org.apache.commons.fileupload.*, java.util.*" isErrorPage="false" errorPage="uploadPhotoError.jsp" %>

<%!

public void processUploadedFile(FileItem item, ServletResponse response) throws Exception {
	try {

		// Process a file upload
	    	String contentType = item.getContentType();

		if (! contentType.equals("image/jpeg") && ! contentType.equals("image/pjpeg")) {
			throw new FileUploadException("Wrong content type!");

		}
	} catch (Exception ex) {
		throw ex;
	}
}

%>

<%
// Check that we have a file upload requeste
boolean isMultipart = FileUpload.isMultipartContent(request);

// Create a new file upload handler
DiskFileUpload upload = new DiskFileUpload();

// Parse the request
List /* FileItem */ items = upload.parseRequest(request);

// Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext()) {
    FileItem item = (FileItem) iter.next();

    if (! item.isFormField()) {
        processUploadedFile(item, response);
    }
}
%>

<html>
<head>
</head>
<body>
File uploaded succesfully! Thank you!
</body>
</html>


/******** uploadPhotoError.jsp ****/
<%@ page language="java" session="false" isErrorPage="true" %>

<html>
<head>
</head>
<body>
<%
out.println(exception.getMessage());
%>
</body>
</html>
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 9 2005
Added on Aug 12 2005
2 comments
340 views