Created
June 29, 2011 00:29
-
-
Save mrdaemon/1052567 to your computer and use it in GitHub Desktop.
Upload servlet using the new Streaming upload in apache commons' uploadfile. This is how it should be done.
This file contains hidden or 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
package org.underwares.uploadtest; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import org.apache.commons.fileupload.FileItemIterator; | |
import org.apache.commons.fileupload.FileItemStream; | |
import org.apache.commons.fileupload.FileUploadException; | |
import org.apache.commons.fileupload.servlet.ServletFileUpload; | |
import org.apache.commons.fileupload.util.Streams; | |
/** | |
* A crappy Test servlet to test and validate file storage | |
* strategies in a static/dynamic isolated web application env. | |
* | |
* @author Alexandre Gauthier <[email protected]> | |
* | |
*/ | |
public class Upload extends HttpServlet { | |
/* FIXME: THIS SHOULD COME FROM CONFIGURATION VALUES | |
* Debug constants, for tweaking. Feel free to get this from your | |
* configuration system, or better yet from JNDI so the app server can | |
* tell you where to place the files. | |
*/ | |
private static final String STATIC_SERVER_URL = | |
"\\\\qlsvrwd-n\\static-ql\\__TESTING__\\"; | |
// Set up log4j logger | |
private static final Logger logger = Logger.getLogger( | |
Upload.class.getName()); | |
/** | |
* Process requests. Practically, handles HTTP <code>POST</code> alone, | |
* because GET has no buisness being for now. | |
* It was not written in <code>doPost()</code> because maybe | |
* I'll wish to call it also for <code>doGet()</code> or whatever. | |
* | |
* @param request servlet request | |
* @param response servlet response | |
* @throws ServletException if a servlet-specific error occurs | |
* @throws IOException if an I/O error occurs | |
*/ | |
protected void processRequest(HttpServletRequest request, | |
HttpServletResponse response) throws ServletException, IOException { | |
ServletFileUpload upload; // upload object | |
FileItemIterator fit; // Iterator for uploaded file(s) | |
// Abort if form is not multipart | |
if(!ServletFileUpload.isMultipartContent(request)) | |
return; | |
upload = new ServletFileUpload(); | |
try { | |
fit = upload.getItemIterator(request); | |
// Process any file(s) resulting from request. | |
while(fit.hasNext()) { | |
FileItemStream item = fit.next(); | |
String name = item.getFieldName(); | |
InputStream stream = item.openStream(); | |
if(item.isFormField()){ | |
logger.log(Level.INFO, | |
"Form had field {0}, set to value: {1}.", | |
new Object[]{name, Streams.asString(stream)} | |
); | |
} else { | |
logger.log(Level.INFO, | |
"Filefield! {0}, {1}({3})", | |
new Object[]{ | |
name, | |
item.getName(), | |
item.getContentType(), | |
} | |
); | |
/* Process the uploaded file | |
* TODO: Some sort of "Oh hey this actually exists, right" | |
* pre dump checks would be nice. | |
*/ | |
File dstfile = new File(STATIC_SERVER_URL, item.getName()); | |
long slen; // Total stream lenght, for display | |
logger.log(Level.INFO, "Starting to receive {0}...", | |
dstfile.getAbsolutePath()); | |
// Dump stream into file, and close handle | |
slen = Streams.copy(stream, | |
new FileOutputStream(dstfile), true); | |
logger.log(Level.INFO, "Complete! Copied {0} bytes!", slen); | |
} | |
} | |
} catch (FileUploadException ex) { | |
logger.log(Level.SEVERE, | |
"Oh dear god, file upload crapped out!", ex); | |
} catch (Exception ex) { | |
logger.log(Level.SEVERE, "Unexpected error", ex); | |
} | |
} | |
/** | |
* Handles the HTTP <code>POST</code> method. | |
* | |
* @param request servlet request | |
* @param response servlet response | |
* @throws ServletException if a servlet-specific error occurs | |
* @throws IOException if an I/O error occurs | |
*/ | |
@Override | |
protected void doPost(HttpServletRequest request, HttpServletResponse response) | |
throws ServletException, IOException { | |
processRequest(request, response); | |
} | |
/** | |
* Returns a short description of the servlet. | |
* @return a String containing servlet description | |
*/ | |
@Override | |
public String getServletInfo() { | |
return "Test Servlet for upload of stuffs."; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment