将输入流保存在本地路径中

时间:2014-02-15 09:13:41

标签: xml

我正在尝试点击URL并尝试将XML文件保存到本地路径但我无法做到。

我正在使用的代码在这里

  

public class T_save {       public static void download(String address,String localFileName){           OutputStream out = null;           URLConnection conn = null;           InputStream in = null;

    try {
       URL url = new URL("url");
    //  URL url = new URL(address);
        conn = url.openConnection();
        in = conn.getInputStream();
        File file = new File(address+localFileName);
        FileWriter fileWriter = new FileWriter(file);

        BufferedReader reader = new BufferedReader(
                new InputStreamReader(in));
        String line = null;
        while ((line = reader.readLine()) != null) {
            fileWriter.write(line);
        }
        fileWriter.flush();
        fileWriter.close();

    } catch (Exception exception) {
        exception.printStackTrace();
    } finally {
        try {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        } catch (IOException ioe) {
        }
    }
}

public static void download(String address) {
    int lastSlashIndex = address.lastIndexOf('\');
    if (lastSlashIndex >= 0 && lastSlashIndex < address.length() - 1) {
        System.out.println(address.substring(0, lastSlashIndex+1)+"\t\t\t"+ 

  address.substring(lastSlashIndex + 1));
        download(address.substring(0, lastSlashIndex+1), address.substring

     (lastSlashIndex + 1));

    } else {
        System.err.println("Could not figure out local file name for "
                + address);
    }
}

public static void main(String[] args) {

    download("C:\\Users\\praveen\\chaithu12.xml");}
    /*
     * for (int i = 0; i < args.length; i++) { download(args[i]); }

     */
 public static class CustomAuthenticator extends Authenticator {



    // for entering password

    protected PasswordAuthentication getPasswordAuthentication() {



        // Get information about the request

        String prompt = getRequestingPrompt();

        String hostname = getRequestingHost();

        InetAddress ipaddr = getRequestingSite();

        int port = getRequestingPort();



        String username = "username";

        String password = "password";



        // Return the information (a data holder that is used by Authenticator)

        return new PasswordAuthentication(username, password.toCharArray());



    }
}
     }

1 个答案:

答案 0 :(得分:0)

你可以这样做

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

public class T_save {
    public static void download(String address, String localFileName) {
        OutputStream out = null;
        URLConnection conn = null;
        InputStream in = null;

        try {
            URL url = new URL("http://www.w3schools.com/");
        //  URL url = new URL(address);
            conn = url.openConnection();
            in = conn.getInputStream();

            File file = new File(address+localFileName);
            FileWriter fileWriter = new FileWriter(file);

            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(in));
            String line = null;
            while ((line = reader.readLine()) != null) {
                fileWriter.write(line);
            }
            fileWriter.flush();
            fileWriter.close();

        } catch (Exception exception) {
            exception.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException ioe) {
            }
        }
    }

    public static void download(String address) {
        int lastSlashIndex = address.lastIndexOf('/');
        if (lastSlashIndex >= 0 && lastSlashIndex < address.length() - 1) {
            System.out.println(address.substring(0, lastSlashIndex+1)+"\t\t\t"+ address.substring(lastSlashIndex + 1));
            download(address.substring(0, lastSlashIndex+1), address.substring(lastSlashIndex + 1));

        } else {
            System.err.println("Could not figure out local file name for "
                    + address);
        }
    }

    public static void main(String[] args) {

        download("D://output.xml");
        /*
         * for (int i = 0; i < args.length; i++) { download(args[i]); }
         */
    }
}

我以这种格式获得了Ur Point你传递的输入

download("C:\\Documents and Settings\\ocp\\output.xml");

以('\')作为分隔符,以便发生错误。解决此问题。两个选项

1)您必须在上述程序中替换此行

int lastSlashIndex = address.lastIndexOf('/');


int lastSlashIndex = address.lastIndexOf('\\');

2)不要改变上述程序中的任何内容

将这样的输入传递给('/')作为分隔符

download("C://Documents and Settings//ocp//output.xml");

它会成功运行..

USE这种类型的身份验证....下载common-codec1.1.jar(from here)并将其放在classpath中

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.apache.commons.codec.binary.Base64;



public class ConnectToUrlUsingBasicAuthentication {

    public static void main(String[] args) {

        try {
            String webPage = "http://google.com";
            String name = "youraddress@gmail.com";
            String password = "urpwd";

            String authString = name + ":" + password;
            System.out.println("auth string: " + authString);
            byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
            String authStringEnc = new String(authEncBytes);
            System.out.println("Base64 encoded auth string: " + authStringEnc);

            URL url = new URL(webPage);
            URLConnection urlConnection = url.openConnection();
            urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
            InputStream is = urlConnection.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);

            int numCharsRead;
            char[] charArray = new char[1024];
            StringBuffer sb = new StringBuffer();
            while ((numCharsRead = isr.read(charArray)) > 0) {
                sb.append(charArray, 0, numCharsRead);
            }
            String result = sb.toString();

            System.out.println("*** BEGIN ***");
            System.out.println(result);
            System.out.println("*** END ***");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

如果您遇到任何问题,请告诉我