[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

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,
1 Solution
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();

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?
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now