jsp, servlet and attachment

Posted on 2006-05-24
Last Modified: 2010-04-01
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,
Question by:poweranger77
    LVL 3

    Expert Comment

    First Try to read the BLOB object as bytes

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

    store these bytes in a file and then display them
    LVL 3

    Expert Comment

    Or if it is the text, try

    String data = abc.getString();


    Author Comment


    i am doing this...

    while ({
      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?

    Author Comment

    please share me some url , sample url..

    LVL 8

    Accepted Solution


    Author Comment

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

                      while ({
                            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 = > -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 :(

    Author Comment

    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?
    LVL 8

    Expert Comment

    Text.txt mention your file name like this

    i didnt understand you r problem exactly

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    If you're not part of the solution, you're part of the problem.   Tips on how to secure IoT devices, even the dumbest ones, so they can't be used as part of a DDoS botnet.  Use PRTG Network Monitor as one of the building blocks, to detect unusual…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now