1.文件名称带中文
记录一次使用HttpURLConnection或HttpsURLConnection获取文件400报错问题,及解决办法。话不多说直接上代码。
请求路径,http:// 192.168.57.** :8080/upload/file/小学生报名表_20220829215306A002.xlsx
try {
URL url;
//url解码
url = new URL("http:// 192.168.57.** :8080/upload/file/小学生报名表02.xlsx");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置超时间为4秒
conn.setConnectTimeout(4 * 1000);
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//得到输入流
InputStream inputStream = conn.getInputStream();
System.out.println(inputStream);
EasyExcelListener easyExcelListener = new EasyExcelListener();
ExcelReaderBuilder read = EasyExcelFactory.read(inputStream, easyExcelListener);
ExcelReader excelReader = read.build();
// step2. 获取各个sheet页信息
List<ReadSheet> sheets = excelReader.excelExecutor().sheetList();
// step3. 获取各个Shhet页表格内容存于map
Map<Integer, List<LinkedHashMap>> sheetInfos = new HashMap<>(sheets.size());
for (ReadSheet sheet : sheets) {
Integer sheetNo = sheet.getSheetNo();
excelReader.read(sheet);
sheetInfos.put(sheetNo, easyExcelListener.getListMap());
}
System.out.println(sheetInfos);
}catch (Exception e){
e.printStackTrace();
}
报错如下:400
java.io.IOException: Server returned HTTP response code: 400 for URL
2.解决办法
先了解一下 java.net.url的构造函数
函数名 | 描述 |
---|---|
URL(String url) | 根据url构建一个URL对象 |
URL(String protocol, String host, int port, String file) | 创建一个URL从指定对象 protocol,host,port 号码,和file。 |
URL(String protocol, String host, int port, String file, URLStreamHandler handler) | URL根据指定的, 数字和protocol,创建对象 。hostportfilehandler |
URL(String protocol, String host, String file) | 根据指定的protocol 名称,host名称和file名称创建URL 。 |
URL(URL context, String spec) | 通过解析指定上下文中的给定规范来创建URL。 |
URL(URL context, String spec, URLStreamHandler handler) | 通过使用指定上下文中的指定处理程序解析给定规范来创建URL。 |
当我们传参有中文参数,或者特殊字符时,我们需要对URL中特殊字符进行编码。
网页中的表单使用POST方法提交时,数据内容的类型是 application/x-www-form-urlencoded,这种类型会:
1.字符"a"-“z”,“A”-“Z”,“0”-“9”,“.”,“-”,“*”,和"_" 都不会被编码;
2.将空格转换为加号 (+) ;
3.将非文本内容转换成"%xy"的形式,xy是两位16进制的数值;
4.在每个 name=value 对之间放置 & 符号。
使用 java.net.URLEncoder.encode() 对url 进行编码
URLEncoder类包含将字符串转换为 application/x-www-form-urlencoded MIME 格式的静态方法。
主要 因为在 http传输过程中,对URL进行编码,是为了避免URL解析发生歧义,简化解码方式,
比如:URL采用“&”作为不同参数的分隔符,假如某个特定的参数的名称或者值本身就包括分隔符“&”,如果不将参数中的“&”做编码转换,那势必会增加URL解析的复杂性,提高解析错误的概率。
解决方式一
url = new URL("http:// 192.168.57.** :8080/upload/file/"+URLEncoder.encode("小学生报名表02.xlsx", "utf-8"));
解决方式二
url = new URL("http", "192.168.57.**", 8080,"/" + "upload/file" + "/" + URLEncoder.encode("小学生报名表02.xlsx", "utf-8"));
3.URLDecoder.decode和URLEncoder.encode区别
URLEncoder.encode(String s,String charcter)s字符串参数,charcte编码方式
URLEncoder.encode(String s,String charcter)静态方法,它可以将普通字符串转换成application/x-www-form-urlencoded MIME字符串,进行编码。
URLDecoder.decode(String s,String charcte) s字符串参数,charcte编码方式
URLDecoder.decode(String s,String charcter)静态方法,它可以将看上去乱码的特殊字符串转换成普通字符串,进行解码。
测试用例
public static void main (String args []){
try {
//进行编码
String encode =URLEncoder.encode("坤坤学java", "utf-8");
//输出编码后的内容
System.out.println("编码后的内容:"+encode);
//解码
//进行解码
String decode = URLDecoder.decode(encode, "utf-8");
//输出解码后的内容
System.out.println("解码后的内容:"+decode);
}catch (Exception e){
e.printStackTrace();
}
}
打印结果
编码后的内容:%E5%9D%A4%E5%9D%A4%E5%AD%A6java
解码后的内容:坤坤学java