|Home |Login |Registration |Struts Step by Step Tutorial |Hibernate Step by Step Tutorial |Spring Step by Step Tutorial |JSP Step by Step Tutorial |JDBC Step by Step Tutorial |Web Services Step by Step Tutorial |EJB fundamentals | ORACLE Step by Step Tutorial | SCJP 5.0 and SCJP 6.0 Study Guide | SCWCD 5.0 Study Guide | SCJP Tips
Java mock test | SCJP mock test | SCJP DUMP | SCBCD mock test |Java online test exam | JSP online test | ORACLE online test | Hibernate online test | Servlet online test | Struts online test | EJB online test | C online test | C++ online test | Aptitude online test

Java interview questions | JSP interview questions | ORACLE interview questions | Hibernate interview questions | Servlet interview questions | Struts interview questions | JDBC interview questions | C/C++ interview questions | Spring interview questions | JMS interview questions | Informatica interview questions | EJB interview questions | OOPS and Design Pattern interview questions
1600 PMP mock questions 1400 CAPM mock questions 800 SCJP 6 mock questions 600 OCAJP 7 mock questions 590 OCPJP 7 mock questions 556 SCWCD 5 mock questions 500 OCEJWCD 6 mock questions pdfDownload (java,struts, hibernet etc) JobsJobs and Walkins

 

Recent Questions

!!! JSP Frequently Asked Questions !!!

Q.How to upload an image from servlet/jsp into server from clients machine?

SCWCD 1.5 Exam Kit

!!!Answer!!!- From Technical Expert

Image upload from JSP to Data Base step by step :

step 1 :
In the JSP
<form name="regform2" method="post" enctype="multipart/form-data">
<input type="file" name="ImageFile" id="ImageFile" onChange="uploadImage()"/>
</form>

java script :
function uploadImage(){
document.regform.action ="<%=request.getContextPath()%>/dfdmin?cmd=uploadimage";
document.regform.submit();
}


Step 2.
In the servlet - add the below code.
This will upload your image to server (D:\\) or In unix you can mention /home/user like that

String rtempfile = File.createTempFile("temp","1").getParent();
MultipartRequest multi = new MultipartRequest(request, rtempfile, 500000 * 1024);
File rnewfile=null;
rnewfile = new File("D:\\"+"jsp"+File.separator+"images"+File.separator+"uploadImage"+File.separator);
if(rnewfile.exists()){
}else{
rnewfile.mkdirs();
}

File f = multi.getFile("ImageFile");
System.out.println(f.getName());
FileInputStream fin =new FileInputStream(f);
RandomAccessFile r = new RandomAccessFile(rnewfile+File.separator+f.getName(),"rw");
filename = f.getName();
// FileOutputStream fos =new FileOutputStream(rnewfile);
byte sizefile[] = new byte[5000000];
fin.read(sizefile);
// fos.write(sizefile);
r.write(sizefile);
//fos.close();
r.close();
fin.close();

DAO.upload(f); // Call to DAO for insert image into database.

Step 3.

Then call to DAO to save this image to data base.
Insert into Database
public void upload(File f){
InputStream is = new FileInputStream(f);
String sql = " INSERT INTO image_upload (IMAGE) VALUES (?) ";
pStmt = conn.prepareStatement(sql);
pStmt.setBinaryStream(1, is, (int)(f.length()));
pStmt.execute();
conn.commit();
}



This will upload multipart file to your data base.

Note : get cos.jar from oreilly website


OR
if your server have already image file then just do below step




Code is here :
File f = new File("d:\\test.jpg");
InputStream is = new FileInputStream(f);
String sql = " INSERT INTO image_upload (IMAGE) VALUES (?) ";
pStmt = conn.prepareStatement(sql);
pStmt.setBinaryStream(1, is, (int)(f.length()));
pStmt.execute();
conn.commit();

This will work.

Answered By : null Replied Date : Mar 24 2012
Answer :


Answered By : null Replied Date : Feb 22 2012
Answer :


Answered By : null Replied Date : Feb 5 2012
Answer :


Answered By : null Replied Date : Feb 4 2012
Answer :


Answered By : null Replied Date : Jan 24 2012
Answer :


Answered By : null Replied Date : Jan 20 2012
Answer :


Answered By : null Replied Date : Dec 25 2011
Answer :


Answered By : null Replied Date : Dec 24 2011
Answer :


Answered By : null Replied Date : Dec 10 2011
Answer :


Answered By : null Replied Date : Nov 29 2011
Answer :


Answered By : null Replied Date : Nov 16 2011
Answer :


Answered By : null Replied Date : Nov 4 2011
Answer :


Answered By : null Replied Date : Nov 3 2011
Answer :


Answered By : null Replied Date : Oct 13 2011
Answer :


Answered By : null Replied Date : Sep 24 2011
Answer :


Answered By : null Replied Date : Sep 22 2011
Answer :


Answered By : null Replied Date : Sep 19 2011
Answer :


Answered By : null Replied Date : Sep 4 2011
Answer :


Answered By : null Replied Date : Aug 29 2011
Answer :


Answered By : null Replied Date : Aug 15 2011
Answer :


