Skip to content

Instantly share code, notes, and snippets.

@pkozelka
Created December 22, 2010 11:02
Show Gist options
  • Save pkozelka/751386 to your computer and use it in GitHub Desktop.
Save pkozelka/751386 to your computer and use it in GitHub Desktop.
Processing upload requests with commons-upload
///... somewhere in the servlet, called from doPost:
private void processUploadRequest(HttpServletRequest request, PrintWriter pw) throws FileUploadException, IOException {
final FileItemFactory factory = new DiskFileItemFactory();
final ServletFileUpload upload = new ServletFileUpload(factory);
@SuppressWarnings("unchecked")
final List<FileItem> items = upload.parseRequest(request);
log("LIST: " + items);
for (FileItem item : items) {
final String fieldName = item.getFieldName();
if (item.isFormField()) {
log("Got a form field: " + fieldName + " = " + item.getString());
} else if ("log".equals(fieldName)) {
log("Got an uploaded file: " + fieldName + ", name = " + item.getName());
pw.println("# " + item.getName());
final InputStream stream = item.getInputStream();
try {
uploadStream(pw, stream);
} finally {
stream.close();
}
} else {
pw.println("ERROR: unsupported upload field:" + fieldName);
}
}
}
//...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment