Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.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
如何下载java页面并将其保存到UTF-8?_Java_Utf 8_Httpurlconnection - Fatal编程技术网

如何下载java页面并将其保存到UTF-8?

如何下载java页面并将其保存到UTF-8?,java,utf-8,httpurlconnection,Java,Utf 8,Httpurlconnection,此函数以UTF8向服务器发送请求,并以UTF8接收: public String downloadPage(String _url, String _reqm, String _params) { try { if (_reqm == null || (_reqm == "POST" && _params == null)) throw new IOException(); URL

此函数以UTF8向服务器发送请求,并以UTF8接收:

public String downloadPage(String _url, String _reqm, String _params) {
        try {
            if (_reqm == null || (_reqm == "POST" && _params == null))
                throw new IOException();

            URL _myURL = null;

            if (_reqm == "GET") {
                _myURL = new URL(_params == null ? _url : _url + "?" + _params); //URLEncoder.encode(_params, "UTF-8")
            } else if (_reqm == "POST") {
                _myURL = new URL(_url);
            }

            HttpURLConnection pageConnection = (HttpURLConnection) _myURL.openConnection();
            pageConnection.setUseCaches(false);
            pageConnection.setDoOutput(true);
            pageConnection.setDoInput(true);
            pageConnection.setInstanceFollowRedirects(false);
            pageConnection.setRequestMethod(_reqm);

            pageConnection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml");
            pageConnection.setRequestProperty("Accept-Charset", "UTF-8");
            pageConnection.setRequestProperty("charset", "UTF-8");
            pageConnection.setRequestProperty("Connection", "keep-alive");
            pageConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31");

            if (_reqm == "POST") {
                pageConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

                OutputStreamWriter writer = new OutputStreamWriter(pageConnection.getOutputStream());
                writer.write(_params); //URLEncoder.encode(_params, "UTF-8")
                writer.flush();
                writer.close();
            }

            BufferedReader reader = new BufferedReader(new InputStreamReader(pageConnection.getInputStream()));
            String inputLine;
            StringBuilder text = new StringBuilder();

            while ((inputLine = reader.readLine()) != null) {
                text.append(inputLine + "\n");
            }

            reader.close();
            return text.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return "ERROR";
        }
    }
public void writeFile(String _content, String _fileName) {
        try {
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(_fileName)), "UTF-8"));
            out.write(_content);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
此函数将UTF8字符串保存到UTF8中的文件:

public String downloadPage(String _url, String _reqm, String _params) {
        try {
            if (_reqm == null || (_reqm == "POST" && _params == null))
                throw new IOException();

            URL _myURL = null;

            if (_reqm == "GET") {
                _myURL = new URL(_params == null ? _url : _url + "?" + _params); //URLEncoder.encode(_params, "UTF-8")
            } else if (_reqm == "POST") {
                _myURL = new URL(_url);
            }

            HttpURLConnection pageConnection = (HttpURLConnection) _myURL.openConnection();
            pageConnection.setUseCaches(false);
            pageConnection.setDoOutput(true);
            pageConnection.setDoInput(true);
            pageConnection.setInstanceFollowRedirects(false);
            pageConnection.setRequestMethod(_reqm);

            pageConnection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml");
            pageConnection.setRequestProperty("Accept-Charset", "UTF-8");
            pageConnection.setRequestProperty("charset", "UTF-8");
            pageConnection.setRequestProperty("Connection", "keep-alive");
            pageConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31");

            if (_reqm == "POST") {
                pageConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

                OutputStreamWriter writer = new OutputStreamWriter(pageConnection.getOutputStream());
                writer.write(_params); //URLEncoder.encode(_params, "UTF-8")
                writer.flush();
                writer.close();
            }

            BufferedReader reader = new BufferedReader(new InputStreamReader(pageConnection.getInputStream()));
            String inputLine;
            StringBuilder text = new StringBuilder();

            while ((inputLine = reader.readLine()) != null) {
                text.append(inputLine + "\n");
            }

            reader.close();
            return text.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return "ERROR";
        }
    }
public void writeFile(String _content, String _fileName) {
        try {
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(_fileName)), "UTF-8"));
            out.write(_content);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
我使用这两种方法,如下所示:

String downloadedPage = myTelecom.downloadPage("http://www.sbrf.ru/moscow/ru/", "GET", null);
myIO.writeFile(downloadedPage, "original.html");
尽管有多种编码指示,但我无法使其工作。 无论请求方法、域或“接受字符集”或“字符集”,它都不起作用

没有俄罗斯人,没有匈牙利人,他们看起来像他们应该看起来的样子,我不知道我在哪里犯了错误


可能是什么问题?

要更快获得更好的帮助,请发布。服务器正在Windows-1251中发送数据。并且您没有显式地将任何编码传递给InputStreamReader,导致它使用平台默认编码进行解码,这只能在偶然情况下是Windows-1251。