Upload and Retrieve image using JSP.
Hello
friends, I am write this blog for those programmer are required a code
for “how to upload and retrieve images (photos) from server using JSP.”
There is two jsp files are used in this project, and this project following libraries are required.
commons-fileupload.jar
commons-fileupload.jar
commons-fileupload-1.2.1.jar
commons-io-1.4
this project done using NetBeans IDE 6.9.1 IDE. and jdk1.6.0_23
Code:
upload_file_multipale.jsp
<%@ page contentType="text/html; charset=iso-8859-1" language="java" errorPage="" %>
<%@ page import="java.util.List"%>
<%@ page import="java.util.Iterator"%>
<%@ page import="java.io.File"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@ page import="org.apache.commons.io.FilenameUtils"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="java.lang.Exception"%>
Your files are uploading.....
<%
String itemName="";
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart){
out.println("The Form is not Multipart!!!!!");
}
else
{
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
out.println(e.toString());
}
Iterator itr = items.iterator();
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
if (item.isFormField()){
String name = item.getFieldName();
String value = item.getString();
}
else {
try {
itemName = item.getName();
itemName = FilenameUtils.getName(itemName);
//out.println(itemName);
File savedFile = new File(config.getServletContext().getRealPath("/")+"uploadedFiles/"+itemName);
item.write(savedFile);
session.setAttribute("FileName",itemName);
} catch (Exception e) {
out.println(e.toString());
}
}
}
}
response.sendRedirect("/FileUpload/upload_file_multipale_html.jsp");
%>
upload_file_multipale_html.jsp
<%@page import="java.io.File"%>
Upload and Retrieve Image (Photo)by using apache.commons.fileupload in JSP
Specify file:
By RAVICHANDRA
Email: kotharavichandra555@gmail.com
My blog: kotharavichandra.blogspot.com
<%
String FileName = (String)session.getAttribute("FileName");
File savedFile = new File(config.getServletContext().getContextPath() +"/uploadedFiles/"+FileName );
%>
Upload and Retrieve image using JSP.
Hello
friends, I am write this blog for those programmer are required a code
for “how to upload and retrieve images (photos) from server using JSP.”
There is two jsp files are used in this project, and this project following libraries are required.
commons-fileupload.jar
commons-fileupload.jar
commons-fileupload-1.2.1.jar
commons-io-1.4
Code:
upload_file_multipale.jsp
<%@ page contentType="text/html; charset=iso-8859-1" language="java" errorPage="" %>
<%@ page import="java.util.List"%>
<%@ page import="java.util.Iterator"%>
<%@ page import="java.io.File"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@ page import="org.apache.commons.io.FilenameUtils"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="java.lang.Exception"%>
Your files are uploading.....
<%
String itemName="";
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart){
out.println("The Form is not Multipart!!!!!");
}
else
{
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
out.println(e.toString());
}
Iterator itr = items.iterator();
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
if (item.isFormField()){
String name = item.getFieldName();
String value = item.getString();
}
else {
try {
itemName = item.getName();
itemName = FilenameUtils.getName(itemName);
//out.println(itemName);
File savedFile = new File(config.getServletContext().getRealPath("/")+"uploadedFiles/"+itemName);
item.write(savedFile);
session.setAttribute("FileName",itemName);
} catch (Exception e) {
out.println(e.toString());
}
}
}
}
response.sendRedirect("/FileUpload/upload_file_multipale_html.jsp");
%>
upload_file_multipale_html.jsp
<%@page import="java.io.File"%>
Upload and Retrieve Image (Photo)by using apache.commons.fileupload in JSP
Specify file:
By RAVICHANDRA
Email: kotharavichandra555@gmail.com
My blog: kotharavichandra.blogspot.com
<%
String FileName = (String)session.getAttribute("FileName");
File savedFile = new File(config.getServletContext().getContextPath() +"/uploadedFiles/"+FileName );
%>
All the best Guys...
Hope you find the answer for your question...!!!
0 comments:
Post a Comment