Java File Upload Applet


Server side upload handling

It is essential that the proper handler be implemented in order to receive uploads. JumpLoader issues HTTP POST request using "multipart/form-data" content type for each file it uploads. I.e. this acts just like standard web browser (see RFC1867).

Thus, any compliant server side technology could be used to process a request, such as ASP, ColdFusion, JSP, Perl, Python, Ruby, etc.

There are plenty of tutorials on the web about implementing file upload handlers, for any technology mentioned above.

PHP upload handler

PHP handler for file upload is straightforward. Please download archive: handler with test page containing upload HTML form.

Please refer to move_uploaded_file() PHP function manual for more info.

Below is a commented code snippet:

//	specify file parameter name
$file_param_name = 'file';

//	retrieve uploaded file name
$file_name = $_FILES[ $file_param_name ][ 'name' ];

//	retrieve uploaded file path (temporary stored by php engine)
$source_file_path = $_FILES[ $file_param_name ][ 'tmp_name' ];

//	construct target file path (desired location of uploaded file) -
//	here we put to the web server document root (i.e. '/home/wwwroot')
//	using user supplied file name
$target_file_path = $_SERVER[ 'DOCUMENT_ROOT' ] . "/" . $file_name;

//	move uploaded file
echo "Moving file " . $source_file_path . " > " . $target_file_path . ": ";
if( move_uploaded_file( $source_file_path, $target_file_path ) ) {
	echo "success";
} else{
	echo "failure";

JSP upload handler

This uses com.oreilly.servlet component to handle multipart. The most important thing is to configure MultipartRequest — target directory (where servlet should save uploaded file) and some constraints, for example maximum allowed request length, file naming policy etc.

Please download archive containing files:

  • uploadForm.jsp — sample HTML form with file input.
  • uploadHandler.jsp — a handler for upload. Besides work on saving POSTed file, this also logs request parameters and saved file info to servlet default logger and optionally writes same info to response.
  • WEB-INF\lib\cos.jar — com.oreilly.servlet library.

Actually this is a ready to deploy Java Web Application, but most likely you need to adjust target directory (set to c:/temp/uploaded).