Jump to content

Trying to read file from web

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
ClemsonCS

ClemsonCS

    Newbie

  • Members
  • PipPip
  • 28 posts
I have this code to download files from the internet. For some reason, it gets "stuck". I put a print in the while loop and after the while loop and on certain files, it stops printing inside the loop and never prints once the loop has terminated. Any ideas? Am I using the correct objects to handle this?

BufferedInputStream in = new BufferedInputStream(url.openStream());

byte[] buff = new byte[4096];

BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));

while(in.read(buff,0,4096) != -1){

    out.write(buff, 0, 4096);

}

System.out.println("end of loop");//this doesnt print



#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Has the server stopped sending data AND not indicated the file is done?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
ClemsonCS

ClemsonCS

    Newbie

  • Members
  • PipPip
  • 28 posts
I don't know, sounds like a possibility. How can I handle that? its not throwing any exceptions.

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I'm not sure. Maybe have a timeout?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

ClemsonCS said:

I have this code to download files from the internet. For some reason, it gets "stuck". I put a print in the while loop and after the while loop and on certain files, it stops printing inside the loop and never prints once the loop has terminated. Any ideas? Am I using the correct objects to handle this?

BufferedInputStream in = new BufferedInputStream(url.openStream());

byte[] buff = new byte[4096];

BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));

while(in.read(buff,0,4096) != -1){

    out.write(buff, 0, 4096);

}

System.out.println("end of loop");//this doesnt print


I would really like to know, what is url and file in your code. Or else I can't read in what might not make the printer to print out the message...
Posted Image

#6
ClemsonCS

ClemsonCS

    Newbie

  • Members
  • PipPip
  • 28 posts
I am having trouble getting this error to replicate itself today. Not sure why, haven't changed the code.

#7
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

ClemsonCS said:

I am having trouble getting this error to replicate itself today. Not sure why, haven't changed the code.

...

I want to know what part of this is missing...


BufferedInputStream in = new BufferedInputStream([B]url[/B].openStream());
I want to know what url is ! I am sure you have the whole code and not only this one...

Also I would like to know what...
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream([B]file[/B]));
file is...

So could you post the whole code?
Posted Image

#8
ClemsonCS

ClemsonCS

    Newbie

  • Members
  • PipPip
  • 28 posts
url:
www.clemson.edu/administration/budget0506.pdf

file object contains:
budget0506.pdf

#9
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

ClemsonCS said:

url:
www.clemson.edu/administration/budget0506.pdf

file object contains:
budget0506.pdf

This far is how I come...
import java.io.*;

import java.net.*;

public class TestMe {


	public static void main(String[] arg) throws IOException {

		

		URL url = new URL("www.clemson.edu/administration/");

		URLConnection cl = url.openConnection();

		BufferedReader in = new BufferedReader(new InputStreamReader(cl.getInputStream()));

		int[] buff = new int[4096];

		BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("budget0506.pdf"));

		while(in.read(null, 0, buff.length) != -1){

		    out.write(0);

		}

		in.close();

		out.close();

		System.out.println("end of loop");//this doesnt print

	}

}

You will have to work on this or ask someone that can reach your url... I can't !
Posted Image