Last active
December 19, 2015 07:09
-
-
Save amrfaissal/5917136 to your computer and use it in GitHub Desktop.
File Upload Servlet
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
import java.io.File; | |
import java.io.IOException; | |
import java.util.List; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import org.apache.commons.fileupload.FileItem; | |
import org.apache.commons.fileupload.FileItemFactory; | |
import org.apache.commons.fileupload.disk.DiskFileItemFactory; | |
import org.apache.commons.fileupload.servlet.ServletFileUpload; | |
import org.apache.commons.io.FilenameUtils; | |
/** | |
* Servlet to handle file upload requests | |
* | |
* @author faissal.elamraoui | |
*/ | |
public class FileUploadServlet extends HttpServlet { | |
/** | |
* | |
*/ | |
private static final long serialVersionUID = -5069680745987375734L; | |
private static final String UPLOAD_DIRECTORY = "/path/to/upload/directory"; | |
@Override | |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) | |
throws ServletException, IOException { | |
super.doGet(req, resp); | |
} | |
@Override | |
protected void doPost(HttpServletRequest req, HttpServletResponse resp) | |
throws ServletException, IOException { | |
// Process only multipart requests | |
if (ServletFileUpload.isMultipartContent(req)) { | |
// Create the Upload Directory | |
File uploadDir = getUploadDir(); | |
if (!uploadDir.exists()) { | |
uploadDir.mkdirs(); | |
} | |
// Create a factory for disk-based file items | |
FileItemFactory factory = new DiskFileItemFactory(); | |
// Create a new file upload handler | |
ServletFileUpload upload = new ServletFileUpload(factory); | |
// Parse the request | |
try { | |
List<FileItem> items = upload.parseRequest(req); | |
for (FileItem item : items) { | |
// Process only file upload - discard other form item types | |
if (item.isFormField()) | |
continue; | |
String filename = item.getName(); | |
// Get only the file name not whole path | |
if (filename != null) { | |
filename = FilenameUtils.getName(filename); | |
} | |
File uploadedFile = new File(UPLOAD_DIRECTORY, filename); | |
if (uploadedFile.createNewFile()) { | |
item.write(uploadedFile); | |
resp.setStatus(HttpServletResponse.SC_CREATED); | |
resp.flushBuffer(); | |
} else { | |
uploadedFile.renameTo(new File(UPLOAD_DIRECTORY, | |
filename + ".old")); | |
item.write(uploadedFile); | |
resp.setStatus(HttpServletResponse.SC_CREATED); | |
resp.flushBuffer(); | |
} | |
} | |
} catch (Exception e) { | |
resp.sendError( | |
HttpServletResponse.SC_INTERNAL_SERVER_ERROR, | |
"An error occured while creating the file: " | |
+ e.getMessage()); | |
} | |
} else { | |
resp.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, | |
"Request contents type is not supported by the servlet."); | |
} | |
} | |
private File getUploadDir() { | |
return new File(UPLOAD_DIRECTORY); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment