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)

Servlet Tutorials

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

In the JSP

<form name="regform2" method="post" enctype="multipart/form-data">

<input type="file" name="ImageFile" id="ImageFile" onChange="uploadImage()"/>

</form>





uploadImage() - java script - document.regform.action ="<%=request.getContextPath()%>/dfdmin?cmd=uploadimage";

document.regform.submit();



In the servlet - add the below code.



String rtempfile = File.createTempFile("temp","1").getParent();

MultipartRequest multi = new MultipartRequest(request, rtempfile, 500000 * 1024);

File rnewfile=null;

rnewfile = new File(CommonArt.IMAGE_PATH+"jsp"+File.separator+"images"+File.separator+"uploadImage"+File.separator);





if(rnewfile.exists()){



}else{



rnewfile.mkdirs();

}

//Enumeration files = multi.getFileNames();

//while (files.hasMoreElements()) {

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();



This will upload multipart file to your path.



Note : get cos.jar from oreilly website.

Reviews and Comments


PMP, CAPM, PMI is a registered certification mark of the Project Management Institute, Inc

Copyright © www.techfaq360.com 2016


About US | Contact US | Privacy Policy | Terms and Conditions  | Website disclaimer  | Cancellation and Refund Policy  | Shipping & Delivery Policy