jsp, servlet and attachment

dear all, i am new to this technology

I am wondering how can i view a attachment that is stored in the Blog datatype in the mySql database.

I plan to put a link, in the jsp page, once it's clicked, it;s going to go to the db to retrieve the attachment that was stored as a binary type.

i am wondering how should i approach this?
please help,
Who is Participating?
First Try to read the BLOB object as bytes

eg., bytes [] data = abc.getBytes();

store these bytes in a file and then display them
Or if it is the text, try

String data = abc.getString();

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

poweranger77Author Commented:

i am doing this...

while (rs.next()){
  Blob attachment = rs.getBlob("ATTACHMENT");
  Blob abc  = (Blob) rs.getBinaryStream("ATTACHMENT");
  bytes [] data = rs.getBytes("ATTACHMENT");
  File abc_file = new File(abc.toString());

I am wondering how can i store the bytes into the file?

can show me the subsequent flow?
poweranger77Author Commented:
please share me some url , sample url..

poweranger77Author Commented:
yeah, that is a good reference.
      File temp = File.createTempFile("test", ".txt");

                  while (rs.next()){
                        Blob blob = rs.getBlob("ATTACHMENT");
                InputStream in = blob.getBinaryStream();
                int length = 0;
                byte[] bytes = new byte[1024];
                OutputStream out = new FileOutputStream(temp);
                int bytesRead = -1;
                while ((bytesRead = in.read(bytes)) > -1) {
                    out.write(bytes, 0, bytesRead);
                  System.out.println("File :" + temp.getAbsolutePath() );
                  System.out.println("Path :"+ temp.getPath() );
                  System.out.println("URL :"+temp.toURI());

But, i notice the file was created without having any extension type. Thus, i put the ".txt" at the back if it's txt file. And i think i have no choice but to put a "prefix" of the file in.... is that necessary? for the prefix to be filled ?

i put this,       File temp = File.createTempFile("", ".txt"); but then it's prompt me, prefix too short error :(
poweranger77Author Commented:
oh yeah, can i specify my own file name?

i can't find any method of this File are allow me to do so. then how can i set my own file name?
Manikandan ThiagarajanSenior consultantCommented:
Text.txt mention your file name like this

i didnt understand you r problem exactly
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.