Saturday, November 2, 2013

Upload a file Using JSP

This demonstration will guide you, how to upload a file into folder (you can define the path) using JSP.

Pre-Request

Application server - I am using Wso2 Application Server. Since it is very user friendly to configure and mange my application using cool user interfaces.

Further Readings,
[1]http://wso2.org/library/articles/2012/10/getting-full-features-apache-tomcat-supported-wso2-application-server
[2] http://docs.wso2.org/wiki/display/AS510/Installation+Guide

Maven - As build tool i am using.

Further Readings,
[1] http://vanjikumaran.blogspot.com/2013/04/installing-maven-and-ant-in-ubuntu.html

let see the codes
<%--
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>



<%--
Created by IntelliJ IDEA.
User: vanji
Date: 6/22/13
Time: 2:07 PM
This example is demonstrate how to upload a file into given location of the server
--%>
<html lang="en">
<head>
<title>File Uploading Form</title>
</head>
<body>
<div>
<h1>Batch Order Upload</h1>
<form action="batch_Order_Process.jsp" method="post" enctype="multipart/form-data">
<ul>
<h3>File Upload:</h3>
Select a file to upload: <br />
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</ul>
</form>
</div>
</body>
</html>
view raw index.jsp hosted with ❤ by GitHub

No comments:

Post a Comment