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

该文章介绍了一个名为UrlUtils的Java工具类,包含从URL获取输入流的方法,以及对中文字符进行UTF-8编码的功能,用于处理HTTP请求中的中文字符处理问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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\]中提到的错误信息"java.io.IOException: Server returned HTTP response code: 400 for URL",这个错误通常表示在与服务器进行通信时发生了问题。HTTP响应代码400表示请求无效,可能是由于请求的URL不正确或请求参数不正确导致的。 根据引用\[2\]中提供的正确的URL示例"https://open.douyin.com/item/comment/list/?open_id=URLEncoder.encode("@8hhauTCnM4ltGM780mDqPP","utf-8")",可以看出正确的URL应该是经过编码的,并且参数应该正确传递。 根据引用\[3\]中提供的代码示例,可以看出在使用org.apache.commons.io.FileUtils进行文件下载时,需要确保URL的正确性,并且需要处理可能的异常情况。 综上所述,根据提供的错误信息"Server returned HTTP response code: 400 for URL: https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi",可能是由于请求的URL不正确或请求参数不正确导致的。您可以检查URL是否正确,并确保请求参数的正确传递。如果问题仍然存在,您可以尝试使用异常处理来捕获可能的异常情况。 #### 引用[.reference_title] - *1* *3* [java.io.IOException: Server returned HTTP response code: 400 for URL 问题定位与解决](https://blog.csdn.net/LS7011846/article/details/84074946)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [java.io.IOException: Server returned HTTP response code: 400 for URL: https://open.douyin.com/](https://blog.csdn.net/weixin_43852094/article/details/115181233)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值