获取网络连接字符串-模板代码

来源:互联网 发布:背六级英语单词的软件 编辑:程序博客网 时间:2024/05/16 05:02
/**     * 获取网络连接字符串     *     * @param is     * @return     * @throws IOException     */    private static String getStringFromInputStream(InputStream is) throws IOException {        ByteArrayOutputStream baos = new ByteArrayOutputStream();        byte[] buffer = new byte[1024];        int len = -1;        while ((len = is.read(buffer)) != -1) {            baos.write(buffer, 0, len);        }        is.close();        /**         * 可从服务端改发出编码为 utf-8,用以下代码         * response.getOutputStream().write("登录成功".getBytes("utf-8"));         * response.getOutputStream().write("登录失败".getBytes("utf-8"));         */        String html = baos.toString();    //默认 baos数组 转换成字符串采用 utf-8 格式        /**         * 可从客户端改接收编码为GBK,用以下代码         * response.getOutputStream().write("登录成功".getBytes());         * response.getOutputStream().write("登录失败".getBytes());         *///        byte[] bytes = baos.toByteArray();  //将 baos数组 转换成ByteArray 数组//        String html = new String(bytes, "gbk"); //以 gbk格式 转换为String 字符串        baos.close();        return html;    }
0 0
原创粉丝点击