if (formFile != null) {
byte[] bytes  = new byte[2*1024];
InputStream in = formFile.getInputStream();
int c = in.read(bytes, 0, bytes.length);

ServletContext servletContext = this.getServlet()
.getServletContext();
String path = servletContext.getRealPath("/fuyou");
String fileName = formFile.getFileName();
System.out.println(new String(fileName.getBytes("ISO-8859-1"),"GB2312"));
System.out.println(formFile.getFileSize()); //打出来的还文件名不是乱码,我提交jSP的编码是:ISO-8859-1
File upLoadFile = new File(path, fileName);
    
RandomAccessFile raf = new RandomAccessFile(upLoadFile, "rw");
while(c!=-1){
raf.write(bytes, 0, bytes.length);
 c = in.read(bytes, 0, bytes.length);
}
raf.close(); }
页面的编码:<%@ page language="java" pageEncoding="ISO-8859-1"%>
上传上去的文件名和打印出来都是乱码?

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【fuyou001】截止到2008-07-28 12:10:33的历史汇总数据(不包括此帖):
    发帖的总数量:164                      发帖的总分数:4445                     每贴平均分数:27                       
    回帖的总数量:1003                     得分贴总数量:380                      回帖的得分率:37%                      
    结贴的总数量:164                      结贴的总分数:4445                     
    无满意结贴数:19                       无满意结贴分:745                      
    未结的帖子数:0                        未结的总分数:0                        
    结贴的百分比:100.00%               结分的百分比:100.00%                  
    无满意结贴率:11.59 %               无满意结分率:16.76 %                  
    敬礼!
      

  2.   

    版本是1.2
    我在strutsapi没找FormFile类的api
      

  3.   

    ISO-8859-1不支持中文
    页面改成gb2312,或者是utf-8<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>后台String fileName = new String(fileName.getBytes("ISO-8859-1"),"UTF-8");
      

  4.   

    System.out.println(new String(fileName.getBytes("GB2312"), "GB2312"));
    打印出来的: System.out.println(request.getCharacterEncoding());//结果是null
    <%@ page language="java"  contentType="text/html; charset=GB2312" pageEncoding="GB2312"%还是有乱码
    还有一个问题我的一文件374M,成功跳转到成功的页面,但目录没有相应的文件?
      

  5.   

    我参考网上的例子,写了过Filter:public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain chain) throws IOException, ServletException {
    String encoding = "GB2312";
        request.setCharacterEncoding(encoding);
        System.out.println("ssssssssssss");
        response.setContentType("text/html;charset=" + encoding + ""); //不知道这句话有什么意思,传文件啊,没必要设置吧?
        chain.doFilter(request,response);

    }
    <filter>
        <filter-name>Encoding</filter-name>
        <filter-class>com.fuyou.util.FirsterFilter</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>Encoding</filter-name>
        <url-pattern>*.do</url-pattern>
      </filter-mapping>
      <filter-mapping>
        <filter-name>Encoding</filter-name>
        <url-pattern>*.jsp</url-pattern>
      </filter-mapping>还是出现了乱码?快疯了
      

  6.   

    文件名乱码,已解决,设置成GBK就好了
      

  7.   

    原来已经有过滤器了,那转码就没必要了,只要前台是gbk就行了
      

  8.   

    解决Struts,formfile下,上传文件时,文件名和表单其他属性项的值乱码的最终解决方案:
    第一步:保证你的jsp的内容中的encoding为:<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
    第二步:确认你的jsp页面另存为时的格式为"ANSI"
    第三步:在你的action中加上
    ///////////begin///////
    request.setCharacterEncoding("utf-8");
    response.setContentType("text/html;charset=utf-8");
    ////////////end////////
    第四步:从form中取值时,不要再转成别的编码格式,直接取OK了。