• Post Reply Bookmark Topic Watch Topic
  • New Topic

easy file upload class

 
Markus Kramer2
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I'm looking for a fileupload-class. I know this has been discussed very often.
I've already used cos. But I don't like cos very much because it saves any files uploaded in a directory and then I've to delete all these files.
I'm looking for a class which gives me more control about the fileupload and which works though it's a multipart/form-data or a normal request.
I imagined it like this:
public String getPara( String name ) {...}
public String getFile( String name, String filename ) {...}
Hope such a class exists not only in my fantasy.
thank's,
Markus
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Markus Kramer2:
hi,
I'm looking for a fileupload-class. I know this has been discussed very often.
(etc etc)
Hope such a class exists not only in my fantasy.
thank's,
Markus

I've been a bit unsatisfied with the file upload samples out there too.
I'm in the process of rewriting one of my own with some of the functionality you are talking about, with the added advantage that it too implements javax.servlet.HttpServletRequest so that for the usual case it behaves exactly the same, for file uploads you have separate functionality to handle the file part...
except that it isn't in a usable form yet
(for those who care, cos refers to the com.oreilly.servlet package)
Anyone else have a useful roll your own ready?
Dave.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I don't like cos very much because it saves any files uploaded in a directory and then I've to delete all these files.

I imagine you mean after moving them where you want them, right?

The problem with a parser that *doesn't* upload to a temp directory first, is that the request would have to reside entirely in memory. What if you uploaded 5 MB worth of files? This would be quite a large drain on resources if it were a multi-user system.

So why don't we make a parser that parses first the directory to store a file, then the file itself? Because HTTP doesn't specify that the form *must* be received in the order that the form is laid out in HTML. This is covered in the FAQ for cos.

But maybe the latest version of the cos comes to the rescue? I haven't tried it out, but now the cos package does have a MultiPart class that implements HTTPRequest, and it does provide methods like getFile(), thanks to the new servlet filters.
 
Markus Kramer2
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, now I've written my own class to handle requests. what do you think about it?

/*
-----------------------------------------------------------
-- HttpRequest.java --
-- --
-- Version: 1.0 --
-- Author: Markus Kramer --
-- --
-----------------------------------------------------------
*/
package com.hadiag.servlet;
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
import java.util.Enumeration;
import com.oreilly.servlet.MultipartRequest;
/**
* This class provides access to the parameters and files submitted by a HTTP-form.
* It handles <code>multipart/form-data</code> and normal requests.
*
* @author Markus Kramer
* @version $Version$
*
*/
public class HttpRequest
{
HttpServletRequest req;
MultipartRequest mpr;
boolean multireq;

/**
* Constructs a new HttpRequest to handle the specified request and to save uploaded files.
* @param request the <code>HttpServletRequest</code>
* @param directorya directory for temporary files (must exist)
* @param sizethe maximum size of the POST content (bytes)
*
*/
public HttpRequest( HttpServletRequest request, String directory, int size )
{
req = request;
multireq = true;
try
{
mpr = new MultipartRequest( req, directory, size );
}
catch( IOException e )
{
multireq = false;
}
}

/**
* Returns the value of a parameter as a String, or an empty String if
* the parameter was not sent.
* If the parameter has multiple values, only the last one is returned.
* @param namethe parameter name
* @returnthe parameter value
*
*/
public String getPara( String name )
{
String value = null;
if( multireq )
{
value = mpr.getParameter(name);
}
else
{
value = req.getParameter(name);
}
if( value == null ) value = "";
return value;
}

/**
* Works like <code>getPara( String name )</code> but if
* the parameter was without a value, the default value is returned.
* @param namethe parameter name
* @param defaultvaluea default value
* @returnthe parameter value or the default value
*
*/
public String getPara( String name, String defaultvalue )
{
String value = getPara( name );
if( value.equals("") ) value = defaultvalue;
return value;
}

/**
* Returns the values of a parameter as a String array, or null if
* the parameter was not sent.
* If any field was sent without a value that entry is stored in the array as a null.
* @param namethe parameter name
* @returnthe parameter values
*
*/
public String[] getParaValues( String name )
{
String[] value = null;
if( multireq )
{
value = mpr.getParameterValues(name);
}
else
{
value = req.getParameterValues(name);
}
return value;
}

/**
* This method saves an uploaded file to a specified location.
* If the file doesn't exist or the request isn't <code>multipart/form-data</code>,
* null is returned.
* @param namethe parameter name
* (ie. <input type="file" name="<code>image1</code>" /> )
* @param filename the location where the uploaded file is saved
* @returna File object of the saved file
*
*/
public File getFile( String name, String filename )
{
if( multireq )
{
File fin = mpr.getFile(name);
if( fin != null )
{
try
{
File fout = new File( filename );
FileInputStream fis = new FileInputStream( fin );
FileOutputStream fos = new FileOutputStream( fout );
byte[] buf = new byte[1024];
int i = 0;
while( (i=fis.read(buf)) != -1 )
{
fos.write( buf, 0, i );
}
fis.close();
fos.close();
return fout;
}
catch( java.io.IOException e ) { return null; }
}
else return null;
}
else return null;
}

protected void finalize() throws Throwable
{
close();
}

/**
* To ensure that all temporary saved files are deleted, you can call this method.
*
*/
public void close()
{
if( multireq )
{
Enumeration files = mpr.getFileNames();
while( files.hasMoreElements() )
{
File f = mpr.getFile( (String)files.nextElement() );
if( f != null ) f.delete();
}
}
}
}
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use jspsmart free classes, it works great.jspsmart
I used it for our team share files.
Need Java real project experience? Join our project team here!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!