url中还有中文字符的问题,高手解答,马上给分

qieyj 2003-09-05 10:43:33
String url_old = java.net.URLEncoder.encode("http://localhost:8080/lantop_web/wj/样品.jpg");
URL stdURL = new URL(url_old);
BufferedReader stdIn = null;
try {
stdURL.openStream();
out.println("success");
}
catch (Exception e) {
out.println("failure");
}
这个例子中url有中文字符,因此,总是输出failure,实际上这个地址是正确的。
怎么解决呢,我用java.net.URLEncoder.encode也不行啊。
...全文
47 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
squallzeng 2003-09-05
  • 打赏
  • 举报
回复
把你的文件名改成英文名不就可以了吗*-*
cbhyk 2003-09-05
  • 打赏
  • 举报
回复
先看看是什么错:
e.printStackTrace()
ejbcreate 2003-09-05
  • 打赏
  • 举报
回复
试试用一个英文的看看:
//String url_old = java.net.URLEncoder.encode("http://localhost:8080/lantop_web/wj/样品.jpg");
String url_old = java.net.URLEncoder.encode("http://localhost:8080/lantop_web/wj/abc.jpg");

URL stdURL = new URL(url_old);
BufferedReader stdIn = null;
try {
stdURL.openStream();
out.println("success");
}
catch (Exception e) {
out.println("failure");
}

如果没出错,哪就说明中文文件名会引起错误;
试试下面的方法:
String url_old = "http://localhost:8080/lantop_web/wj/"+java.net.URLEncoder.encode("样品.jpg");

zbcomn 2003-09-05
  • 打赏
  • 举报
回复
<% //获得样品的编码
String tmpId;
tmpId="样品";
tmpId = java.net.URLEncoder.encode(tmpId);
%>
<a href="222.jsp?tmpTitle=<%=tmpId%>">样品</a>
<%
String tmpTitle;
tmpTitle = request.getParameter("tmpTitle");
tmpTitle = new String(tmpTitle.getBytes("ISO-8859-1"),"gb2312");//转换
%>
myblessu 2003-09-05
  • 打赏
  • 举报
回复
这样行不行?

String url_old = "http://localhost:8080/lantop_web/wj/"+URLEncoder.encode("样品.jpg");

leshui 2003-09-05
  • 打赏
  • 举报
回复
先把 样品.jpg 编码一下
然后加上路径试试
tifaling 2003-09-05
  • 打赏
  • 举报
回复
只能根据exception来判断错误

81,092

社区成员

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

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