Skip to content

Instantly share code, notes, and snippets.

@joachimhs
Created February 5, 2014 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joachimhs/8825565 to your computer and use it in GitHub Desktop.
Save joachimhs/8825565 to your computer and use it in GitHub Desktop.
public class UploadEmberfestPhotoHandler extends ContenticeHandler {
private static final Logger logger = Logger.getLogger(UploadEmberfestPhotoHandler.class.getName());
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, FullHttpRequest fullHttpRequest) throws Exception {
String jsonReturn = "{}";
String messageContent = getHttpMessageContent(fullHttpRequest);
String contentType = getContentType(fullHttpRequest);
int boundaryIndex = contentType.indexOf("boundary=");
byte[] boundary = contentType.substring((boundaryIndex + 9)).getBytes();
logger.info("boundary: " + contentType.substring((boundaryIndex + 9)));
ByteArrayInputStream content = new ByteArrayInputStream(messageContent.getBytes());
MultipartStream multipartStream = new MultipartStream(content, boundary);
boolean nextPart = multipartStream.skipPreamble();
while (nextPart) {
String key = null;
String header = multipartStream.readHeaders();
if (header.contains("name=")) {
key = header.substring((header.indexOf("name=") + 6), header.length() - 2).trim();
if (key.contains("\"")) {
key = key.replaceAll("\"", "");
}
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
multipartStream.readBodyData(outputStream);
if (key != null && key.startsWith("photo;")) {
FileOutputStream out = null;
try {
out = new FileOutputStream(new File("/Users/jhsmbp/Projects/TeknologihusetWeb/site/uploads/test.png"));
out.write(outputStream.toByteArray());
out.flush();
} finally {
if (out != null) {
out.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
nextPart = multipartStream.readBoundary();
}
writeContentsToBuffer(channelHandlerContext, jsonReturn, "application/json; charset=UTF-8");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment