Upload file and store the file in server some specific path.
index.jsp:
=======
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body>
<div
style="margin: 12% auto; width: 80%; box-shadow: 10px 10px 5px #888888; background-color: #DCD7D7; height: 500px;">
<center>
<br />
<div>
<div style="margin: 0px 5% 0 0; width: 96%;">
<h2>Crawling Quality Reports</h2>
</div>
<div>
<form action="TestController" method="post" enctype="multipart/form-data" onsubmit="return validateForm()" >
<table style="line-height: 2;">
<tr>
<td>Select Site</td>
<td>:</td>
<td><select name="site" ID="siteid">
<option selected="selected">none</option>
<option>Amazon</option>
<option>FlipKart</option>
<option>Snapdeal</option>
<option>Paytm</option>
<option>Jabong</option>
<option>Babyoye</option>
<option>Firstcry</option>
<option>Purplle</option>
<option>Nykaa</option>
<option>Pepperfry</option>
<option>fabfurnish</option>
<option>Urbanladder</option>
</select></td>
</tr>
<tr>
<td>Select Crawl type</td>
<td>:</td>
<td><select name="crawlType" ID="crawlTypeid">
<option selected="selected">none</option>
<option>C1</option>
<option>C2</option>
<option>C2_1</option>
<option>C2_3</option>
<option>P4</option>
</select></td>
</tr>
<tr>
<td>Select Crawl stage</td>
<td>:</td>
<td><select name="crawlStage">
<option selected="selected">none</option>
<option>FT</option>
<option>MT</option>
</select></td>
</tr>
<tr>
<td>Present Cycle</td>
<td>:</td>
<td><input type="text" name="presentcycle" id="txtpresentcycle" onkeypress="return isNumber(event)"></td>
</tr>
<tr>
<td>previous Cycle</td>
<td>:</td>
<td><input type="text" name="previousCycle" id="txtpreviouscycle" onkeypress="return isNumber(event)"></td>
</tr>
<tr>
<td>Select File for QA</td>
<td>:</td>
<td><input type="file" name="QAFile" /></td>
</tr>
</table>
<div>
<input type="submit" value="Submit" />
</div>
</form>
</div>
</div>
</center>
</div>
</body>
</html>
Add the below jars:
==============
==============
commons-fileupload-1.3.2.jar
commons-io-2.5.jar
commons-io-2.5.jar
Controller Class:
============
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String fileName = null;
String filePath = null;
String siteName = "";
String crawlType = "";
String crawlStage = "";
int presentCycle = 0;
int previousCycle =0;
System.out.println("inside---sss");
if (!ServletFileUpload.isMultipartContent(request)) {
// if not, we stop here
}
// configures upload settings
DiskFileItemFactory factory = new DiskFileItemFactory();
// sets memory threshold - beyond which files are stored in disk
factory.setSizeThreshold(MEMORY_THRESHOLD);
// sets temporary location to store files
factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
ServletFileUpload upload = new ServletFileUpload(factory);
// sets maximum size of upload file
upload.setFileSizeMax(MAX_FILE_SIZE);
// sets maximum size of request (include file + form data)
upload.setSizeMax(MAX_REQUEST_SIZE);
// constructs the directory path to store upload file
// this path is relative to application's directory
String uploadPath = getServletContext().getRealPath("")
+ File.separator + UPLOAD_DIRECTORY;
System.out.println("inside uploadPath " + uploadPath);
// creates the directory if it does not exist
File uploadDir = new File(uploadPath);
if (!uploadDir.exists()) {
uploadDir.mkdir();
}
try {
// parses the request's content to extract file data
System.out.println("inside try uploadPath " + uploadPath);
// @SuppressWarnings("unchecked")
List<FileItem> formItems = upload.parseRequest(request);
if (formItems != null && formItems.size() > 0) {
// iterates over form's fields
for (FileItem item : formItems) {
// processes only fields that are not form fields
if (!item.isFormField()) {
fileName = new File(item.getName()).getName();
filePath = uploadPath + File.separator + fileName;
File storeFile = new File(filePath);
// saves the file on disk
item.write(storeFile);
request.setAttribute("message", "Upload has been done successfully!");
}
else {
if (item.getFieldName().equals("site")) siteName=item.getString();
if (item.getFieldName().equals("crawlType")) crawlType=item.getString();
if (item.getFieldName().equals("crawlStage")) crawlStage=item.getString();
if (item.getFieldName().equals("presentcycle")) presentCycle=Integer.parseInt(item.getString());
if (item.getFieldName().equals("previousCycle")) previousCycle=Integer.parseInt(item.getString());
System.out.println("getFieldName " + item.getFieldName());
System.out.println("getFieldName " + item.getString());
}
}
}
} catch (Exception ex) {
request.setAttribute("message",
"There was an error: " + ex.getMessage());
}
No comments:
Post a Comment