Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Servlet/JSP/JSF/JavaFX Script  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
reply to topicflat modethreaded modego to previous topicgo to next topicgo to back
作者 请教一个中文名图片不能显示的问题?
空心菜





发贴: 214
于 2005-03-01 11:01 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
比如我上传了一张中文名字的照片,上传没有问题,在数据库中保存的也是正确的中文名字,
在浏览器的地址是这样的
http://localhost:8080/upload/%E7%85%A7%E7%89%87.jpg
可是却提示
type Status report

message /upload/%E7%85%A7%E7%89%87.jpg

description The requested resource (/upload/%E7%85%A7%E7%89%87.jpg) is not available.
请问有人解决了吗?我没有搜到
还请提示一二



作者 Re:请教一个中文名图片不能显示的问题? [Re:空心菜]
floater

Java Jedi

总版主


发贴: 3233
于 2005-03-01 22:38 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
The % string is url encoded, you need to decode this string.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler, Refactoring - Improving the Design of Existing Code
作者 Re:请教一个中文名图片不能显示的问题? [Re:空心菜]
空心菜





发贴: 214
于 2005-03-04 16:08 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
明白了
多谢指点Big Smile



作者 Re:请教一个中文名图片不能显示的问题? [Re:空心菜]
空心菜





发贴: 214
于 2005-03-10 15:55 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我找了找发现还是不知怎么decode
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html>
<head>
<title>URL Decoder/Encoder</title>
<style type="text/css">
<!--
body {background: white; color: black;}
form {margin: 0;}
h1 {font-family: Arial, sans-serif; line-height: 0.85em; border-bottom: 2px solid; margin-bottom: 0.33em; padding-bottom: 0;}
textarea {background: #EEF;}
#footer {border-top: 1px solid #000; color: #999; font: italic 75% sans-serif;}
#footer p {margin: 0 0 1em 0;}
#footer img {float: right; margin: 0 0 0.5em 2em;}
-->
</style>
<script type="text/javascript">
function encode() {
  var obj = document.getElementById('dencoder');
  var unencoded = obj.value;
  obj.value = escape(unencoded);
}
function decode() {
  var obj = document.getElementById('dencoder');
  var encoded = obj.value;
  obj.value = unescape(encoded.replace(/\+/g, " "));
}
</script>
</head>
<body><form onsubmit="return false;"><h1>URL Decoder/Encoder</h1>
<textarea cols="80" rows="20" id="dencoder"></textarea>
<div><input type="button" onclick="decode()" value="Decode">
<input type="button" onclick="encode()" value="Encode"></div>
<ul><li>Input a string of text and encode or decode it as you like.</li>
<li>Handy for turning encoded JavaScript URLs from complete gibberish into readable gibberish.</li>
<li>If you'd like to have the URL Decoder/Encoder for offline use, just view source and save to your hard drive.</li></ul></form>
</body>
</html>
这个是我找的,不管用啊,encode的字符都不一致
还是要请教floater 一下Blush



作者 Re:请教一个中文名图片不能显示的问题? [Re:空心菜]
chengbd



版主


发贴: 687
于 2005-03-10 17:38 user profilesend a private message to usersend email to chengbdreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
decode应该是tomcat来完成的工作,应该配置tomcat


作者 Re:请教一个中文名图片不能显示的问题? [Re:空心菜]
chengbd



版主


发贴: 687
于 2005-03-10 17:50 user profilesend a private message to usersend email to chengbdreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
tomcat不能显示中文原因主要是编码的问题,
因为Tomcat5的http Connector所用的URI解码默认用的是 ISO-8859-1,
而一般浏览器默认用的发送编码为UTF-8,这样问题就出现了,
初步的解决方法如下:
在server.xml中类似如下配置:
< Connector port="8080" maxThreads="150" minSpareThreads="25"
maxSpareThreads="75" enableLookups="false" redirectPort="8443"
acceptCount="100" debug="0" connectionTimeout="20000"
disableUploadTimeout="true" URIEncoding="UTF-8" charset="gb2312"/ >
其中关键是 URIEncoding="UTF-8" 这项,其含义是指定URI的编码为:UTF-8这样配置后重启Tomcat,基本能解决前面提到的中文路径和中文文件名问题。



作者 Re:请教一个中文名图片不能显示的问题? [Re:空心菜]
floater

Java Jedi

总版主


发贴: 3233
于 2005-03-10 23:06 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
check the java.net.URLDecoder class to see whether you can use that.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler, Refactoring - Improving the Design of Existing Code
作者 Re:请教一个中文名图片不能显示的问题? [Re:空心菜]
空心菜





发贴: 214
于 2005-03-17 15:25 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list


import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class SetCharacterEncodingFilter implements Filter {

  protected String encoding = null;

  protected FilterConfig filterConfig = null;

  protected boolean ignore = true;

  public void destroy() {

    this.encoding = null;
    this.filterConfig = null;

  }

  /**
   * @param request
   * The servlet request we are processing
   * @param result
   * The servlet response we are creating
   * @param chain
   * The filter chain we are processing
   *
   * @exception IOException
   * if an input/output error occurs
   * @exception ServletException
   * if a servlet error occurs
   */
  public void doFilter(ServletRequest request, ServletResponse response,
      FilterChain chain) throws IOException, ServletException {

    // Conditionally select and set the character encoding to be used
    if (ignore || (request.getCharacterEncoding() == null)) {
      String encoding = selectEncoding(request);
      if (encoding != null) {
        request.setCharacterEncoding(encoding);
        HttpServletRequest hr = (HttpServletRequest) request;
        System.out.println(hr.getRequestURI());
        System.out.println(URLDecoder.decode(hr.getRequestURI(),
            "utf-8"));
      }
    }
    chain.doFilter(request, response);
  }

  /**
   * Place this filter into service.
   *
   * @param filterConfig
   * The filter configuration object
   */
  public void init(FilterConfig filterConfig) throws ServletException {

    this.filterConfig = filterConfig;
    this.encoding = filterConfig.getInitParameter("encoding");
    String value = filterConfig.getInitParameter("ignore");
    if (value == null)
      this.ignore = true;
    else if (value.equalsIgnoreCase("true"))
      this.ignore = true;
    else if (value.equalsIgnoreCase("yes"))
      this.ignore = true;
    else
      this.ignore = false;

  }

  /**
   * @param request
   * The servlet request we are processing
   */
  protected String selectEncoding(ServletRequest request) {

    return (this.encoding);
  }
}

经过chengbd 和floater的提示
chengbd 的不管用,或许是tomcat的问题
然后我试了试floater的
这是我加的一个filter,处理/*
我加了两句输出,输出结果入下
/upload/%E5%9B%BE%E7%89%87.jpg
/upload/图片.jpg
而且很奇怪,在这里智能用utf-8,如果用GBK的话,第二行输出就会乱码,如果用iso-8859-1的话就会问号,encoding的init value为GBK
还请多多指点!!!!



作者 Re:请教一个中文名图片不能显示的问题? [Re:空心菜]
chengbd



版主


发贴: 687
于 2005-05-26 09:49 user profilesend a private message to usersend email to chengbdreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
:) 我用这个办法能在IE中解析中文路径。

chengbd edited on 2005-05-26 10:15

知难而进
作者 Re:请教一个中文名图片不能显示的问题? [Re:空心菜]
chengbd



版主


发贴: 687
于 2005-05-26 10:13 user profilesend a private message to usersend email to chengbdreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我用的是tomcat 5.0.29,在没有修改前,在IE中访问:http://localhost:8080/testweb/images/猫.gif,提示:
type Status report

message /testweb/images/%E7%8C%AB.gif

description The requested resource (/testweb/images/%E7%8C%AB.gif) is not available

修改后,就可以在IE中正常访问了。

有些不明白楼主最终要达到什么目的,是只要在IE中显示,还是要用代码来访问这些资源。建议写个演示的完整的web程序,放在附件中以供大家测试。



知难而进
作者 Re:请教一个中文名图片不能显示的问题? [Re:空心菜]
jiehaorou





发贴: 3
于 2005-08-07 14:09 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
String ct=new String("图片名");
String picName=new String(ct.getBytes("GBK"),"ISO-8859-1");
将中文字符编码了,应该可以
可以试一下


jiehaorou edited on 2005-08-07 14:12

作者 Re:请教一个中文名图片不能显示的问题? [Re:空心菜]
mfc42d





发贴: 116
于 2005-08-10 13:55 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
http://blogger.org.cn/blog/more.asp?name=hongrui&id=7557


作者 Re:请教一个中文名图片不能显示的问题? [Re:空心菜]
mfc42d





发贴: 116
于 2005-08-10 13:56 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
使用Tomcat 5.0.20,我们使用Form submit 的数据將会以ISO8859-1处理,我们必须自己将字符串转换为GB2312/GBK(简体中文),在web程序中,对所有的 request.getParameter("xx"); 作了 toGBKString() 的处理,发现还是出现了中文问题,中文还是可能变成乱码!
经过分析,发现问题出在 QueryString的处理上,在使用 Tomcat 4.x时,对于 SUBMIT 時无论采用 GET or POST,Tomcat server 对 parameters 的处理都采用ISO8859-1处理,但在 Tomcat 5.x 版,将get请求独立出来,如果Form 的 Method 採用 GET 及或者在 URL 上的写中文,上传到 Tomcat时,无论如何转码,都是乱码,即使使用 URLEncode结果也一样。
通过研究tomcat的文档可以找到解决办法,在$TOMCAT_HOME/webapps/tomcat-docs/config/http.html 中写道
URIEncoding:

This specifies the character encoding used to decode the URI bytes, after %xx decoding the URL. If not specified, ISO-8859-1 will be used.

useBodyEncodingForURI:

This specifies if the encoding specified in contentType should be used for URI query parameters, instead of using the URIEncoding. This setting is present for compatibility with Tomcat 4.1.x, where the encoding specified in the contentType, or explicitely set using Request.setCharacterEncoding method was also used for the parameters from the URL. The default value is false
以上 Tomcat 参数,是设定在 server.xml 中的 http <Connector />中,必须设定这两个参数其中之一。
URIEncoding 设定为 URIEncoding="ISO-8859-1" 指定为 "ISO-8859-1" 编码,让 QueryString 的编码与 post body 相同。
useBodyEncodingForURI 用来兼容 Tomcat 4.x 版的,值是 "true" or "false",指 "要不要让 QueryString 与 POST BODY 采用相同的编码 ",设成 true,就可以做到 "ISO-8859-1" 编码。
建议采用 URIEncoding 的设定,因为 useBodyEncodingForURI是为了兼容 Tomcat 4.X。不过按照原文的说明,这两个参数都不设,Tomcat 也应该采用 "ISO-8859-1" 的编码,为什末还是有问题呢?
只好看 Tomcat Source Code了
在org.apache.tomcat.util.http.Parameters类,Tomcat用来处理QueryString,
private String urlDecode(ByteChunk bc, String enc)
throws IOException {
if( urlDec==null ) {
urlDec=new UDecoder();
}
urlDec.convert(bc);
String result = null;
if (enc != null) {
bc.setEncoding(enc);
result = bc.toString();
} else {
CharChunk cc = tmpNameC;
cc.allocate(bc.getLength(), -1);
// Default encoding: fast conversion
byte[] bbuf = bc.getBuffer();
char[] cbuf = cc.getBuffer();
int start = bc.getStart();
for (int i = 0; i < bc.getLength(); i++) {
cbuf[i] = (char) (bbuf[i + start] & 0xff);
}
cc.setChars(cbuf, 0, bc.getLength());
result = cc.toString();
cc.recycle();
}
return result;
}
tomcat处理 QueryString时,如果没有设定 encode,并没有采用 ISO-8859-1 的编码,而是用 fast conversion 来处理,才会造成中文问题,所以必须在 Server.xml 中加上 URLEncoding 的参数才行.
Connector 的设定
<Connectordebug="0"acceptCount="100"connectionTimeout="20000"disableUploadTimeout="true"port="80"redirectPort="8443"enableLookups="false"minSpareThreads="25"maxSpareThreads="75"maxThreads="150"maxPostSize="0"URIEncoding="ISO-8859-1"></Connector>
所以在使用 Tomcat 4 通过 GET or POST 的方式传参数时,通常都是使用 Filter 的方式解决中文传参数的问题。
但是到了 Tomcat 5.0.20 之后,解决中文传参数的问题,就必须考虑是使用 GET or POST,两种的方式不同。
使用 GET 的方式
String name = new String((request.getParameter("name")).getBytes("ISO-8859-1"),"GBK");
使用 POST 的方式
request.setCharacterEncoding("GBK");

如果设定URIEncoding="UTF-8"和使用 IE, 设定总是以 UTF8发送URL,你还可以用<img src="我的图片.jpg" />

使用Filter的处理 :先判断是使用那种方式( GET or POST),假若是用 GET 的方式就采用第一个;若使用POST 方式,就采用第二个

HttpServletRequest httpServletRequest = (HttpServletRequest) request;
if (httpServletRequest.getMethod().equals("POST"))
{ request.setCharacterEncoding("UTF-8");}
else if (httpServletRequest.getMethod().equals("GET"))
{
//进行处理
}
注意spring的filter病没有解决这个问题,以下是他的代码
protected void doFilterInternal(
HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {

if (this.forceEncoding || request.getCharacterEncoding() == null) {
request.setCharacterEncoding(this.encoding);
}
filterChain.doFilter(request, response);
}



作者 Re:请教一个中文名图片不能显示的问题? [Re:空心菜]
bluecrystal





发贴: 2788991
于 2005-09-28 09:50 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
呵呵
我的建议是基于web的程序,在文件命名上不要用中文



Just Software & Travel
-- 我的blog -- 技术点滴/经验分享
作者 Re:请教一个中文名图片不能显示的问题? [Re:bluecrystal]
javadd





发贴: 736
于 2005-11-22 18:55 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
bluecrystal wrote:
呵呵
我的建议是基于web的程序,在文件命名上不要用中文


用户绝对要上传中文文件的.




作者 Re:请教一个中文名图片不能显示的问题? [Re:javadd]
dy810810





发贴: 7
于 2005-12-11 16:44 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
javadd wrote:
用户绝对要上传中文文件的.

用户要是上传一个jsp文件呢?
还是写个程序专门来读取文件进行下载吧。




reply to topicflat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923