Download big files

I am trying to download a big file from the internet. Currently I am doing the following:
            URLConnection conn = downloadURL.openConnection();
            InputStream in = conn.getInputStream();
            OutputStream out = new FileOutputStream(dst);
            // Transfer bytes from in to out
            byte[] buf = new byte[SIZE];
            int len;
            while ((len = in.read(buf)) > 0 && !cancelled) {
                out.write(buf, 0, len);
            }That code works for me most of the time, but sometimes the file is not downloaded correctly and I do not know how to test if the download was completed or not, and I do not know how to guarantee that the file is downloaded completely.
Is there some way to make this?
Greetings,
Magus

That statement makes no sense. I'm programming in Java, and Java has a fine mechanism for throwing exceptions.No, that statement makes no sense. Your Java code is talking TCP/IP to a server, which doesn't have such a mechanism. It can close or reset the connection; that's it. If it had reset the connection, your read() would block forever, or timeout. If it had closed the connection, your read() would have returned -1, as it did, so that is what happened.
1. Can you expound upon what makes you think that is the reason that -1 was returned, as opposed to the connection timing out, the connection dropping, etc.?Because no exception was thrown. Or else one was thrown and you have swallowed it, but the code you posted doesn't indicate that.
2. Can you indicate why Java would return a -1 instead of throwing a some I/O exception?Because the server, or possibly an intermediate firewall, closed the connection, rather than Java incurring some exception at the client end such as a timeout. The documentation you quoted at me before bears that out. That's what the -1 means. You quoted that at me yourself.
The whole point here is that, according to the API documentation, -1 apparently indicates that the end of the stream was reached normally, and that error conditions are indicated by exception.Exactly so. So the server closed the connection normally, or an intermediate firewall did, but before it had sent all the data. Why is another question. Have a look at the server logs, or investigate the firewall configuration.
I guess I'm asking if anyone has seen this behavior, and has any insight on why it doesn't seem to follow the API.It does.
you seem to think that this behavior is in line with the API documentation; I disagree.Well you've quoted enough of it yourself: have another look, or another think. Premature (but normal) closing of the connection by the server or a firewall is the only possible explanation for what you're seeing. If it wasn't closed you wouldn't be getting the -1; if there was a timeout you would get a SocketTimeoutException; if there was any other exception you would catch that.
I've seen plenty of short downloads in my life, but the server doesn't have a way of indicating that via TCP/IP. You have to count.
NB some firewalls do close long-lived connections on their own account. Is there a client-side firewall that might be doing that.
Or else the transmitted content-length is wrong; for example, it overflows an integer.

