xdevel2000
xdevel2000

Reputation: 21374

URLConnection.getContent with HTML file

When I use something like:

URL url = new URL(a_url);
URLConnection url_conn = url.openConnection();
Object content = url_conn.getContent();

and the MIME type of the file retrieved is HTML or XML I debugged that content at run-time will contain an instance of:

sun.net.www.protocol.http.HttpURLConnection$HttpInputStream

Now if I want to use instanceof on that instance how can I do?

if (content instanceof PlainTextInputStream)
{
...
}  
else if(content instanceof ImageProducer)
{
...
}
else if(content instanceof ???) {}

Upvotes: 1

Views: 6174

Answers (1)

Ramesh PVK
Ramesh PVK

Reputation: 15446

You should not depend of implementation classes. It will break someday.

I think this how you should do it based on the request headers:

URLConnection url_conn = url.openConnection();
httpURLConnection http_url_conn = (httpURLConnection )url.openConnection();

String contentType = http_url_conn.getContentType()

  if(contentType.contains("text/plain")){
    //handle plain text
    .....
  } else if(contentType.contains("images/jpeg")){
    //handle image
    ......
  } 

Read more about Content-Type here:

Upvotes: 7

Related Questions