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;
    }
}
相关推荐
Noii.2 分钟前
Spring Boot初级概念及自动配置原理
java·spring boot·后端
探索java9 分钟前
Tomcat Server 组件原理
java·后端·tomcat
勿在浮沙筑高台9 分钟前
无法获取实体类com.example.springdemo2.entity.po.UserPO对应的表名!
java·spring boot·mybatis
用户83562907805130 分钟前
Java使用Spire.Doc实现Word转PDF:格式精准的自动化解决方案
java
陆小叁42 分钟前
基于Flink CDC实现联系人与标签数据实时同步至ES的实践
java·elasticsearch·flink
CHEN5_0244 分钟前
【Java基础】反射,注解,异常,Java8新特性,object类-详细介绍
java·开发语言
云间月13141 小时前
飞算JavaAI智慧文旅场景实践:从景区管理到游客服务的全链路系统搭建
java·开发语言
盖世英雄酱581361 小时前
必须掌握的【InheritableThreadLocal】
java·后端
找不到、了1 小时前
JVM的逃逸分析深入学习
java·jvm
用户0332126663671 小时前
Java 查找并替换 PDF 中的文本:高效自动化处理指南
java