%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="org.apache.commons.fileupload.*" %>
Rad Upload Plus
| Files Uploaded |
| File Name |
File size |
<%
/*
* This is a sample jsp script that demonstrates handling file uploads using
* jsp. It is provided free of charge and with no warranty.
* In order to make use of this JSP you need to have the commons file upload
* library installed.
*/
int i=0;
/*
* Use a pathname that the servlet container has write permissions as
* your save path.
*/
String savePath="";
String parentPath=request.getParameter("userfile_parent");
try {
System.out.println( "CONTENT-LENGHT" + request.getContentLength());
System.out.println("The session id = " + session.getId());
DiskFileUpload fu = new DiskFileUpload();
// maximum size before a FileUploadException will be thrown
fu.setSizeMax(5000000);
// maximum size that will be stored in memory
fu.setSizeThreshold(4096);
// the location for saving data that is larger than getSizeThreshold()
fu.setRepositoryPath("/tmp/");
List fileItems = fu.parseRequest(request);
Iterator item = fileItems.iterator();
System.out.println("top level = " + parentPath);
while(item.hasNext())
{
FileItem fi = (FileItem) item.next();
if(fi.isFormField())
{
if(fi.getFieldName().equals("userfile_parent") && parentPath == null)
{
parentPath = fi.getString();
System.out.println(fi.getFieldName() + " = " + fi.getString());
}
continue;
}
String fileName = null;
if(parentPath != null && !parentPath.equals(""))
{
fileName = fi.getName().replaceFirst(parentPath,"");
}
else
{
String[] parts = fi.getName().split("/");
fileName = parts[parts.length-1];
}
System.out.println("Original " + fi.getName() +" & localname " + fileName);
if(fileName != null && !fileName.equals(""))
{
if( (i %2)==1)
{
out.print("");
}
else
{
out.print("
");
}
out.println("| "+ fileName +" | " + fi.getSize() +" |
");
i++;
if(! savePath.equals(""))
{
File f = new File(savePath + fileName);
File parent = f.getParentFile();
if(! parent.exists())
{
parent.mkdirs();
}
System.out.println("f = " + f.getAbsolutePath());
fi.write(f);
}
}
}
}
catch (Exception ex) {
// ex.printStackTrace();
}
%>
Sample JSP Upload handler provided by
Rad Inks
have you seen our Secure FTP Applet or
our Multimedia Messaging Solution?