Answered By : null Replied Date : Aug 6 2011
Answer :


Answered By : null Replied Date : Aug 5 2011
Answer :


Answered By : null Replied Date : Aug 4 2011
Answer :


Answered By : null Replied Date : Aug 2 2011
Answer :


Answered By : null Replied Date : Jul 30 2011
Answer :


Answered By : null Replied Date : Jul 26 2011
Answer :


Answered By : null Replied Date : Jul 22 2011
Answer :


Answered By : null Replied Date : Jul 19 2011
Answer :


Answered By : null Replied Date : Jul 16 2011
Answer :


Answered By : null Replied Date : Jul 14 2011
Answer :


Answered By : null Replied Date : Jul 14 2011
Answer :


Answered By : null Replied Date : Jul 12 2011
Answer :


Answered By : null Replied Date : Jul 11 2011
Answer :


Answered By : null Replied Date : Jul 10 2011
Answer :


Answered By : null Replied Date : Jul 9 2011
Answer :


Answered By : null Replied Date : Jul 8 2011
Answer :


Answered By : null Replied Date : Jul 7 2011
Answer :


Answered By : null Replied Date : Jul 6 2011
Answer :


Answered By : null Replied Date : Jul 5 2011
Answer :


Answered By : null Replied Date : Jul 3 2011
Answer :


Answered By : null Replied Date : Jul 3 2011
Answer :


Answered By : null Replied Date : Jun 22 2011
Answer :


Answered By : null Replied Date : Jun 22 2011
Answer :


Answered By : null Replied Date : Jun 16 2011
Answer :


Answered By : null Replied Date : Jun 15 2011
Answer :


Answered By : null Replied Date : Jun 14 2011
Answer :


Answered By : null Replied Date : Jun 3 2011
Answer :


Answered By : null Replied Date : Jun 2 2011
Answer :


Answered By : null Replied Date : May 10 2011
Answer :


Answered By : null Replied Date : Apr 20 2011
Answer :


Answered By : null Replied Date : Apr 18 2011
Answer :


Answered By : null Replied Date : Apr 16 2011
Answer :


Answered By : null Replied Date : Apr 14 2011
Answer :


Answered By : null Replied Date : Apr 12 2011
Answer :


Answered By : null Replied Date : Apr 10 2011
Answer :


Answered By : null Replied Date : Apr 10 2011
Answer :


Answered By : null Replied Date : Apr 9 2011
Answer :


Answered By : null Replied Date : Apr 7 2011
Answer :


Answered By : null Replied Date : Apr 6 2011
Answer :


Answered By : null Replied Date : Apr 6 2011
Answer :


Answered By : null Replied Date : Apr 4 2011
Answer :


Answered By : null Replied Date : Apr 2 2011
Answer :


Answered By : null Replied Date : Mar 31 2011
Answer :


Answered By : null Replied Date : Mar 30 2011
Answer :


Answered By : null Replied Date : Mar 23 2011
Answer :


Answered By : null Replied Date : Mar 14 2011
Answer :


Answered By : null Replied Date : Feb 22 2011
Answer :


Answered By : null Replied Date : Feb 21 2011
Answer :


Answered By : null Replied Date : Feb 16 2011
Answer :


Answered By : null Replied Date : Feb 16 2011
Answer :


Answered By : null Replied Date : Feb 15 2011
Answer :


Answered By : null Replied Date : Jan 29 2011
Answer :


Answered By : null Replied Date : Jan 27 2011
Answer :


Answered By : null Replied Date : Jan 25 2011
Answer :


Answered By : null Replied Date : Jan 25 2011
Answer :


Answered By : null Replied Date : Jan 24 2011
Answer :


Answered By : null Replied Date : Jan 22 2011
Answer :


Answered By : null Replied Date : Jan 22 2011
Answer :


Answered By : null Replied Date : Jan 21 2011
Answer :


Answered By : null Replied Date : Jan 21 2011
Answer :


Answered By : null Replied Date : Jan 20 2011
Answer :


Answered By : null Replied Date : Jan 16 2011
Answer :


Answered By : null Replied Date : Jan 15 2011
Answer :


Answered By : null Replied Date : Jan 12 2011
Answer :


Answered By : null Replied Date : Jan 4 2011
Answer :


Answered By : null Replied Date : Dec 31 2010
Answer :


Answered By : null Replied Date : Dec 30 2010
Answer :


Answered By : null Replied Date : Dec 27 2010
Answer :


Answered By : null Replied Date : Dec 27 2010
Answer :


Answered By : null Replied Date : Dec 26 2010
Answer :


Answered By : null Replied Date : Dec 15 2010
Answer :


Answered By : null Replied Date : Dec 15 2010
Answer :


Answered By : null Replied Date : Dec 13 2010
Answer :


Answered By : null Replied Date : Dec 13 2010
Answer :


Answered By : null Replied Date : Dec 10 2010
Answer :


Answered By : null Replied Date : Dec 10 2010
Answer :


Answered By : null Replied Date : Dec 6 2010
Answer :


Answered By : null Replied Date : Dec 4 2010
Answer :


