Hello To Every One,
I make a application from where I upload the Image from local disk then store in DB.Before storing the Image in DB I want to check this program.
I include the commons-fileupload-1.2.1.jar file in lib folder.
and also set class path on command prompt.
classpath=C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar;.,
C:\Vijay\ImageProgram\WEB-INF\lib\commons-fileupload-1.2.1.jar;.;
I found Error 404 at run time.
Please take a view of that code if any error please assist me.
My files are:
UploadFile.java:
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadException;
public class UploadFile extends HttpServlet {
public void doPost(HttpServletRequest req,HttpServletResponse res)
{
try{
FileUpload fup=new FileUpload();
boolean isMultipart = FileUpload.isMultipartContent(req);
// Create a new file upload handler
System.out.println(isMultipart);
DiskFileUpload upload = new DiskFileUpload();
// Parse the request
List /* FileItem */ items = upload.parseRequest(req);
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
System.out.println("its a field");
} else {
System.out.println("its a file");
System.out.println(item.getName());
File cfile=new File(item.getName());
File tosave=new File(getServletContext().getRealPath("/"),cfile.getName());
item.write(tosave);
}
}
}catch(Exception e){System.out.println(e);}
}
}
Home.html:
<html>
<form method="post" action="/uploadFile" enctype="multipart/form-data">
File
<input type="file" name="upfile"/>
<input type="submit"/>
</form>
</html>
web.xml:
<web-app>
<
servlet>
<servlet-name>one</servlet-name>
<servlet-class>UploadFile</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>one</servlet-name>
<url-pattern>/uploadFile</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>Home.html</welcome-file>
</welcome-file-list>
</web-app>
Thanks.