The Artima Developer Community
Sponsored Link

Java Answers Forum
listing file locations from web page

6 replies on 1 page. Most recent reply: Apr 1, 2004 5:53 AM by Christian Guldner

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
bob

Posts: 4
Nickname: downe
Registered: Mar, 2004

listing file locations from web page Posted: Mar 30, 2004 4:39 AM
Reply to this message Reply
Advertisement
hey,
i wanted to list the locations of image files of my web page i'm creating; i expected to list them in an array...

Could anyone help? cheers


Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: listing file locations from web page Posted: Mar 30, 2004 7:48 AM
Reply to this message Reply
You lost me on "hey"....

Adam

bob

Posts: 4
Nickname: downe
Registered: Mar, 2004

Re: listing file locations from web page Posted: Mar 30, 2004 4:16 PM
Reply to this message Reply
heh sorry,
i want to create a program that will find the file locations of the images files on my web page. I anticipated the list would be collected in an array and display in a TextArea.
Thanks..

Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: listing file locations from web page Posted: Mar 31, 2004 3:33 AM
Reply to this message Reply
And where is it to find these file locations? Scan the HTML page and look for img tags? Or is it to scan your disk for these images? Or something else?

Adam

bob

Posts: 4
Nickname: downe
Registered: Mar, 2004

Re: listing file locations from web page Posted: Mar 31, 2004 2:27 PM
Reply to this message Reply
it would scan the html page, i could image an input box to type the url you'd like to scan

bob

Posts: 4
Nickname: downe
Registered: Mar, 2004

Re: listing file locations from web page Posted: Mar 31, 2004 2:29 PM
Reply to this message Reply
sorry 'imagine' an input box (i'm tired thats my exuse)

Christian Guldner

Posts: 1
Nickname: guldner
Registered: Apr, 2004

Re: listing file locations from web page Posted: Apr 1, 2004 5:53 AM
Reply to this message Reply
I believe there are many ways to achieve what you want (many libraries that will parse a html doc for you)
Maybe you can use this as a starting point (although it seems strange to use swing, it works): http://java.sun.com/developer/TechTips/1999/tt0923.html

I have changed the example to list img-tags' src attribute:

import java.io.*;
import java.net.*;
import javax.swing.text.*;
import javax.swing.text.html.*;

class GetLinks {
public static void main(String[] args) {
EditorKit kit = new HTMLEditorKit();
Document doc = kit.createDefaultDocument();

// The Document class does not yet
// handle charset's properly.
doc.putProperty("IgnoreCharsetDirective",
Boolean.TRUE);
try {

// Create a reader on the HTML content.
//Reader rd = getReader(args[0]);
Reader rd = getReader("http://www.domain.tld/test.html");

// Parse the HTML.
kit.read(rd, doc, 0);

// Iterate through the elements
// of the HTML document.
ElementIterator it = new ElementIterator(doc);
javax.swing.text.Element elem;
while ((elem = it.next()) != null) {
SimpleAttributeSet s1 = (SimpleAttributeSet)elem.getAttributes().getAttribute(HTML.Tag.IMG);
Object o = elem.getAttributes().getAttribute(HTML.Attribute.SRC);
if (o != null) {
System.out.println(o);
}
}
} catch (Exception e) {
e.printStackTrace();
}
System.exit(1);
}

// Returns a reader on the HTML data. If 'uri' begins
// with "http:", it's treated as a URL; otherwise,
// it's assumed to be a local filename.
static Reader getReader(String uri)
throws IOException {
if (uri.startsWith("http:")) {

// Retrieve from Internet.
URLConnection conn =
new URL(uri).openConnection();
return new
InputStreamReader(conn.getInputStream());
} else {

// Retrieve from file.
return new FileReader(uri);
}
}
}

Flat View: This topic has 6 replies on 1 page
Topic: Reversing a String Previous Topic   Next Topic Topic: dynamic class re-loading: class casting problem

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use