Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2005

Tip: Looking for answers? Try searching our database.

FileInputStream via getInputStream

Thread view: 
Billy - 14 Oct 2005 07:48 GMT
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);}
Andrew Thompson - 14 Oct 2005 08:08 GMT
> 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();
[quoted text clipped - 4 lines]
>     } 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>
Billy - 14 Oct 2005 09:10 GMT
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();

Billy wrote:
> 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();
[quoted text clipped - 4 lines]
>     } 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>
Andrew Thompson - 14 Oct 2005 09:16 GMT
> I can't make is "self contained" as I don't own de jar in wich
> "loadmap()" resides.

Use a 'dummy' jar.

>  The server is intranet so that won't help either.

Move it (the page, not the intranet)

> The problem ..

No problems, just challenges.

HTH
Chris Smith - 14 Oct 2005 16:50 GMT
> 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.

Signature

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Billy - 25 Oct 2005 18:22 GMT
>    URL url = new URL("http://www.mysite/myfile");
>    URLConnection con = url.openConnection();
[quoted text clipped - 5 lines]
>    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:-))
Roedy Green - 15 Oct 2005 04:25 GMT
>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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Roedy Green - 15 Oct 2005 04:23 GMT
>public static Image loadmap (InputStream fs)
>{
>...
> }

Your loadmap code might be making the erroneous assumption that every
read will get all the bytes it asks for.

see http://mindprod.com/jgloss/readblocking.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.