Answered By : null Replied Date : Dec 3 2010
Answer :


Answered By : null Replied Date : Dec 3 2010
Answer :


Answered By : null Replied Date : Dec 3 2010
Answer :


Answered By : null Replied Date : Dec 3 2010
Answer :


Answered By : null Replied Date : Dec 3 2010
Answer :


Answered By : null Replied Date : Dec 3 2010
Answer :


Answered By : null Replied Date : Dec 1 2010
Answer :


Answered By : null Replied Date : Dec 1 2010
Answer :


Answered By : null Replied Date : Dec 1 2010
Answer :


Answered By : null Replied Date : Nov 30 2010
Answer :


Answered By : null Replied Date : Nov 30 2010
Answer :


Answered By : null Replied Date : Nov 29 2010
Answer :


Answered By : null Replied Date : Nov 28 2010
Answer :


Answered By : null Replied Date : Nov 28 2010
Answer :


Answered By : null Replied Date : Nov 28 2010
Answer :


Answered By : null Replied Date : Nov 25 2010
Answer :


Answered By : null Replied Date : Nov 23 2010
Answer :


Answered By : null Replied Date : Nov 22 2010
Answer :


Answered By : null Replied Date : Nov 21 2010
Answer :


Answered By : null Replied Date : Nov 16 2010
Answer :


Answered By : null Replied Date : Nov 15 2010
Answer :


Answered By : null Replied Date : Oct 29 2010
Answer :


Answered By : null Replied Date : Oct 27 2010
Answer :


Answered By : null Replied Date : Oct 15 2010
Answer :


Answered By : null Replied Date : Oct 10 2010
Answer :


Answered By : null Replied Date : Oct 8 2010
Answer :


Answered By : null Replied Date : Oct 4 2010
Answer :


Answered By : null Replied Date : Oct 3 2010
Answer :


Answered By : null Replied Date : Sep 23 2010
Answer :


Answered By : null Replied Date : Sep 17 2010
Answer :


Answered By : null Replied Date : Sep 14 2010
Answer :


Answered By : null Replied Date : Sep 14 2010
Answer :


Answered By : null Replied Date : Sep 10 2010
Answer :


Answered By : null Replied Date : Sep 8 2010
Answer :


Answered By : null Replied Date : Sep 8 2010
Answer :


Answered By : null Replied Date : Sep 6 2010
Answer :


Answered By : null Replied Date : Sep 5 2010
Answer :


Answered By : null Replied Date : Sep 4 2010
Answer :


Answered By : null Replied Date : Sep 3 2010
Answer :


Answered By : null Replied Date : Sep 3 2010
Answer :


Answered By : null Replied Date : Aug 31 2010
Answer :


Answered By : null Replied Date : Aug 22 2010
Answer :


Answered By : null Replied Date : Aug 14 2010
Answer :


Answered By : null Replied Date : May 5 2012
Answer :


Answered By : null Replied Date : May 6 2012
Answer :


Answered By : null Replied Date : Jun 22 2012
Answer :


Answered By : null Replied Date : Jun 29 2012
Answer :


Answered By : null Replied Date : Jun 30 2012
Answer :


Answered By : null Replied Date : Jul 1 2012
Answer :


Answered By : null Replied Date : Jul 21 2012
Answer :


Answered By : null Replied Date : Jul 24 2012
Answer :


Answered By : null Replied Date : Jul 30 2012
Answer :


Answered By : null Replied Date : Aug 4 2012
Answer :


Answered By : null Replied Date : Aug 26 2012
Answer :


Answered By : null Replied Date : Sep 7 2012
Answer :


Answered By : null Replied Date : Sep 8 2012
Answer :


Answered By : null Replied Date : Sep 10 2012
Answer :


Answered By : null Replied Date : Sep 10 2012
Answer :


Answered By : null Replied Date : Oct 10 2012
Answer :


Answered By : null Replied Date : Oct 25 2012
Answer :


Answered By : null Replied Date : Oct 27 2012
Answer :


Answered By : null Replied Date : Dec 14 2012
Answer :


Answered By : null Replied Date : Jan 2 2013
Answer :


Answered By : null Replied Date : Jan 9 2013
Answer :


Answered By : null Replied Date : Jan 14 2013
Answer :


Answered By : null Replied Date : Jan 15 2013
Answer :


Answered By : null Replied Date : Feb 23 2013
Answer :


Answered By : null Replied Date : Mar 2 2013
Answer :


Answered By : null Replied Date : Mar 5 2013
Answer :


Answered By : null Replied Date : Mar 20 2013
Answer :


Answered By : gfhgf Replied Date : Mar 29 2013
Answer : hjhgjhg


Answered By : null Replied Date : Apr 27 2013
Answer :


Answered By : null Replied Date : May 4 2013
Answer :


Answered By : null Replied Date : May 5 2013
Answer :


Answered By : null Replied Date : May 6 2013
Answer :


 

You can also contribute to this answer:

Your Name:
Answer:

 
Ask Question and get answer from Expert.
View Answers List from Expert.

The information you are posting should be related to java and ORACLE technology. Not political.