Skip to content

Instantly share code, notes, and snippets.

@christiangoudreau
Created September 12, 2011 14:19
Show Gist options
  • Save christiangoudreau/1211364 to your computer and use it in GitHub Desktop.
Save christiangoudreau/1211364 to your computer and use it in GitHub Desktop.
Extracter
public abstract class Extracter {
public void extract(HttpServletRequest request) throws FileUploadException, IOException {
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iterator = upload.getItemIterator(request);
while (iterator.hasNext()) {
FileItemStream item = iterator.next();
if (!item.isFormField()) {
byte[] content = extractContent(item);
String contentType = item.getContentType();
workOnItem(content, contentType);
}
}
}
public abstract void workOnItem(byte[] content, String contentType) throws IOException;
private byte[] extractContent(FileItemStream item) throws IOException {
InputStream stream = item.openStream();
ByteBuffer content = ByteBuffer.wrap(IOUtils.toByteArray(stream));
byte[] bytes = content.array();
IOUtils.closeQuietly(stream);
return bytes;
}
}
private class MyExtracter extends Extracter {
@Override
public void workOnItem(byte[] content, String contentType) throws IOException {
BlobKey hd = generateHd(content, contentType);
BlobKey midSize = generateMidSize(content, contentType);
BlobKey thumbnail = generateThumbnail(content, contentType);
createPicture(hd, midSize, thumbnail);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment