Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7086003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:29:43+00:00 2026-05-28T07:29:43+00:00

I have written a code for downloading a file(blob in database table).I get a

  • 0

I have written a code for downloading a file(blob in database table).I get a prompt while downloading but when i download it’s size increases and opens as a blank file or corrupted file.The controller calling 2 service methods downloadfile():- to get the blob type into a byte array.

downloadfilename() :- to get the filename stored in the database.
Code for Contoller

  protected ModelAndView handleRequestInternal(HttpServletRequest request,
                HttpServletResponse response) throws Exception {
            byte[] fileBytes = userService.downloadFile();
            String filename = userService.downloadFileName();

            String fileType = filename.substring(filename.indexOf(".")+1,filename.length());
            System.out.println("FILETYPE IS :>>>>>>>>>>>>>>>"+fileType);

            if (fileType.trim().equalsIgnoreCase("txt"))
            {
            response.setContentType( "text/plain" );
            }
            else if (fileType.trim().equalsIgnoreCase("doc"))
            {
            response.setContentType( "application/msword" );
            }
            else if (fileType.trim().equalsIgnoreCase("xls"))
            {
            response.setContentType( "application/vnd.ms-excel" );
            }
            else if (fileType.trim().equalsIgnoreCase("pdf"))
            {
            response.setContentType( "application/pdf" );
            }
            else if (fileType.trim().equalsIgnoreCase("ppt"))
            {
            response.setContentType( "application/ppt" );
            }
            else
            {
            response.setContentType( "application/octet-stream" );
            }

            response.setHeader("Content-Disposition","attachment; filename=\""+filename+"\"");
            response.setHeader("cache-control", "no-cache");
            response.setHeader("cache-control", "must-revalidate");

            ServletOutputStream outs = response.getOutputStream();
            outs.write(fileBytes);
            outs.flush();
            outs.close();
            return null;

        }

DAO CLASS goes like this..

public String downloadFile(){
String selectquery = "select * from upload_file where id=1";
                System.out.println("inside after query");
                ResultSet rs = stmt.executeQuery(selectquery);
                while (rs.next()) {
                    fileBytes = rs.getBytes("description");
                    fileName = rs.getString("upload_filename");
                    System.out.println("**************************************"
                            + fileName);    
                }
                return fileName;
            } catch (SQLException s) {
                System.out.println(s);
            }
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:29:44+00:00Added an answer on May 28, 2026 at 7:29 am

    You question is not really clear, but I think I know what you mean.

    If a file is large (10MB) then you must specify the content length in the http header. If not the browsers will behave stange. (The exact limit 10MB is browser depended)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written jQuery code, in files Main.html and ajax.php . The ajax.php file
I have written code to perform click event on JMenuItem but that is not
I have written code in Java to merge two xml file, the first file
I have written code in -(void)applicationWillEnterForeground:(UIApplication *)application , but my application is not detecting
I have written code below.but it will print this exception and i really don't
I have written code ORM::factory('cds')->find_all(1, 2); It is returning all row . But according
I have written code for many patterns but unable to write for this..... not
I have written code for passing file descriptors between unrelated processes using streams. The
I have written code for javascript but it is not called any how. I
I have written code to insert an Image in SQL server but it throws

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.