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 8097015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:37:32+00:00 2026-06-05T21:37:32+00:00

I am working on a blackberry application in which I need to hit a

  • 0

I am working on a blackberry application in which I need to hit a url create a connection and write a file and save that to SDcard. Currently I am following this particular code. But while creating FileOutputStream object it throws CLassCastException. I am struggling with this.

public void run() {
            HttpConnection httpConnection = null;
            DataOutputStream httpDataOutput = null;
            InputStream httpInput = null;
            OutputStream  fos=null;
            int rc;
            try {
                httpConnection = new HttpConnectionFactory()
                .getHttpConnection("http://faultcode.techvalens.net/PDF/DrawingSample.PDF");
                rc = httpConnection.getResponseCode();
                if (rc != HttpConnection.HTTP_OK) {
                    throw new IOException("HTTP response code: " + rc);
                }
                httpInput = httpConnection.openInputStream();
                InputStream is = httpInput;

                FileConnection fconn=(FileConnection)Connector.open("file:///SDCard/Test.txt",
                        Connector.READ_WRITE);
                if(!fconn.exists())
                    fconn.create();
                System.out.println(fconn.exists());

                fos = new FileOutputStream( File.FILESYSTEM_PATRIOT, "Test.txt" );
            //  byte[] b = IOUtilities.streamToBytes(inp);

                byte[] buffer = new byte[702];
                    int len1 = 0;
                    while ((len1 = is.read(buffer)) != -1) {
                        fos.write(buffer, 0, len1);
                    }

            } catch (Exception ex) {
                System.out.println("URL Error........" + ex.getMessage());
            } finally {
                try {
                    if (httpInput != null)
                        httpInput.close();
                    if (httpDataOutput != null)
                        httpDataOutput.close();
                    if (httpConnection != null)
                        httpConnection.close();
                } catch (Exception e) {
                    e.printStackTrace();

                }
            }
        }

the above code i am using.
Please let me know what is my mistake.
THanx in advance…!!!

  • 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-06-05T21:37:34+00:00Added an answer on June 5, 2026 at 9:37 pm

    The task was basic but created an unforgettable concept for me. Below is the code now i am using, its the best if i need to download any type file from URL.

    public void run(){
            GetURLWebService _webService = new GetURLWebService(methodName,elecID,pdfType,performedBy,notes);
            String pdfURL = _webService.getWebServiceData();
            _pdfURL = pdfURL;
    
            System.out.println("Download the pdf...!!!");
            try {
                int chunkIndex = 0;
                int totalSize = 0;
                /*
                 * File connection
                 */
                FileConnection file =(FileConnection)Connector.open(localFile);
                if (!file.exists()) {
                    file.create();
                }
                file.setWritable(true);
                OutputStream out = file.openOutputStream();
                /*
                 * HTTP Connections
                 */
                String currentFile = _pdfURL + connectionType();
                //log("Full URL: " + currentFile);
                HttpConnection conn;
                InputStream in;
                int rangeStart = 0;
                int rangeEnd = 0;
                while (true) {
                    //  log("Opening Chunk: " + chunkIndex);
                    conn = (HttpConnection) Connector.open(currentFile, 
                            Connector.READ_WRITE, true);
                    rangeStart = chunkIndex * chunksize;
                    rangeEnd = rangeStart + chunksize - 1;
                    // log("Requesting Range: " + rangeStart + 
                    //      "-" + rangeEnd);
                    conn.setRequestProperty("Range", "bytes=" +
                            rangeStart + "-" + rangeEnd);
                    int responseCode = conn.getResponseCode();
                    if (responseCode != 200 && responseCode != 206)
                    {
                        // log("Response Code = " + conn.getResponseCode());
                        break;
                    }
                    // log("Retreived Range: " + conn.getHeaderField("Content-Range"));
                    in = conn.openInputStream();
                    int length = -1;
                    byte[] readBlock = new byte[256];
                    int fileSize = 0;
                    while ((length = in.read(readBlock)) != -1) {
                        out.write(readBlock, 0, length);
                        fileSize += length;
                        Thread.yield(); // Try not to get cut off
                    }
                    totalSize += fileSize;
                    // log("Chunk Downloaded: " + fileSize + " Bytes");
                    chunkIndex++; // index (range) increase
                    in.close();
                    conn.close();
                    in = null;
                    conn = null;
                    /*
                     * Pause to allow connections to close and other Threads
                     * to run.
                     */
                    Thread.sleep(1000);
                }
                // log("Full file downloaded: " + totalSize + " Bytes");
                out.close();
                file.close();
                // log("Wrote file to local storage");
            } catch (Exception e) {
                System.out.println(e.toString());
            }
        }
        private String connectionType() {
            switch (conn) {
            case 1:
                return ";deviceside=false";
            case 2:
                return ";deviceside=true;interface=wifi";
            default:
                return ";deviceside=true";
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently i am working on Blackberry Application and stuck at this point that how
I am working on an application for blackberry that will be downloading a file.
I have been working on a BlackBerry application that consumes web services from ColdFusion
I'm working on a Blackberry application (JDE 4.6.1) on a Windows system. I need
I have a working application which gets the GPS Long/Lat of from a BlackBerry
I am designing a Netflix Application for BlackBerry mobile devices. I am currently working
I am working on a UI based application in which I need to show
I am working on a blackberry application which includes radio button controls. HorizontalFieldManager hr
I am working on a BlackBerry Application which requires the application be running once
I'm working on a blackberry application which requires to pass a parameter to another

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.