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

file uploading to db using servlet

chinmaya mishra
Greenhorn

Joined: Jun 09, 2007
Posts: 7
I have a web application which will uoload the file into data base(oracle).

i have html file to browse file as "upload.html"

<form action="x">
File: <input type="file" name="upFile">

<input type="submit" name="submit" value="upload">
</form>


my servlet code is:


import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class InsertMessage extends HttpServlet{

public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{

response.setContentType("text/html");

PrintWriter out=response.getWriter();


//what Code to upload the file to database.

}
}




Can any body help me in this regard?

can somebody send me the servlet code for the same?




Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41524
    
  53
What do you mean by "upload to the database"? Do you wish to store the bytes that make up the file in a binary field in the database? If so, you can use an attribute of type BLOB.

Servlets can handle file uploads by way of the Apache Commons FileUpload library.


Ping & DNS - my free Android networking tools app
Gabriel Vince
Greenhorn

Joined: Feb 05, 2009
Posts: 24
chinmaya mishra wrote:I have a web application which will uoload the file into data base(oracle).

i have html file to browse file as "upload.html"


Can any body help me in this regard?

can somebody send me the servlet code for the same?



hi,

you have to use multipart formdata and parse input, see servlet file upload
udaykumar maddigatla
Ranch Hand

Joined: Feb 03, 2009
Posts: 69
First you have to write <from ENCTYPE='multipart/form-data' ...>
in your html form
Here is the full code

<html>
<form method="post" action="/uploadFile" enctype="multipart/form-data">
Name
<input type="text" name="uname"/>
File
<input type="file" name="upfile"/>
<input type="submit"/>
</form>

</html>



---------------------------My servlet---------------------------
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;


/**
* @author sm23772
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
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);}
}
}

SCJP 5.0(84%), SCWCD 5.0(97%), SCDJWS 5.0(98%)
chinmaya mishra
Greenhorn

Joined: Jun 09, 2007
Posts: 7
Thank you for your reply.

Do i need to add any jar file to my class path for the package "org.appache.commons.fileupload"

because when i compiled it i got error as that package/class can not be found.

if i need to add any jar file can you tell me that jar file name.

thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41524
    
  53
Googling for "Apache Commons FileUpload" will help you find that jar file fast.
chinmaya mishra
Greenhorn

Joined: Jun 09, 2007
Posts: 7
Ulf Dittmer wrote:What do you mean by "upload to the database"? Do you wish to store the bytes that make up the file in a binary field in the database? If so, you can use an attribute of type BLOB.

Servlets can handle file uploads by way of the Apache Commons FileUpload library.




I want to store the txt file in database so that the same file i can retrieve and see the content. i will store file in db as BLOB. can you send me the servlet code.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41524
    
  53
There's lots of example code at http://commons.apache.org/fileupload/using.html

If you're not proficient in JDBC, start reading at http://java.sun.com/docs/books/tutorial/jdbc/index.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: file uploading to db using servlet