Results 1 to 2 of 2
  1. #1
    sari's Avatar
    sari is offline Member
    Join Date
    Jan 2009
    Posts
    32
    Rep Power
    0

    Default Trouble with ImageIO

    Hello all,

    It has been quite some time since my last post here; Since then I have been Improving my Java Skills, but I have come across a problem. My current project is to create a program that saves an entire website and all the data that goes along with it: Images, Style Sheets, Java Scripts, Etc. I am currently in the early stages of developing and decided to go about writing the program in this manor. First analyse the index page for images, style sheets, scripts, and then links. Then save the content to a folder and go to the next step and so on, being repeated for each page. I am having a problem with the saving the images the program completely recreates the "Image" folder tree but the images come up empty. I have never used ImageIO before maybe someone can help. Below Is the source code remember that I have only just begun this program so it only reads the index page and the images on the index page right now.

    Java Code:
    import java.io.*;
    import java.net.*;
    import javax.imageio.*;
    import java.awt.image.*;
    
    class loader {
    
        private static void saveImage(String url, String saveName) throws Exception {
            URL addr = new URL(url);
            File save = new File(saveName);
            int begin = saveName.indexOf("/");
            int end = saveName.lastIndexOf("/");
            new File(saveName.substring(begin, end)).mkdirs();
            BufferedImage img = ImageIO.read(addr);
            ImageIO.write(img, saveName.substring(saveName.indexOf("."), saveName.length()), save);
        }
        
        public static void main(String args[]) {
            HttpURLConnection con = null;
            File tm = null;
            String[] sites = args;
            URL tmp = null;
            for (int i = 0;i < sites.length; i++) {
                try {
                    tmp = new URL("http://" + sites[i]);
                    con = (HttpURLConnection) tmp.openConnection();
                    con.connect();
                    new File("/home/keith/" + sites[i]).mkdirs();
                    tm = new File("/home/keith/" + sites[i] + "/index.html");
                    StringBuffer cont = new StringBuffer();
                    BufferedReader in = new BufferedReader(new InputStreamReader((InputStream) con.getContent()));
                    PrintWriter out = new PrintWriter(new FileOutputStream(tm));
                    String line = null;
                    do {
                        line = in.readLine();
                        if (line.contains("<img src=")) {
                            int begin = line.indexOf("<img src=\"") + 10;
                            int end = line.indexOf("\"", begin);
                            String file = line.substring(begin, end);
                            System.out.println("Saving: " + file);
                            if (!file.contains("http:")) {
                                saveImage("http://" + sites[i] + "/" + file, "/home/keith/" + sites[i] + "/" + file);
                            }
                        }
                        if (line != null) {
                            cont.append(line + "\n");
                        }
                    } while (line != null);
                    out.print(cont.toString());
                    out.flush();
                    out.close();
                    in.close();
                    tm = null;
                    tmp = null;
                } catch (ArrayIndexOutOfBoundsException ee) {
                    ee.printStackTrace();
                    continue;
                } catch (Exception e) {
                    e.printStackTrace();
                    System.exit(1);
                }
            }
        }
    }
    Last edited by sari; 03-30-2009 at 10:40 PM.
    If you don't try you can't succeed.

  2. #2
    sari's Avatar
    sari is offline Member
    Join Date
    Jan 2009
    Posts
    32
    Rep Power
    0

    Default

    Bump...
    Since the last post I have updated the code It does not seem to be doing much different now though
    If you don't try you can't succeed.

Similar Threads

  1. trouble with actionPerformed
    By diggitydoggz in forum New To Java
    Replies: 2
    Last Post: 12-26-2008, 03:18 AM
  2. hi there got alittle trouble
    By justime8 in forum New To Java
    Replies: 7
    Last Post: 12-24-2008, 01:22 AM
  3. having some trouble
    By Unknown1369 in forum New To Java
    Replies: 13
    Last Post: 07-22-2008, 12:52 AM
  4. Trouble in Runing
    By kavithaprabhaker in forum New To Java
    Replies: 5
    Last Post: 05-13-2008, 06:59 AM
  5. Help with ImageIO
    By romina in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 06:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •