HttpURLConnection无法打开含有中文的链接???

ghost_029 2011-06-21 12:04:04
HttpURLConnection无法打开含有中文的链接???

URL url = new URL(http://192.168.0.154:8080/test/标题.mp3);
hc = (HttpURLConnection) url.openConnection()
int resCode = hc.getResponseCode();
if (resCode == 404)
throw new Exception(sUrl);

抛异常! 如果http://192.168.0.154:8080/test/标题.mp3
换成http://192.168.0.154:8080/test/1.mp3 就可以,文件名含中文就报错????
...全文
1678 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
mo-session 2013-12-26
  • 打赏
  • 举报
回复
通过本帖子,解决问题,多谢各位。
  • 打赏
  • 举报
回复
全部使用这个UTF-8支持中文的
shuyumac 2012-10-09
  • 打赏
  • 举报
回复
String urlStr = "http://10.10.10.10:8080/servertest/测试下载.pdf";
String docname = urlStr.substring(urlStr.lastIndexOf("/") + 1);

urlStr = urlStr.substring(0,urlStr .lastIndexOf("/"))+"/"+URLEncoder.encode(docname,"utf-8");

URL url = new URL(urlStr);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();

前面的 http://10.10.10.10:8080 不要进行转码,只对后面的进行转码就可以了。我遇到的问题是这样解决的。
LucEaspe 2011-11-08
  • 打赏
  • 举报
回复
用utf8编码
sll_docking 2011-11-08
  • 打赏
  • 举报
回复
谢谢,,解决大问题了,,
飞跃颠峰 2011-06-21
  • 打赏
  • 举报
回复
Url中不能有中文,要转码哦
若鱼1919 2011-06-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 java2000_net 的回复:]
Java code
URL url = new URL(URLEncoder.encode("http://192.168.0.154:8080/test/标题.mp3","UTF-8"));


编码根据具体情况修改,比如GBK
[/Quote]

java.net.MalformedURLException: no protocol: http%3A%2F%2F192.168.0.154%3A8080%2Ftest%2F%E6%A0%87%E9%A2%98.mp3
老紫竹 2011-06-21
  • 打赏
  • 举报
回复
URL url = new URL(URLEncoder.encode("http://192.168.0.154:8080/test/标题.mp3","UTF-8"));


编码根据具体情况修改,比如GBK
zs312979674 2011-06-21
  • 打赏
  • 举报
回复
在读取任何内容的过程中、java默认的是老外的那一套、所以自己要该下子编码、
ghost_029 2011-06-21
  • 打赏
  • 举报
回复
这个有,链接本身是没有问题的
若鱼1919 2011-06-21
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 java2000_net 的回复:]
你的tomcat,是不是支持呢?在 host里面有个
URIEncoding="GBK"
或者
URIEncoding="UTF-8"

这个参数要加上的。

换句话说,你的服务器也要支持中文URL才可以。
[/Quote]

明白了
ghost_029 2011-06-21
  • 打赏
  • 举报
回复
up 求救............
老紫竹 2011-06-21
  • 打赏
  • 举报
回复
你的tomcat,是不是支持呢?在 host里面有个
URIEncoding="GBK"
或者
URIEncoding="UTF-8"

这个参数要加上的。

换句话说,你的服务器也要支持中文URL才可以。
ghost_029 2011-06-21
  • 打赏
  • 举报
回复
引用 7 楼 java2000_net 的回复:
Java code
URL url = new URL("http://192.168.0.154:8080/test/"+URLEncoder.encode("标题","UTF-8")+".mp3");


还是报404的异常
若鱼1919 2011-06-21
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 java2000_net 的回复:]
Java code
URL url = new URL("http://192.168.0.154:8080/test/"+URLEncoder.encode("标题","UTF-8")+".mp3");
[/Quote]

//URL url = new URL("http://localhost:8080/猫.ico");
//URL url = new URL(URLEncoder.encode("http://localhost:8080/猫.ico","utf-8"));
//URL url = new URL("http://localhost:8080/"+URLEncoder.encode("猫","utf-8")+".ico");
//URL url = new URL("http://localhost:8080/"+new String("猫".getBytes("gb2312"),"iso-8859-1")+".ico");
试了,都不好用!
老紫竹 2011-06-21
  • 打赏
  • 举报
回复
URL url = new URL("http://192.168.0.154:8080/test/"+URLEncoder.encode("标题","UTF-8")+".mp3");
老紫竹 2011-06-21
  • 打赏
  • 举报
回复
URL url = new URL("http://192.168.0.154:8080/test/"+URLEncoder.encode("标题.mp3","UTF-8"));



不会变通啊,呵呵!
ghost_029 2011-06-21
  • 打赏
  • 举报
回复
使用UTF-8和GBK都抛下面的异常:
java.net.MalformedURLException: no protocol: http%3A%2F%2F166.111.138.154%3A8080%2Ftest%2F%B1%EA%CC%E2.mp3
softroad 2011-06-21
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 alexandertech 的回复:]

Url中不能有中文,要转码哦
[/Quote]

+1

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