Last active
May 30, 2017 08:01
-
-
Save vanjikumaran/5291194 to your computer and use it in GitHub Desktop.
File Upload Using JSP
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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.fileUploadModule</groupId> | |
<artifactId>FileUpload</artifactId> | |
<packaging>war</packaging> | |
<version>1.0-SNAPSHOT</version> | |
<name>FileUpload Maven Webapp</name> | |
<url>http://maven.apache.org</url> | |
<dependencies> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>3.8.1</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>commons-fileupload</groupId> | |
<artifactId>commons-fileupload</artifactId> | |
<version>1.2.2</version> | |
</dependency> | |
<dependency> | |
<groupId>commons-io</groupId> | |
<artifactId>commons-io</artifactId> | |
<version>2.4</version> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.httpcomponents</groupId> | |
<artifactId>httpclient</artifactId> | |
<version>4.2.3</version> | |
</dependency> | |
<dependency> | |
<groupId>javax.servlet</groupId> | |
<artifactId>servlet-api</artifactId> | |
<version>2.5</version> | |
<scope>provided</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<finalName>FileUpload</finalName> | |
</build> | |
</project> |
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
<%-- | |
Created by IntelliJ IDEA. | |
User: vanjikumaran | |
Date: 4/2/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></title> | |
</head> | |
<body> | |
</body> | |
</html> |
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
<%-- | |
Created by IntelliJ IDEA. | |
User: vanjikumaran | |
Date: 4/2/13 | |
Time: 2:25 PM | |
To change this template use File | Settings | File Templates. | |
--%> | |
<%@ page contentType="text/html;charset=UTF-8" language="java" %> | |
<html> | |
<head> | |
<title>Error Page</title> | |
</head> | |
<body> | |
Error Occurred | |
</body> | |
</html> |
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
<%-- | |
Created by IntelliJ IDEA. | |
User: vanjikumaran | |
Date: 4/2/13 | |
Time: 12:21 PM | |
To change this template use File | Settings | File Templates. | |
--%> | |
<%@ page contentType="text/html;charset=UTF-8" language="java" %> | |
<html> | |
<head> | |
<title>File Upload Sample</title> | |
</head> | |
<body> | |
<h1>Batch Order Upload</h1> | |
<form action="batchOrderProcess.jsp" method="post" enctype="multipart/form-data"> | |
Select a file to upload: <br/> | |
<input type="file" name="file" size="50"/> | |
<br/> | |
<input type="submit" value="Upload File"/> | |
</form> | |
</body> | |
</html> |
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
<html> | |
<body> | |
<h2>Hello World!</h2> | |
</body> | |
</html> |
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
<%-- | |
Created by IntelliJ IDEA. | |
User: vanjikumaran | |
Date: 4/2/13 | |
Time: 2:22 PM | |
To change this template use File | Settings | File Templates. | |
--%> | |
<%@ page contentType="text/html;charset=UTF-8" language="java" %> | |
<html> | |
<head> | |
<title>Success</title> | |
</head> | |
<body> | |
Success | |
</body> | |
</html> |
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
<!DOCTYPE web-app PUBLIC | |
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" | |
"http://java.sun.com/dtd/web-app_2_3.dtd" > | |
<web-app> | |
<display-name>Archetype Created Web Application</display-name> | |
</web-app> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment