Image uploading
Hi,can anyone explain the following code.
The code related to uploading an image file to oracle database using java servlet.
CODE
import java.io.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class AddPhotoServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
// Apache Commons-Fileupload library classes
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload sfu = new ServletFileUpload(factory);
if (! ServletFileUpload.isMultipartContent(request)) {
System.out.println("sorry. No file uploaded");
return;
}
// parse request
List items = sfu.parseRequest(request);
FileItem id = (FileItem) items.get(0);
String photoid = id.getString();
/* String photoid=request.getParameter("id");*/
FileItem title = (FileItem) items.get(1);
String phototitle = title.getString();
//String phototitle = request.getParameter("title"); */
// get uploaded file
FileItem file = (FileItem) items.get(2);
/*String s=request.getParameter("photo");
File file=new File(s);
FileInputStream fin = new FileInputStream(file); */
// Connect to Oracle
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "scott", "tiger");
con.setAutoCommit(false);
PreparedStatement ps = con.prepareStatement("insert into photos values(?,?,?)");
ps.setString(1, photoid);
ps.setString(2, phototitle);
// size must be converted to int otherwise it results in error
ps.setBinaryStream(3, file.getInputStream(), (int) file.getSize());
ps.executeUpdate();
con.commit();
con.close();
out.println("Proto Added Successfully. <p> <a href='listphotos'>List Photos </a>");
}
catch(Exception ex) {
out.println( "Error --> " + ex.getMessage());
}
}
}
In the above code I want to know functionality of DiskFileItemFactory, ServletFileUpload and FileItem classes.
please help me.Thank you.
View Answers
Ads
Related Tutorials/Questions & Answers:
Image uploading
Image uploading Hi,can anyone explain the following code.
The code related to
uploading an
image file to oracle database using java servlet.
CODE
import java.io.*;
import java.io.IOException;
import
Image uploading in FTP Server
Image uploading in FTP Server I want to upload images to a ftp server every 1 hour automatically using java..please help
Advertisements
change the name of the image after uploading the image
change the name of the
image after
uploading the image my form consists of a textbox and
uploading the image.after
uploading the
image i want to change the name of the
image with the content of the textbox before saving
uploading image in the form
uploading image in the form Hi All,
I am working to build a form like railway registration form which accepts user id and password and
image for authentication.
And i want all users should be able to see the
image once
Uploading Multiple Image On Server?
Uploading Multiple
Image On Server? Hello sir,
I am stuck with a problem of
uploading multiple images on server.
i have done a code which works fine for
uploading single
image,but it doesn't work with
uploading multiple
Uploading image using jsp
Uploading image using jsp how to upload
image using jsp. Already i tried, But that
image file does not read.
It returns only -1 without reading that
image file ...
I want know that solution using by u...
Thanks,
P.S.N.
 
struts image uploading
struts
image uploading please let me know how to upload
image in database using struts ..i have written form class and 'm hanging out in action class ....
form
FileUploadForm:
package com.action.form;
import
struts image uploading
struts
image uploading please let me know how to upload
image in database using struts ..i have written form class and 'm hanging out in action class ....
form
FileUploadForm:
package com.action.form;
import
struts image uploading
struts
image uploading please let me know how to upload
image in database using struts ..i have written form class and 'm hanging out in action class ....
form
FileUploadForm:
package com.action.form;
import
struts image uploading
struts
image uploading please let me know how to upload
image in database using struts ..i have written form class and 'm hanging out in action class ....
form
FileUploadForm:
package com.action.form;
import
Uploading an image into the table - JSP-Servlet
Uploading an
image into the table how to upload an
image... binary stream of given
image.
FileInputStream fis;
try {
// Load JDBC...");
// create a file object for
image by specifying full path of
image
Problem in uploading image to to mysql database
Problem in
uploading image to to mysql database Hi, need some help here, i have a program where a user can input name, city and upload
image. when a user click the submit button the name, city and the
image(Save as BLOB) must
Problem in uploading image to to mysql database
Problem in
uploading image to to mysql database Hi, need some help here, i have a program where a user can input name, city and upload
image. when a user click the submit button the name, city and the
image(Save as BLOB) must
save the uploading image with user specified name
save the
uploading image with user specified name My form consists of a textbox and an upload
image with submit button.When i submit the form I want to save the uploaded
image in a folder with user specfied name
save the uploading image with user specified name
save the
uploading image with user specified name My form consists of a textbox and an upload
image with submit button.When i submit the form I want to save the uploaded
image in a folder with user specfied name
uploading of image showing exception when size increases.
uploading of
image showing exception when size increases. Hi . I have a project in which the user can upload images. it works properly, but when the
image size increases to arnd 1 MB or more it shows exception. I am using tomcat
rename before uploading
rename before uploading I need to change (rename)the file name/
image name before
uploading it so that the file gets uploaded on server with this new name
Uploading files
Uploading files Hi,
Please provide html code for my question.
I need to insert the browsed files temporarily to text area while attaching more than one files during mailing
photo uploading
photo uploading i want the code upload the
image in oracle database
uploading a file
uploading a file When I am trying to upload a file to another system in lan at a location "http://192.168.12.5:8080/tomcat-docs/myapps",then it is giving the following error message "http://192.168.12.5:8080/tomcat-docs/myapps
uploading problem
uploading problem i use glassfish server..
using netbeans for jsp...
i wnat to upload a file to a folder 'doc' and insert corresponding
data... curresponding
image files start new
upload");
%><
image
image how to add the
image in servlet code
Uploading File on Server
Uploading File on Server Hello,
Can someone explain or suggest example. How do i
uploading files on the FTP Server.
Thanks
Image
Image how to insert
image in xsl without using xml. the
image was displayed in pdf..Please help me
Image
Image how to insert
image in xsl without using xml. the
image was displayed in pdf..Please help me
Image
Image how to insert
image in xsl without using xml. the
image was displayed in pdf..Please help me