image.java
------------
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.disk.*;
import org.apache.commons.fileupload.servlet.*;
public class image extends HttpServlet {
private static final long serialVersionUID = 1L;
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
}
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
String ename ="";
String empcnumber = "";
String empemailid = "";
String address1 = "";
String address2 = "";
int count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0;
if (!isMultipart) {
System.out.println("File Not Uploaded");
} else {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;
FileItem fileItem = null;
String filePath = "D:\\Tomcat 6.0\\webapps\\Upload\\images";
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
Iterator itr = items.iterator();
while (itr.hasNext()) {
fileItem = (FileItem) itr.next();
if (fileItem.isFormField()) {
String name = fileItem.getFieldName();
String value = fileItem.getString();
if (name.equals("empname")) {
ename = value;
count1 = 1;
}
if (name.equals("address1")) {
address1 = value;
count2 = 2;
}
if (name.equals("address2")) {
address2 = value;
count5 = 5;
}
if (name.equals("contactnumber")) {
empcnumber = value;
count3 = 3;
}
if (name.equals("emailid")) {
count4 = 4;
empemailid = value;
}
} else {
try {
File uploadedFile = null;
String myFullFileName = fileItem.getName(), myFileName = "", slashType = (myFullFileName.lastIndexOf("\\") > 0) ? "\\" : "/";
int startIndex = myFullFileName.lastIndexOf(slashType);
myFileName = myFullFileName.substring(startIndex + 1,myFullFileName.length());
uploadedFile = new File(filePath, myFileName);
fileItem.write(uploadedFile);
out.println("");
out.println("");
out.println("
");
if(count1==1)
out.println("
");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
public void init() throws ServletException {
// Put your code here
}
}
index.jsp
---------
<html>
<head><title>Upload page</title></head>
<body>
<form action="image" method="post" enctype="multipart/form-data">
<center>
<table border="2">
<tr>
<td align="right">Employee Name:</td>
<td ><input type="text" name="empname"></td>
</tr>
<tr>
<td align="right">Employee Address:</td>
<td ><input type="text" name="address1"></td>
</tr>
<tr>
<td>
</td>
<td>
<input type="text" name="address2">
</td>
</tr>
<tr>
<td align="right">Contact Number:</td>
<td ><input type="text" name="contactnumber"></td>
</tr>
<tr>
<td align="right">Employee Email ID:</td>
<td ><input type="text" name="emailid"></td>
</tr>
<tr>
<td align="right">Employee Image </td>
<td>
<input name="file" type="file" id="file">
</td>
</tr>
<tr>
<td align="center">
<input type="submit" name="Submit" value="Submit"/>
<input type="reset" name="Reset" value="Reset"/>
</td>
</tr>
</table>
</center>
</form>
</body>
</html>
download these jar files
------------------------
commons-fileupload-1.2
commons-io-1.3.1
commons-lang-2.1
------------
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.disk.*;
import org.apache.commons.fileupload.servlet.*;
public class image extends HttpServlet {
private static final long serialVersionUID = 1L;
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
}
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
String ename ="";
String empcnumber = "";
String empemailid = "";
String address1 = "";
String address2 = "";
int count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0;
if (!isMultipart) {
System.out.println("File Not Uploaded");
} else {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;
FileItem fileItem = null;
String filePath = "D:\\Tomcat 6.0\\webapps\\Upload\\images";
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
Iterator itr = items.iterator();
while (itr.hasNext()) {
fileItem = (FileItem) itr.next();
if (fileItem.isFormField()) {
String name = fileItem.getFieldName();
String value = fileItem.getString();
if (name.equals("empname")) {
ename = value;
count1 = 1;
}
if (name.equals("address1")) {
address1 = value;
count2 = 2;
}
if (name.equals("address2")) {
address2 = value;
count5 = 5;
}
if (name.equals("contactnumber")) {
empcnumber = value;
count3 = 3;
}
if (name.equals("emailid")) {
count4 = 4;
empemailid = value;
}
} else {
try {
File uploadedFile = null;
String myFullFileName = fileItem.getName(), myFileName = "", slashType = (myFullFileName.lastIndexOf("\\") > 0) ? "\\" : "/";
int startIndex = myFullFileName.lastIndexOf(slashType);
myFileName = myFullFileName.substring(startIndex + 1,myFullFileName.length());
uploadedFile = new File(filePath, myFileName);
fileItem.write(uploadedFile);
out.println("");
out.println("");
out.println("
"); out.println(""); out.println(" |
if(count1==1)
out.println("
Name: | "+ename+" |
Addresss: | "+address1+" |
"+address2+" | |
Contact No | "+empcnumber+" |
Email ID | "+empemailid+" |
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
public void init() throws ServletException {
// Put your code here
}
}
index.jsp
---------
<html>
<head><title>Upload page</title></head>
<body>
<form action="image" method="post" enctype="multipart/form-data">
<center>
<table border="2">
<tr>
<td align="right">Employee Name:</td>
<td ><input type="text" name="empname"></td>
</tr>
<tr>
<td align="right">Employee Address:</td>
<td ><input type="text" name="address1"></td>
</tr>
<tr>
<td>
</td>
<td>
<input type="text" name="address2">
</td>
</tr>
<tr>
<td align="right">Contact Number:</td>
<td ><input type="text" name="contactnumber"></td>
</tr>
<tr>
<td align="right">Employee Email ID:</td>
<td ><input type="text" name="emailid"></td>
</tr>
<tr>
<td align="right">Employee Image </td>
<td>
<input name="file" type="file" id="file">
</td>
</tr>
<tr>
<td align="center">
<input type="submit" name="Submit" value="Submit"/>
<input type="reset" name="Reset" value="Reset"/>
</td>
</tr>
</table>
</center>
</form>
</body>
</html>
download these jar files
------------------------
commons-fileupload-1.2
commons-io-1.3.1
commons-lang-2.1
0 comments:
Post a Comment