aspose file tools*
The moose likes Servlets and the fly likes Upload Image to database through Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Upload Image to database through Servlet" Watch "Upload Image to database through Servlet" New topic
Author

Upload Image to database through Servlet

vijay kumar dahiya
Ranch Hand

Joined: Oct 19, 2008
Posts: 33
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.


Vijay
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

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 .


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

A 404 indicates that your servlet is not even being found. Check your URL and check your configuration.
vijay kumar dahiya
Ranch Hand

Joined: Oct 19, 2008
Posts: 33
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

Joined: May 15, 2009
Posts: 5
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
Marshal

Joined: Mar 22, 2005
Posts: 42928
    
  68
Samir,
the section "Processing the uploaded items" in http://commons.apache.org/fileupload/using.html explains this.
Samir Bukkawar
Greenhorn

Joined: May 15, 2009
Posts: 5
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

Joined: Apr 14, 2004
Posts: 10336


I want to do bar code generation project

What do you mean by this? Are you looking for recommendations for graphing APIs?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Samir Bukkawar
Greenhorn

Joined: May 15, 2009
Posts: 5
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

Joined: Apr 14, 2004
Posts: 10336

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
Marshal

Joined: Mar 22, 2005
Posts: 42928
    
  68
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

Joined: May 15, 2009
Posts: 5
Thank you all of you......!
Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

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..



Sorry, perhaps my english language isn't too good.. Prepare for SCJP 6, Please God help me.. ☼
References : [Java.Boot] [JavaChamp] [JavaPrepare]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Upload Image to database through Servlet