Created
December 22, 2010 11:02
-
-
Save pkozelka/751386 to your computer and use it in GitHub Desktop.
Processing upload requests with commons-upload
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
///... 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