Created
February 5, 2014 15:05
-
-
Save joachimhs/8825565 to your computer and use it in GitHub Desktop.
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 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