Skip to content

Instantly share code, notes, and snippets.

@vanjikumaran
Created July 4, 2013 05:57
Show Gist options
  • Save vanjikumaran/5925223 to your computer and use it in GitHub Desktop.
Save vanjikumaran/5925223 to your computer and use it in GitHub Desktop.
JSP file processor
<%--
Created by IntelliJ IDEA.
User: vanji
Date: 6/22/13
Time: 2:18 PM
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.io.*,java.util.*" %>
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="org.apache.commons.fileupload.disk.*" %>
<%@ page import="org.apache.commons.fileupload.servlet.*" %>
<%
int MAX_MEMORY_SIZE = 1024 * 1024 * 2;
final int MAX_REQUEST_SIZE = 1024 * 1024;
DiskFileItemFactory factory = new DiskFileItemFactory();
// Sets the size threshold beyond which files are written directly to disk.
factory.setSizeThreshold(MAX_MEMORY_SIZE);
// Sets the directory used to temporarily store files that are larger
// than the configured size threshold. We use temporary directory for java
factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
// constructs the folder where uploaded file will be stored
String uploadFolder = "/home/vanjikumaran/Area51/IN";
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Set overall request size constraint
upload.setSizeMax(MAX_REQUEST_SIZE);
try {
// Parse the request
List items = upload.parseRequest(request);
for (Object item1 : items) {
FileItem item = (FileItem) item1;
if (!item.isFormField()) {
String fileName = new File(item.getName()).getName();
String filePath = uploadFolder + File.separator + fileName;
File uploadedFile = new File(filePath);
// saves the file to upload directory
item.write(uploadedFile);
}
}
response.sendRedirect("success.jsp");
} catch (Exception e) {
response.sendRedirect("errorPage.jsp");
}
%>
<html>
<head>
<title>File Processor</title>
</head>
<body>
system currently processing the file
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment