Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 在本地路径中保存输入流_Xml - Fatal编程技术网

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

Xml 在本地路径中保存输入流,xml,Xml,我试图点击URL并尝试将XML文件保存到本地路径,但我无法做到 我使用的代码在这里 公共类T_save{ 公共静态void下载(字符串地址、字符串localFileName){ OutputStream out=null; URLConnection conn=null; InputStream in=null try { URL url = new URL("url"); // URL url = new URL(address); conn =

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

我使用的代码在这里

公共类T_save{ 公共静态void下载(字符串地址、字符串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=新文件(地址+本地文件名);
FileWriter FileWriter=新的FileWriter(文件);
BufferedReader reader=新的BufferedReader(
新的InputStreamReader(in);
字符串行=null;
而((line=reader.readLine())!=null){
fileWriter.write(行);
}
fileWriter.flush();
fileWriter.close();
}捕获(异常){
异常。printStackTrace();
}最后{
试一试{
if(in!=null){
in.close();
}
if(out!=null){
out.close();
}
}捕获(ioe异常ioe){
}
}
}
公共静态无效下载(字符串地址){
int lastSlashIndex=地址.lastIndexOf('\');
如果(lastSlashIndex>=0&&lastSlashIndex
您可以这样做

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]); }
         */
    }
}
使用(“\”)作为分隔符,因此发生错误..要解决此问题..有两个选项

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

int lastSlashIndex=address.lastIndexOf('/')


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

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

以(“/”)作为分隔符,像这样传递输入

download("C://Documents and Settings//ocp//output.xml");
它将成功运行

使用此类型的身份验证。。。。下载common-codec1.1.jar()并将其放置在类路径中

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();
        }
    }

}

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

是否有其他方法可以将输入流保存到本地路径中的xml文件中?任何其他方法都意味着??您好,我尝试过,但无法找到的本地文件名“+c:/users/praveen/praveen.xmlHi-naren路径似乎已解决。在包含验证器类之后,我得到了http 401 eror..试图检查出错的地方java.io.IOException:服务器返回了http响应代码:401,URL:URL位于sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1625)在sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Http-sURLConnectionImpl.java:254)在T_save.download(T_save.java:28)在T_save.download(T_save.java:98)在T_save.main(T_save.java:108)运行程序后,我得到上面的错误。。我更新了我正在使用的问题代码检查您的用户名和密码是否正确。。。您在代码中应用身份验证的位置?
download("C://Documents and Settings//ocp//output.xml");
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();
        }
    }

}