Available categories: [/] [development. ~]
一个奇怪的中文编码问题的粗暴解决办法 [Permalink] Fri Jan 21 08:14:45 CST 2005 老系统中文编码有问题。如果不设置contentType为 public class HtmlMimeResponseWrapper extends HttpServletResponseWrapper { //.... public PrintWriter getWriter() throws IOException { PrintWriter writer = super.getWriter(); MyWriter myWriter = new MyWriter(writer); return myWriter; } //... }没错!就是用一个filter,把他的PrintWriter包裹起来。decorator里面decorate,哈哈。。PrintWriter是一个内部类,细节如下: class MyWriter extends PrintWriter { PrintWriter writer; public MyWriter(PrintWriter writer) { super(writer); this.writer = writer; } // 关键在这里! public void write(String buf) { String newbuf = null; try { newbuf = new String(buf.getBytes("iso-8859-1")); } catch (UnsupportedEncodingException ex) { } writer.write(newbuf); } public boolean checkError() { return writer.checkError(); } public void print(Object obj) { writer.print(obj); } public void print(String s) { writer.print(s); } public void print(boolean b) { writer.print(b); } public void print(char c) { writer.print(c); } public void print(char[] s) { writer.print(s); } public void print(double d) { writer.print(d); } public void print(float f) { writer.print(f); } public void print(int i) { writer.print(i); } public void print(long l) { writer.print(l); } public void println() { writer.println(); } public void println(Object x) { writer.println(x); } public void println(String x) { writer.println(x); } public void println(boolean x) { writer.println(x); } public void println(char x) { writer.println(x); } public void println(char[] x) { writer.println(x); } public void println(double x) { writer.println(x); } public void println(float x) { writer.println(x); } public void println(int x) { writer.println(x); } public void println(long x) { writer.println(x); } public void close() { writer.close(); } public void flush() { writer.flush(); } public void write(String str, int off, int len) { writer.write(str, off, len); } public void write(char[] cbuf) { writer.write(cbuf); } public void write(char[] cbuf, int off, int len) { String s=new String(cbuf, off, len); this.write(s); } public void write(int c) { writer.write(c); } }现在把问题从头掐了。虽然粗暴了一点,不过效果达到了。对于那些没有时间深究,或者懒得深究的问题,用这样的方法还是不错的。 另外值得一提的是jb的Delegate member wizard功能。通过他可以快速的作出一个Wrapper类,不用把super class的方法都一个一个去机械的实现,很是省力。只不过以前的jb里,这个功能处于Wizard主菜单,而现在则藏到了Edit下。。害我一顿好找
Posted by: miles |
? |
Available categories: [/] [development. ~]
html hits:?51246