Image Upload in jsp or servlet

Joined
Nov 16, 2011
Messages
1
Reaction score
0
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....
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top