dcsimg
How to get filename of a BLOB before sending to client browser??
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   j_gray
Posted On:   Wednesday, March 6, 2002 07:50 AM

I have a servlet that retrieves binary data and serves it to the client's browser w/ServletOutputStream. The code works fine, but I have to hardcode the filename of the BLOB file in my java code. I want to be able to get a handle on the file when I pull it out of db2 and then access the file's metadata & retrieve the file name from the metadata. That way, I won't have to hardcode file names in my servlet. Can someone give me some pointers on how this may be accomplished. Thanks in advance! Code below... import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; import java.lang.*; import java.util.*; public   More>>

I have a servlet that retrieves binary data and serves it to the client's browser w/ServletOutputStream. The code works fine, but I have to hardcode the filename of the BLOB file in my java code. I want to be able to get a handle on the file when I pull it out of db2 and then access the file's metadata & retrieve the file name from the metadata. That way, I won't have to hardcode file names in my servlet. Can someone give me some pointers on how this may be accomplished. Thanks in advance! Code below...





import java.io.*;

import java.sql.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.lang.*;

import java.util.*;




public class MRblob extends HttpServlet { Connection con; public void init(ServletConfig config) throws ServletException { super.init(config); try {



Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");

con = DriverManager.getConnection("jdbc:db2:domtest", "db2admin", "startnow");
}
catch (ClassNotFoundException e) { //throw new UnavailableException(this, "Couldn't load JdbcOdbcDriver");
}
catch (SQLException e) { //throw new UnavailableException(this, "Couldn't get db connection");
} }

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {



try {


ServletOutputStream out = res.getOutputStream();


Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT BLOBBY FROM PRODTEST WHERE KEYVAL='gogogogogogo'");
res.setContentType("application/download");

res.setHeader("Content-Disposition", "inline; filename=amovie.exe");


if (rs.next()) {

BufferedInputStream theData = new BufferedInputStream(rs.getBinaryStream("BLOBBY"));

byte[] buf = new byte[4 * 1024]; // 4K buffer


int len;

while ((len = theData.read(buf, 0, buf.length)) != -1) {

out.write(buf, 0, len);
} }

else

{ res.sendError(res.SC_NOT_FOUND);
} }
catch(SQLException e) { // Report it } } }

   <<Less
About | Sitemap | Contact