Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Upload Image to database through Servlet

 
vijay kumar dahiya
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be sure to use UBB code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the .
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A 404 indicates that your servlet is not even being found. Check your URL and check your configuration.
 
vijay kumar dahiya
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Sir,
Allthough Error 404 has been solved.
But It show that error.
However I set the classpath=%classpath%;C:\Vijay\webservices\Upload\WEB-INF\lib\commons-fileupload-1.2.1.jar;
Error 500--Internal Server Error
javax.servlet.ServletException: [HTTP:101249][ServletContext(id=29091418,name=Upload,context-path=/Upload)]: Servlet class UploadFile for servlet one could not be loaded because the requested class was not found in the classpath C:\Vijay\webservices\Upload;C:\Vijay\webservices\Upload\WEB-INF\classes;C:\bea\user_projects\vijay\.\myserver\.wlnotdelete\extract\myserver_Upload_Upload\jarfiles\WEB-INF\lib\commons-fileupload-1.2.164351.jar.
java.lang.UnsupportedClassVersionError: UploadFile (Unsupported major.minor version 50.0).
at weblogic.servlet.internal.ServletStubImpl.prepareServlet(ServletStubImpl.java:794)
at weblogic.servlet.internal.ServletStubImpl.getServlet(ServletStubImpl.java:504)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:349)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:305)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:6291)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:317)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:97)
at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3575)
at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2573)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:178)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:151)

Please Help me.
 
Samir Bukkawar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I have uploaded Image using these API's successfully.
But I want to upload Image as well as some text data in the form.
As we mentioned "enctype="multipart/form-data" in the form tag of our JSp/Html page
I am not able to use request.getParameter();

How can I proceed?

Please any one help me..?

My first JSP page is this

<%@ page language="java" errorPage="" %>
<html>
<head>
<title>Insert Image into database</title>
</head>

<body>
<form name="form1" action="saveImage.jsp" enctype="multipart/form-data" method="post">
<input type="file" name="imagePath" /> <br>
<input type ="text" name = "name"/> <br>
<input type="submit" name="Submit" value="Submit"/>
</form>
</body>
</html>


please any one tell me How can I retrieve the Image as well as name....?
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Samir,
the section "Processing the uploaded items" in http://commons.apache.org/fileupload/using.html explains this.
 
Samir Bukkawar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ...!

Its really working.

Thanks very much.

I want to do bar code generation project.

please suggest me ho van I start..?

any releted links..?

Thanks,

Samir Bukkawar

 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I want to do bar code generation project

What do you mean by this? Are you looking for recommendations for graphing APIs?
 
Samir Bukkawar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks 'Paul Sturrock’ for replying and showing intrest.

No I want to make just one servlate class which takes a digit at run time in its request.
then, I want to convert that digit in to the barcode.
After converting this, the barcode should save in pdf format.
I am using Apache Tomcat 5.5

Thanks,
Samir
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
iText is a library that lets you generate PDFs. Can't really help you with the barcode generation stuff, but five seconds of Googling came up with this.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See http://faq.javaranch.com/java/OtherOpenSourceProjectsFaq#barcode for several libraries that can create barcodes. But let's not derail this topic any further by talking about barcodes; start a new topic if you have further questions on that.
 
Samir Bukkawar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all of you......!
 
Leonardo Carreira
Ranch Hand
Posts: 489
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends..

i was follow tutorial in this post..
Yap it could ran well.. the file would be copied to Server's directory...

iam trying to save the file to database by using Hibernate..
this is my Mapping Hibernate :


and this is my action for save the data :


but in the table i could see anything in that column...
and how to get that data back again?..
in my case.. i save a .PDF file..
is it possible to save another format like .CSV/.XSL/.DOC/.RAR/.ZIP?

Sorry for the basic question...

Thanks in advance..


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic