中文链接无法打开!!!!

一叶障树 2009-09-17 09:35:30
http://localhost:8080/AAA/download/20090917092500953_我是中国人.txt
这个链接打开时出错,信息如下:

type ステータスレポート
メッセージ /AAA/download/20090917092500953_%E6%88%91%E6%98%AF%E4%B8%AD%E5%9B%BD%E4%BA%BA.txt
説明 The requested resource (/AAA/download/20090917092500953_%E6%88%91%E6%98%AF%E4%B8%AD%E5%9B%BD%E4%BA%BA.txt) is not available.

哪位大哥帮小弟解决下?
谢谢啦!

...全文
127 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
一叶障树 2009-09-17
  • 打赏
  • 举报
回复
已经解决啦,谢谢各位的帮忙哈!
浴火涅磐 2009-09-17
  • 打赏
  • 举报
回复
楼主除了设置之外还要加一个解码器,具体是什么码还需要去判断,这个很麻烦
twinkleliang 2009-09-17
  • 打赏
  • 举报
回复
tomcat设置修改一下server.xml <!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->下面增加一行URIEncoding="UTF-8" useBodyEncodingForURI="true"
Johnson_Hong 2009-09-17
  • 打赏
  • 举报
回复
ie是以utf-8编码url的body部分,以gbk编码参数部分,可以在ie的选项里勾选总是以utf-8发送。

你的中文部分在url的body里,如果你把tomcat的server.xml里加上URIEncoding="utf-8",就可以打开中文的url,例如http://localhost:8081/test/测试.jsp
yojiwei 2009-09-17
  • 打赏
  • 举报
回复
对链接中的中文实现进行unicode编码,然后再读出来,就可以了。
对中文编码比较简单直接的方法:找到C:\Program Files\Java\jdk1.5.0_10\bin\native2ascii.exe
然后cmd,将native2ascii.exe拖入cmd打开的dos窗口,回车。
写你需要编码的中文如:
C:\Documents and Settings\Administrator>"C:\Program Files\Java\jdk1.5.0_10\bin\n
ative2ascii.exe"
我是中国人
\u6211\u662f\u4e2d\u56fd\u4eba
用于方便快捷的中文乱码处理
一叶障树 2009-09-17
  • 打赏
  • 举报
回复
我现在是直接点击链接打开文件的,是不是应该写个servlet,在里面用URLDecoder.decode()解码后再打开呢?
一叶障树 2009-09-17
  • 打赏
  • 举报
回复
英数字的文件名都没有问题的
一叶障树 2009-09-17
  • 打赏
  • 举报
回复
文件名及链接在页面中显示都没有问题,就是点击打开时中文被转成%E6%88%91%E6%98%AF%E4%B8%AD%E5%9B%BD%E4这种东西了
dahaidao 2009-09-17
  • 打赏
  • 举报
回复
最好不用中文的文件名,这样方便一点。
imasmallbird 2009-09-17
  • 打赏
  • 举报
回复
1、首先下载的文件中最好不要有中文名,可以在库中建立两个字段,一个用来存储改名后的文件名(自己起名,不包含中文,做为文件的唯一标识),一个用来存储改名前的文件名,这样下载的时候在URL中写的是自己起的名就可以了
2、如果没有办法改变,要求只能用中文名,那么就要设置编码方式
后台获得文件名后,对其编码:String downloadFileName = java.net.URLEncoder.encode(fileName,"gb2312");
在页面中:<a href="http://...../<%=downloadFileName%>"></a>
mianfeidog 2009-09-17
  • 打赏
  • 举报
回复
ls都说完了,我没什么好说的了。
gesanri 2009-09-17
  • 打赏
  • 举报
回复
你确定不是你的路径的问题吗?就带中文的不行,其它都可以?如果是的话,把中文先URL编码一下,再访问试试,这里有一个别人写的,你试试


import java.io.*;
/**
*
* @author Todd.Zhang
*/
public class URLEncoder {
private static final int MAX_BYTES_PER_CHAR = 10; // rather arbitrary limit, but safe for now
private static boolean[] dontNeedEncoding;
private static String defaultEncName = "";
private static final int caseDiff = ('a' - 'A');
static
{
dontNeedEncoding = new boolean[256];
for (int i='a'; i<='z'; i++)
{
dontNeedEncoding[i] = true;
}
for (int i='A'; i<='Z'; i++)
{
dontNeedEncoding[i] = true;
}
for (int i='0'; i<='9'; i++)
{
dontNeedEncoding[i] = true;
}
dontNeedEncoding[':'] = true;
dontNeedEncoding['/'] = true;
dontNeedEncoding[' '] = true;
dontNeedEncoding['-'] = true;
dontNeedEncoding['_'] = true;
dontNeedEncoding['.'] = true;
dontNeedEncoding['*'] = true;
//defaultEncName = System.getProperty("microedition.encoding");
if(defaultEncName == null || defaultEncName.trim().length() == 0){
defaultEncName = "UTF-8";
}
}
public static final int MIN_RADIX = 2;
public static final int MAX_RADIX = 36;
private URLEncoder() {}
public static String encode(String s)
{
String str = null;
str = encode(s, defaultEncName);
return str;
}

public static String encode(String s,String enc)
{
boolean needToChange = false;
boolean wroteUnencodedChar = false;
StringBuffer out = new StringBuffer(s.length());
ByteArrayOutputStream buf = new ByteArrayOutputStream(MAX_BYTES_PER_CHAR);
OutputStreamWriter writer = null;
try
{
writer = new OutputStreamWriter(buf, enc);
}
catch (UnsupportedEncodingException ex)
{
try
{
writer = new OutputStreamWriter(buf,defaultEncName);
}
catch (UnsupportedEncodingException e)
{
//never reach
}
}
for (int i = 0; i < s.length(); i++)
{
int c = (int) s.charAt(i);
if (c<256 && dontNeedEncoding[c])
{
out.append((char) (c==' ' ? '+' : c));
wroteUnencodedChar = true;
}
else
{
// convert to external encoding before hex conversion
try
{
if (wroteUnencodedChar)
{ // Fix for 4407610
writer = new OutputStreamWriter(buf,enc);
wroteUnencodedChar = false;
}
if(writer != null)
writer.write(c);
/*
* If this character represents the start of a Unicode
* surrogate pair, then pass in two characters. It's not
* clear what should be done if a bytes reserved in the
* surrogate pairs range occurs outside of a legal surrogate
* pair. For now, just treat it as if it were any other
* character.
*/
if (c >= 0xD800 && c <= 0xDBFF)
{
if ((i + 1) < s.length())
{
int d = (int) s.charAt(i + 1);
if (d >= 0xDC00 && d <= 0xDFFF)
{
writer.write(d);
i++;
}
}
}
writer.flush();
}
catch (IOException e)
{
buf.reset();
continue;
}
byte[] ba = buf.toByteArray();
for (int j = 0; j < ba.length; j++)
{
out.append('%');
char ch = toHex((ba[j] >> 4) & 0xF,16);
// converting to use uppercase letter as part of
// the hex value if ch is a letter.
if (isLetter(ch))
{
ch -= caseDiff;
}
out.append(ch);
ch = toHex(ba[j] & 0xF,16);
if (isLetter(ch))
{
ch -= caseDiff;
}
out.append(ch);
}
buf.reset();
needToChange = true;
}
}
return (needToChange? out.toString() : s);
}
private static char toHex(int digit,int radix)
{
if ((digit >= radix) || (digit < 0))
{
return '\0';
}
if ((radix < MIN_RADIX) || (radix > MAX_RADIX))
{
return '\0';
}
if (digit < 10)
{
return (char)('0' + digit);
}
return (char)('a' - 10 + digit);
}
private static boolean isLetter(char c)
{
if( (c >= 'a' && c <= 'z') || (c >='A' && c <= 'Z'))
return true;
return false;
}
}

你调用URLEncoder.encode(url);后再访问

81,122

社区成员

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

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