FileInputStream via getInputStream

B

Billy

I have a class wich loads data from a InputStream :

public static Image loadmap (InputStream fs)
{
....
}

When I use it with a local filename all works as excpected:

loadmap (new FileInputStream("myfile"));



Now I want to open this file from a webserver using an url as is the
following, but is seems that urlfs passed to loadmap is empty? Any
ideas?


try {
URL url = new URL("http://www.mysite/myfile");
try {
URLConnection con = url.openConnection();
con.connect();
InputStream urlfs;
urlfs = con.getInputStream();
return loadmap (urlfs);
} catch (IOException e) {System.out.println(e);}
} catch (MalformedURLException e) {System.out.println(e);}
 
A

Andrew Thompson

Billy said:
try {
URL url = new URL("http://www.mysite/myfile");

'404' in 'mybrowser', but.. is your server treating it as a directory?
try {
URLConnection con = url.openConnection();
con.connect();
InputStream urlfs;
urlfs = con.getInputStream();
return loadmap (urlfs);
} catch (IOException e) {System.out.println(e);}
} catch (MalformedURLException e) {System.out.println(e);}

And what, exactly are you doing with the
'java.net.UnknownHostException's?

No, don't tell me, let the (short, complete) code do the talking.
<http://www.physci.org/codes/sscce.jsp>
 
B

Billy

I can't make is "self contained" as I don't own de jar in wich
"loadmap()" resides. The server is intranet so that won't help either.

The problem is that i don't know if
con.getInputStream() results in the same datatype as
FileInputStream("myfile")

URL url = new URL("http://www.mysite/myfile");
URLConnection con = url.openConnection();
con.connect();
InputStream urlfs;
urlfs = con.getInputStream();


try {
URL url = new URL("http://www.mysite/myfile");


'404' in 'mybrowser', but.. is your server treating it as a directory?

try {
URLConnection con = url.openConnection();
con.connect();
InputStream urlfs;
urlfs = con.getInputStream();
return loadmap (urlfs);
} catch (IOException e) {System.out.println(e);}
} catch (MalformedURLException e) {System.out.println(e);}


And what, exactly are you doing with the
'java.net.UnknownHostException's?

No, don't tell me, let the (short, complete) code do the talking.
<http://www.physci.org/codes/sscce.jsp>
 
C

Chris Smith

Billy said:
I can't make is "self contained" as I don't own de jar in wich
"loadmap()" resides. The server is intranet so that won't help either.

That is a shame. My first guess is that the problem is in loadmap().
Since it apparently works when the data is immediately available, but
not when you need to wait for a connection over a network, my guess is
that the person writing the loadmap() function is misusing the
available() method.

Try this code, and let us know if it fixes anything. Exception handling
removed for clarity.

URL url = new URL("http://www.mysite/myfile");
URLConnection con = url.openConnection();
con.connect();
InputStream urlfs = con.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
int c;
while ((c = urlfs.read()) != -1) out.write((byte) c);
urlfs.close();

return loadmap (new ByteArrayInputStream(out.toByteArray());

Not that you should put the code into production without thinking
through the consequences... but just let us know if it works.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
R

Roedy Green

The problem is that i don't know if
con.getInputStream() results in the same datatype as
FileInputStream("myfile")

They DON'T result in the same type. You can find that out by dumping
inputstream.getClass()

But they DO both implement InputStream.
 
B

Billy

URL url = new URL("http://www.mysite/myfile");
URLConnection con = url.openConnection();
con.connect();
InputStream urlfs = con.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
int c;
while ((c = urlfs.read()) != -1) out.write((byte) c);
urlfs.close();
return loadmap (new ByteArrayInputStream(out.toByteArray());

out.size() gives a positive value, so I already got rid of the
java.lang.NegativeArraySizeException
Also the available() method was missing, so that has been solved (after
lots of negociating:))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top