java.io.IOException: Server returned HTTP response code: 400 for URL解决方法

java 复制代码
package com.office.convertoffice.utils;

import org.apache.commons.io.IOUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

/**
 * @Auther: ZHANG PU
 * @Date: 2023/4/26 10:12
 * @Description:
 */
public class UrlUtils {

    /**
     * 功能描述: 从地址中获取流
     *
     * @author zhang pu
     * @date 10:13 2023/4/26
     */
    public static InputStream getInputStreamByUrl(String strUrl) {
        HttpURLConnection conn = null;
        try
        {
            String s = tranformStyle(strUrl);
            URL url = new URL(s);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(20 * 1000);
            final ByteArrayOutputStream output = new ByteArrayOutputStream();
            IOUtils.copy(conn.getInputStream(), output);
            return new ByteArrayInputStream(output.toByteArray());
        } catch (Exception e)
        {
            e.printStackTrace();
        } finally
        {
            try
            {
                if (conn != null)
                {
                    conn.disconnect();
                }
            } catch (Exception e)
            {
            }
        }
        return null;
    }

    /**
     * 对中文字符进行UTF-8编码
     *
     * @param source 要转义的字符串
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String **tranformStyle**(String source) throws UnsupportedEncodingException {
        char[] arr = source.toCharArray();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arr.length; i++)
        {
            char temp = arr[i];
            if (isChinese(temp))
            {
                sb.append(URLEncoder.encode("" + temp, "UTF-8"));
                continue;
            }
            sb.append(arr[i]);
        }
        return sb.toString();
    }

    /**
     * 获取字符的编码值
     *
     * @param s
     * @return
     * @throws UnsupportedEncodingException
     */
    public static int getValue(char s) throws UnsupportedEncodingException {
        String temp = (URLEncoder.encode("" + s, "GBK")).replace("%", "");
        if (temp.equals(s + ""))
        {
            return 0;
        }
        char[] arr = temp.toCharArray();
        int total = 0;
        for (int i = 0; i < arr.length; i++)
        {
            try
            {
                int t = Integer.parseInt((arr[i] + ""), 16);
                total = total * 16 + t;
            } catch (NumberFormatException e)
            {
                e.printStackTrace();
                return 0;
            }
        }
        return total;
    }

    /**
     * 判断是不是中文字符
     *
     * @param c
     * @return
     */
    public static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS)
        {
            return true;
        }
        return false;
    }
}
相关推荐
程序员小凯1 小时前
Spring Boot测试框架详解
java·spring boot·后端
豐儀麟阁贵1 小时前
基本数据类型
java·算法
_extraordinary_1 小时前
Java SpringMVC(二) --- 响应,综合性练习
java·开发语言
程序员 Harry2 小时前
深度解析:使用ZIP流式读取大型PPTX文件的最佳实践
java
wxweven3 小时前
校招面试官揭秘:我们到底在寻找什么样的技术人才?
java·面试·校招
程序员Aries3 小时前
自定义网络协议与序列化/反序列化
linux·网络·c++·网络协议·程序人生
陈陈爱java3 小时前
新知识点背诵
java
失散133 小时前
分布式专题——39 RocketMQ客户端编程模型
java·分布式·架构·rocketmq
泽02023 小时前
Linux之环境变量
java·linux·redis
程序媛徐师姐4 小时前
Java基于SpringBoot的茶叶商城系统,附源码+文档说明
java·spring boot·java springboot·茶叶商城系统·java茶叶商城系统·茶叶·java茶叶商城