Similar Messages

  • Abuout Download big file(Like *.avi)

    Hi,all:
    Sorry,my English is not good.
    I have a problem about jsp download big file,
    The file's real name is not same as it's save name.
    This is the JSP code:
    FileInputStream file = null;
    BufferedInputStream bis = null;
    ServletOutputStream aaa = null;
    BufferedOutputStream bos = null;
    try
    file=new FileInputStream(DLNAME);
    bis = new BufferedInputStream(file);
    response.setContentType("APPLICATION/OCTET-STREAM;charSet=Shift_JIS");
    response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
    aaa=response.getOutputStream();
    bos = new BufferedOutputStream (aaa);
    byte[] buff = new byte[2048];
    int bytesRead=0;
    while((bytesRead = bis.read(buff, 0, buff.length))!=-1)
    bos.write(buff, 0, bytesRead);
    file.close();     
    bis.close();
    aaa.close();
    bos.close();
    catch(IOException e){System.out.println(e.getMessage());}
    catch(Exception e){System.out.println("ddddd");}
    when the client download file,if he select save file and no cancel, the jsp can work well.
    but if he select turn of or cancel the process the web page is so slowly.
    pleas help me!!!
    my E-mail: [email protected]
    Thanks!!!

    Hello,
    Sorry!My English is not good.
    The server's OS is Windows2000,clients use Windows OS too.
    when clients execute my code ,the standard windows's download-dialog is appear,
    so I can't know what the clients want do.
    can you tell me how to get the clients' act?
    Thanks!

  • Sudhir Choudhrie - Can't Able to Download Big Files

    Hi, This is Sudhir Choudhrie, i've chosen Firefox for my daily browsing and downloading need because it has a function which lets you pause the running download and can start it whenever or next day you want. And you don't need any third-party trial download manager. But nowadays i finding it difficult to download big files. Whenever i choose to start a paused downloading file, i can't be able to download that file again. Please tell me if it's a website error or some error in Firefox. Thanks,

    Hi,
    Thank you for your question. This I do not know the answer, however I would be happy to test a download url. If this is an issue with downloads it is also possible to troubleshoot [https://support.mozilla.org/en-US/kb/cant-download-or-save-files]

  • Tips for downloading big files(World Of Warcraft)

    I need to download this 8 gigs of the game more. I'm downloading directly from bilzzard its going about 150 kb/s. Just wonder some tips yall do when downloading big files like this. Also if my macbook pro goes to sleep will it slow down the download because thats what seems to happen when I leave it on overnight.

    Do it in the evening through overnight. Set the MBP to not sleep. System Preferences>Energy Saver>Computer Sleep = Never.

  • Rt2860 wifi network hangs on downloading big files.

    After upgrade to 3.2 kernel now my rt2860 pci card is not working properly. Wifi connects fine, and i can browse the internet and download small files. But if i try to download a big file (> 1 gb) over the LAN, it starts and then hangs after downloading 8-10 mb. I have to disconnect the network, and connect again.
    I fixed it by installing rt2860 package from aur: https://aur.archlinux.org/packages.php?ID=14557 and blacklisting 2800pci.
    I would be happy with this solution, but now every time a kernel is updated i loose rt2860 after restart, and i have to manually recompile and instal the aur rt2860 package again.
    Are there any tweaks or config to fix the rt2800pci hanging problem ? or how can i make it not to loose aur package after every kernel upgrade ?

    I do not use rt2800pci on either Arch or Ubuntu. For me, it just doesn't work. You are doing better than I, because I can't get a connection at all with it.
    I put up with the effort of recompiling rt2860 after every kernel update, because it works. Besides, it helps me keep up my chops on compiling and installing kernel modules.
    Tim

  • Safari Incompletely Downloads Big Files

    In the next Safari update, the browser should be able to download 100% of files that are at least 1GB in size.

    Hello Michael,
    I meant to get support there to supplement support here: )
    Happy to help you.
    Since it works in the test/other account try,
    •To Reset Safari
    Under the Safari menu, choose Reset.
    Click the Reset button in the dialog box.
    • QUIT Safari & Sherlock, go to Home~/Library/Preferences / [list mode and remove the com.apple.Safari.plist & com.apple.Sherlock.plist file to the trash.
    Do a reboot.
    • Home~/Library/Cache folder /Safari cache, folder drag that to the trash
    • Home~/Library/Cookies/Cookies.plist file, to the trash.
    Do a reboot, run Disk Utility, Disk permissions repairTravis A.'s Regular Maintenance/General Troubleshooting if you need more on running Disk permission repair.
    Launch & run Safari any better?
    This freeware utility tool for maintenance if you do not use one already. OnyX,. make sure to download the correct version for your os. Use this article to guide you.Clearing caches with OnyX
    Apple knowledge base article Safari quits unexpectedlyfor more.
    Good luck! Please let us know how you fare.
    Eme'~[)

  • Impossible to download "big" file : error 3259

    hey everyone,
    I got an ipad for christmas with the ios 3.2.2
    I installed the last version of iTunes 10.1.1.4
    Itunes wants me to download and upgrade for ios 4.2... the system is strating to download but after a while the download stops and say error 3259 !!!
    I am fighting for 3 days trying to find a solution !!!
    - I have no antivirus running ( as it could be the problem )...
    - my computer is connected to my triple play box via ethernet ( as it could also be a problem if it was a wifi connexion )
    Then, I don't know...
    please help me... LOL

    no ideas ?????

  • Downloding big files on 3g/4g

    hello all
    first of all i like to say i mad at nokia for stoping 3g /4g big downloading over 20meg//
    i have a unlited internet plan sim deal
    but my phone stoping me to use
    ok here we go
    i like nokia to make it so we have a opshon for downloading over 3g big files /on/off system
    first    i understand 3g can be slower then wifi some times and unstabul but it belive it up to the user to use it or not/
    2ed   i understand some ppl use download and go over data so i sogest you set it by defrolt seting to the 20meg on but can be removed by seting.s menu
    now i explane y i a making this post today for the opshon for on/off downloading big files over 20meg not ust desable all the time
    today i went in my car and i got lost yes lost i went to use my phone to get me to a fomila place
    but as i was going to use nokia here maps it ask to download my regon map's i tryed just england on it own it was 200meg and it asked for wifi i had no way of geting wifi i was lost and nokia not alow me to try download on 3g even i got unlimited internet plan
    i belive this opeshon shud be up to the user to use or not
    even if it slow bad downloads crash or data limits / its up to the users// your makeing custermers hate nokia/windowsphones
    thanks keep the peace pzz add your coments
    Solved!
    Go to Solution.

    Nokia hasn't told you to go to the Windows webpage, I have suggested you do that. If you want an answer from Nokia you need to contact Nokia directly as this is a public forum.
    Microsoft control the software so it's them that you need to let know that you are unhappy, it really is that simple. I don't know what exactly can be said here to make you feel better?
    Nokia does pass on user feedback to Microsoft but what is the harm done if you also tell Microsoft yourself? Is it really that difficult to copy and paste your comments on to another site?
    There is nothing further that the users of this forum can say or do to help you on this.

  • How to download the big file in SDN

    Hi buddies,
         Do you have any idea how to download the big files in SDN such as NetWeaver CE trial? I cannot download it with download tool, and if I download it by IE, after dowloading for a while, I always get "connection reset" error.
         Hope you can help me.
    YiNing

    Yining,
    There seems to be a few people making comments about problems with downloads.  I myself have never had a problem, but I think that downloading from in Europ isn't a problem.
    I would try and use a tool such as GetRight download manager which picks up the fact that something is downloading and manages that download, so it will be able to restart the download.
    Paul

  • Hi, how do i share big files using the creative cloud , it says download the crative cloud connection, but then sends me nowhere that says that

    hi, how do i share big files using the creative cloud , it says download the crative cloud connection, but then sends me nowhere that says that

    Hi Jonty, check the file size limit:  Creative Cloud Desktop application FAQ
    You can also check the details around sharing and collaborating files: Sync and share files and folders with collaborators | Adobe Creative Cloud tutorials
    Atul_Saini

  • Problems uploading big files via FTP and downloading files

    I've been having problems uploading big files like video files (.mov 12MB) via FTP to my website (small files like .html or .doc I can upload but it takes longer than usual). I'm using Fetch 4.0.3. as FTP. Same problems when downloading files via Bit Torrent. I recently moved to Spain, since then I seem to have the problem. But my roommate with a PC doesn't have that problem. Connecting to internet with Ethernet cable also didn't resolve the problem. I also tested it from a Starbucks coffee connecting to Internet from there but still couldn't upload that 12MB file to the FTP. The security settings for firewall are set to "allow all incoming connections". I didn't change any of my settings so I don't know what the problems could be. I have a MacBook Pro, Mac OS X (10.5.7) Any suggestions? Thanks!

    Welcome to Apple Discussions!
    Much of what is available on Bittorrent is not legal, beta, or improperly labelled versions. If you want public domain software, see my FAQ*:
    http://www.macmaps.com/macosxnative.html#NATIVE
    for search engines of legitimate public domain sites.
    http://www.rbrowser.com/ has a light mode that supports binary without SSH security.
    http://rsug.itd.umich.edu/software/fugu/ has ssh secure FTP.
    Both I find are quick and a lot more reliable than Fetch. I know Fetch used to be the staple FTP program, but it grew too big for my use.
    - * Links to my pages may give me compensation.

  • Can't download big pdf files after upgrading to ios7.

    Safari closes itself every time I try to download PDF file bigger than about 50 MB. I never had any crashes with IOS6 with my iPad2 before. And yes, I've tried all cold boot and reset tricks.

    Report the problem to Apple at http://www.apple.com/feedback/ipad.html

  • Not enough space on my new SSD drive to import my data from time machine backup, how can I import my latest backup minus some big files?

    I just got a new 256GB SSD drive for my mac, I want to import my data from time machine backup, but its larger than 256GB since it used to be on my old optical drive. How can I import my latest backup keeping out some big files on the external drive?

    Hello Salemr,
    When you restore from a Time Machine back up, you can tell it to not transfer folders like Desktop, Documents. Downloads, Movies, Music, Pictures and Public. Take a look at the article below for the steps to restore from your back up.  
    Move your data to a new Mac
    http://support.apple.com/en-us/ht5872
    Regards,
    -Norm G. 

  • How to download a file under link in FF?

    there is a 'save target as...' popup menu option in "big" firefox, which I use to skip default action for specific files. I.e. to download a pdf file to read it later instead of open it in acroread right now.
    How can I get the same function in mobile version of FF?
    Thanks a lot.

    Since the last update I've got the same problem. When I click a download link (mp3), instead of downloading the file FF opens a new tab and start playing the mp3. This is not what I want. I want to download the file at home so that I can listen to it later when there is no internet connection available. When I long click the download link there are some options, but there is no option "save link as".
    Now I have to use Chrome to achieve my goal.
    Please help!

  • Downloading a file from Database [Blob field]

    I am trying to write an application which can upload and download files [Excel, Word etc.] to/from an Oracle 9i database Blob field. I am using Java/JSP for the same.
    The upload part works just fine. However, when I try to download the file that I uploaded, I get an error.
    A dialog box comes up asking me to Open/Save the file. However, when I try to save it, it says
    �Internet Explorer cannot download �..tion=download&planId= testplan from localhost
    Internet Explorer was not able to open this Internet Site. The requested site is either unavailable or cannot be found. Please try again later.�
    I am using IE 6.0. I tested the same with Firefox browser and was able to download the file.
    Can anyone help?
    Code:
    Following is the code I am using for the same.
    /* Code to retrieve from Blob field */
    String sqlString = "SELECT PLAN_DOCUMENT_NAME,PLAN_DOCUMENT FROM BRS_PLAN_DESCRIPTION WHERE PLAN_ID = ?";
    ps = con.prepareStatement(sqlString);
    ps.setString (1,planId);
    rs = ps.executeQuery();
    while (rs.next()) {
         fileBytes = rs.getBytes("PLAN_DOCUMENT");
         fileName = rs.getString("PLAN_DOCUMENT_NAME");
    brsPlanDocument.setPlanId(planId);
    brsPlanDocument.setFileName(fileName);
    brsPlanDocument.setFileBytes(fileBytes);
    /* Code for download */
    String fileName = brsPlanDocument.getFileName();
    String fileType = fileName.substring(fileName.indexOf(".")+1,fileName.length());
    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");
    byte[] fileBytes=brsPlanDocument.getFileBytes();
    ServletOutputStream outs = response.getOutputStream();
    outs.write(fileBytes);
    outs.flush();
    outs.close();

    Hi,
    is this problem solved for you, I am also writing the java code to store different files in blob fields in database(db2udb) and allow users to open them through jsp pages. Upload seems to be working fine.....My big problem is only excel files are opened properly in both IE and Firefox. Word files, image files are not getting opened. Any suggestion as what I could be doing wrong....my jsp is kind of similar to the above one...
    thanks in advace, please guide me
    long pmsId = new Long(request.getParameter("pmsId")).longValue();
    String fileName = request.getParameter("fileName");
    int fileSeq = new Integer(request.getParameter("fileSeq")).intValue();
    if(fileName.endsWith("txt")) {
    response.setContentType("text/plain");
    log.debug(" this is a text file");
    } else if(fileName.endsWith("xls")) {
    response.setContentType("application/vnd.ms-excel");
    log.debug(" this is a excel file");
    } else if(fileName.endsWith("gif")) {
    response.setContentType("image/gif");
    log.debug(" this is a image file");
    } else if(fileName.endsWith("doc")) {
    response.setContentType("application/msword");
    log.debug(" this is a doc file");
    } else if(fileName.endsWith("pdf")) {
    response.setContentType("application/pdf");
    log.debug(" this is a pdf file");
    } else if(fileName.endsWith("ppt")) {
    response.setContentType("application/ppt");
    log.debug(" this is a ppt file");
    } else {
    response.setContentType("application/everythingelse");     
    log.debug(" this is a unknown ile");
    response.setHeader("Content-Disposition", "attachment;filename="+fileName);
    OutputStream out1 = response.getOutputStream();
    Class.forName("com.ibm.db2.jcc.DB2Driver");
    db2Conn = DriverManager.getConnection("jdbc:db2:TESTDB","db2admin","db2fv1000");
    //log.debug("connection obtained");
    pstmt = db2Conn.prepareStatement(" SELECT * FROM UPLOADDOCS WHERE PMSID = ? AND DOCSEQ = ? ");
    //log.debug("statemenmt prepared");
    pstmt.setLong(1, pmsId);
    pstmt.setInt(2, fileSeq);
    rs = pstmt.executeQuery();
    int count = 0;
    if(rs.next()){
    InputStream is = rs.getBinaryStream("DOCUMENT"); //"is" is
    //now the binary data
    //of the file
    byte[] buf = new byte[4096];
    int len;
    while ((len = is.read(buf)) > 0)
         out1.write(buf, 0, len);
    out1.close();
    out1.flush();

Maybe you are looking for