Hey all,
I'm trying to create an endpoint on my server where users can post one or more
large files. These files will then be processes and pushed to S3. For testing
purposes, I am not processing and pushing to S3 yet, just calculating an sha1
hash and printing it to std out. The code below works, but I couldn't figure
out how to do this without accessing classes called "FileItemStream" and, more
ominously, "DiskFileItemFactory", so I wanted to make sure this code doesn't
actually try to write the files to disk. I want the input stream to be pulling
the data straight from the client. I don't see another way to do this.
I am using org.restlet.ext.fileupload
thanks!
@Post("multipart")
public Representation postFiles(Representation entity) {
if( entity == null ) {
return fail( Status.CLIENT_ERROR_BAD_REQUEST,
"Multipart/form-data required" );
}
if( ! MediaType.MULTIPART_FORM_DATA.equals(entity.getMediaType(), true)
) {
return fail( Status.CLIENT_ERROR_BAD_REQUEST,
"Multipart/form-data required" );
}
DiskFileItemFactory factory = new DiskFileItemFactory();
RestletFileUpload upload = new RestletFileUpload(factory);
try {
FileItemIterator fii = upload.getItemIterator(entity);
while( fii.hasNext() ) {
MessageDigest md =
MessageDigest.getInstance("SHA-1");
FileItemStream fis = fii.next();
InputStream is = fis.openStream();
while( true ) {
byte[] input = new byte[1024];
int amount = is.read(input);
if( amount == -1 )
break;
md.update(input, 0, amount);
}
is.close();
byte[] sha1hash = md.digest();
System.out.println( fis.getFieldName() + " : "
+ fis.getName() + " : " + byteArray2Hex(sha1hash) );
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
....
-----------------------------
Bjorn Roche
http://www.xonami.com
Audio Collaboration
http://blog.bjornroche.com
------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=3016298