How to store and retriew .pdf or .doc using file uploader component

hi,I want to store a .pdf / .doc file in the database and retiew it. I am using sun studio enterprise for EJB developing(sun application server as the container) , Oracel as DBMS and sun studio creator for frontend.kaushalya
[252 byte] By [kaushalya.cse] at [2007-11-26 11:17:39]
# 1

public String saveButton_action ()

{

// TODO: Process the button click action. Return value is a navigation

// case name where null will return to the same page.

tagsDataProvider1.cursorLast ();

//UploadedFile f = fileUpload1.getUploadedFile ();

if (fileUpload1.getUploadedFile ()!=null)

{

tagsDataProvider1.setValue ("attachment",fileUpload1.getUploadedFile().getBytes ());

tagsDataProvider1.setValue ("attachmentfilename",fileUpload1.getUploadedFile().getOriginalName ());

tagsDataProvider1.setValue ("attachmentcontenttype",fileUpload1.getUploadedFile().getContentType ());

}

tagsDataProvider1.commitChanges ();

return "home";

}

This servlet returns the data to the user unadulterated:

/*

* FileView.java

*

* Created on October 6, 2005, 4:50 PM

*

* To change this template, choose Tools | Options and locate the template under

* the Source Creation and Management node. Right-click the template and choose

* Open. You can then make changes to the template in the Source Editor.

*/

package docman;

/**

*

* @author USER

*/

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.Statement;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import javax.servlet.ServletConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.sql.DataSource;

public class FileView extends HttpServlet {

String ct;

String type;

/** Creates a new instance of DisplayPicture */

public FileView() {

}

public void init(ServletConfig config) throws ServletException {

super.init(config);

}

public void destroy() {

}

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

String id=request.getParameter("id");

type=request.getParameter("type");

//String ct=request.getParameter("contenttype");

//if ((ct==null)||(ct.equals(""))) {

//ct="image/x-jpeg";

//ct="image/bmp";

//}

System.out.println("FileView 1.0");

System.out.println("requested file with ID: "+id+" content: "+ct+" Doctype:"+type);

try {

ServletOutputStream out = response.getOutputStream();

byte[] f=this.getImage(id);

response.setContentType(ct);

out.write(f);

} catch (Exception e) {

System.out.println(e.getMessage());

e.printStackTrace();

}

}

/** Handles the HTTP <code>GET</code> method.

* @param request servlet request

* @param response servlet response

*/

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

*/

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);

}

/** Returns a short description of the servlet.

*/

public String getServletInfo() {

return "Displays a picture from the database identified by a parameter IMAGEID";

}

private byte[] getImage(String id) throws IOException {

Statement sta=null;

Connection con=null;

ResultSet rs=null;

byte[] result=null;

try {

Context initContext = new InitialContext();

Context envCtx = (Context) initContext.lookup("java:comp/env");

DataSource ds = null;

try{

ds = (DataSource)envCtx.lookup("jdbc/xxxxx");

}catch ( javax.naming.NameNotFoundException ex){

log("No context found for jdbc/xxxxx in java:comp/env Attempting to look it up in initial context");

ds = (DataSource)initContext.lookup("jdbc/xxxxx");

}

Connection conn = ds.getConnection();

sta = conn.createStatement();

if(type==null){rs=sta.executeQuery("SELECT * FROM tags where tagid="+id);}

else if (type.toLowerCase ().equals("tag")){rs=sta.executeQuery("SELECT * FROM tags where tagid="+id);}

else if (type.toLowerCase ().equals("crewdoc")){rs=sta.executeQuery("SELECT * FROM crewdocumentation where docid="+id);}

if (rs.next()) {

result=rs.getBytes("attachment");

ct=rs.getString ("attachmentcontenttype");

System.out.println("bytes returned : "+result.length);

} else {

System.out.println("Could find image with the ID specified or there is a problem with the database connection");

}

rs.close();

sta.close();

conn.close();

} catch (Exception e) {

log(e.getMessage());

e.printStackTrace();

}

// ByteArrayOutputStream output = new ByteArrayOutputStream();

//output.write(result, 78, result.length-78);

//output.flush();

//output.close();

//return output.toByteArray();

return result;

}

}

I pinched both from examples i found on this forum using the search facility...

And this hyperlink retreives the file:

<ui:hyperlink binding="#{TagView.tagHyperlink}" id="tagHyperlink"

text="#{TagView.tagsDataProvider.value['tags.attachmentfilename']}" url="/servlet/FileView/?id=%23%7BTagView.tagsDataProvider.value%5B'tags.tagid'%5D%7D+&quot;&amp;&quot;+#{TagView.tagsDataProvider.value['tags.attachmentfilename']}"/>

[code]

yossarian at 2007-7-7 3:32:49 > top of Java-index,Development Tools,Java Tools...