Image Upload in jsp or servlet

Discussion in 'Java' started by Sidhartha, Nov 16, 2011.

  1. Sidhartha

    Sidhartha

    Joined:
    Nov 16, 2011
    Messages:
    1
    I want to upload image to server by using servlet ,I have tried this code but it is uploadin image


    <form action="abc.do" method="post" enctype="multipart/form-data">
    <table bgcolor="#857629">
    <tr>
    <td>Image</td>
    <td><input type="file" name="image" /></td>
    </tr>
    <tr>
    <td>
    <input type="submit" value="Upload"/>
    </td>
    </tr>
    </table>
    </form>










    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;
    import java.util.*;
    import java.io.*;
    import org.apache.commons.fileupload.*;
    import org.apache.commons.fileupload.servlet.*;
    import org.apache.commons.fileupload.disk.*;

    /**
    *
    * @author user
    */
    public class UploadPhoto extends HttpServlet {


    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    String itemName="";
    boolean fileUpload=false;
    try {

    boolean isMultipart=ServletFileUpload.isMultipartContent(request);

    FileItemFactory factory=new DiskFileItemFactory();
    ServletFileUpload upload=new ServletFileUpload(factory);
    upload.setSizeMax(100000);
    List items=null;
    items=upload.parseRequest(request);
    out.print("<script>alert('a');</script>");
    Iterator itr=items.iterator();
    while(itr.hasNext()){
    FileItem item=(FileItem)itr.next();
    String name=item.getFieldName();
    if(name.equals("image")){
    itemName=item.getName();
    File savedFile=new File(request.getRealPath("/")+"images\\"+itemName);
    item.write(savedFile);
    fileUpload=true;
    }
    }
    //* TODO output your page here
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Servlet UploadPhoto</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>Servlet UploadPhoto at " + request.getContextPath () + "</h1>");
    if(fileUpload == true){
    out.println("Image Uploaded");
    out.println("<table><tr><td width='210'></td><td> <img border=2 src=images/"+itemName.toString().trim()+" width=137 height=140></td></tr></table>");
    }else{
    out.println("Image Uploaded Fail.");
    }
    out.println("</body>");
    out.println("</html>");

    }catch(Exception m){
    m.printStackTrace();
    }
    finally {
    out.close();
    }
    }


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    processRequest(request, response);
    }

    /**
    * Handles the HTTP <code>POST</code> method.
    * @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 {
    processRequest(request, response);
    }

    /**
    * Returns a short description of the servlet.
    * @return a String containing servlet description
    */
    @Override
    public String getServletInfo() {
    return "Short description";
    }// </editor-fold>

    }

    can anyone help me to accomplish this task....
    Sidhartha, Nov 16, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. circuit_breaker
    Replies:
    2
    Views:
    1,993
    Jack Jia
    Apr 4, 2004
  2. KevinRobinson
    Replies:
    1
    Views:
    8,420
    Roedy Green
    Nov 15, 2005
  3. alexjaquet@gmail.com
    Replies:
    0
    Views:
    4,349
    alexjaquet@gmail.com
    Jun 2, 2006
  4. javadev
    Replies:
    5
    Views:
    12,894
    javadev
    Nov 16, 2006
  5. agapitolw@yahoo.es
    Replies:
    4
    Views:
    777
Loading...

Share This Page