Sunday, April 3, 2011

download

import java.io.*;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
*
* @author saravana
*/
public class download extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String path= request.getParameter("path");
String filename = request.getParameter("file");
//PrintWriter out = response.getWriter();
System.out.println("inside download servlet");
ServletOutputStream myOut= response.getOutputStream();
try {
File f = new File(path+"\\"+filename);


/*if(f.exists())
// System.out.println(f.toString());
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet download</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet download at " + path + "</h1>");
out.println("<h1>Servlet download at " + filename + "</h1>");*/

response.setContentType("application/download");
response.setHeader("Content-Disposition", "attachment; filename= \""+filename+"\"");

FileInputStream fileIn = new FileInputStream(f);

byte[] outputByte = new byte[4096];
while(fileIn.read(outputByte, 0, 4096) != -1){
myOut.write(outputByte, 0, 4096);
}
}catch (IOException ioe){

throw new ServletException(ioe.getMessage( ));

}

finally {
if (myOut != null)
myOut.close( );
if (buf != null)
buf.close( );
}
}
}

resume upload

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException ;

import java.util.List;

import javax.servlet. RequestDispatcher;
import javax.servlet. ServletContext;
import javax.servlet. ServletException ;
import javax.servlet. ServletInputStream;
import javax.servlet. ServletOutputStream;
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.portlet.PortletFileUpload;
import org.apache.commons. fileupload.servlet.ServletFileUpload;
import org.apache.struts. upload.FormFile;

import com.oreilly. servlet.MultipartRequest;
import com.oreilly. servlet.multipart.MultipartParse r;
public class uploadFile extends HttpServlet {

/**
*
*/
private static final long serialVersionUID = 6807130476935151822 L;

public void service (HttpServletRequest req, HttpServletResponse res)
throws IOException , ServletException
{

System.out.println( " :in upload File :");


doPost(req,res) ;

}

public void doPost(HttpServletRequest req, HttpServletResponse res)
{
System.out.println( " :in do post upload File :");
String message="";
try{
MultipartRequest mr=new MultipartRequest( req,"c:\\ testUpload" );
// MultipartParser mp=new MultipartParser( );

if (ServletFileUpload. isMultipartConte nt(req)){

ServletFileUpload servletFileUpload = new ServletFileUpload( new
DiskFileItemFactory ());
List fileItemsList = servletFileUpload.parseRequest( req);
for(FileItem fi : fileItemsList)
{ System.out.println( "uploaded file name " + fi.getName() );
byte fileByteData []=fi.get();
ByteArrayOutputStre am baos = new ByteArrayOutputStre am();
System.out.println( "File Name : " +
fi.getName() .substring( fi.getName( ).lastIndexOf( "\\")+1)) ;
FileOutputStream fileOut=new FileOutputStream( "c:\\"+
fi.getName() .substring( fi.getName( ).lastIndexOf( "\\")+1)) ;
byte[] array = fi.get();
for (int cnt = 0; cnt e= req.getParameterNam es();
System.out.println( "Request Parameters : " );
try{

ServletInputStream is= req.getInputStream( );

Object o=req.getParameter( "file");

System.out.println( o.getClass( ).toString( ));



}catch(Exception ex)
{
System.out.println( "EX : " + ex.getMessage( ));


}
while(e.hasMoreElem ents())
{
System.out.println( e.nextElement( ));
}*/

}

}

Friday, April 1, 2011

upload Image in java

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("
");
out.println("");
out.println("
");
if(count1==1)
out.println("
"); if(count2==2) out.println(" "); if(count5==5) out.println(" "); if(count3==3) out.println(" "); if(count4==4) 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