Created
September 12, 2011 14:19
-
-
Save christiangoudreau/1211364 to your computer and use it in GitHub Desktop.
Extracter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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