`
sinye
  • 浏览: 217559 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

jspsmartupload utf8支持

    博客分类:
  • java
阅读更多

相信很多朋友都用过这个上传下载组件,但是,这个组件默认使用的编码是gb2312的,因此,如果你的页面是utf-8的,就会出现乱码等问题。在这里,修改了一下里面的方法,这样可以支持utf-8编码的上传和下载。感兴趣的朋友可以按照下面的方法自己做一个,嫌麻烦的朋友直接去下面下载用就可以了。

具体修改步骤如下:

(1)修改SmartUpload类下的upload()方法

找到这一句,修改为下面包含utf-8的语句。 注意加粗部分

 

//String s11 = new String(m_binArray, m_startData, (m_endData - m_startData) + 1);

String s11 = new String(m_binArray, m_startData, (m_endData - m_startData) + 1,"UTF-8");

 

(2)修改SmartUpload类下的getDataHeader()方法。注意加粗部分

找到这一句:String s = new String(m_binArray, i, (j - i) + 1);修改如下:

 

//String s = new String(m_binArray, i, (j - i) + 1);
String s=null;
try{
     String encode=m_response.getCharacterEncoding();
     if(encode.equalsIgnoreCase("UTF-8")){
             s = new String(m_binArray, i, (j - i) + 1,"UTF-8");
     }else{
             s = new String(m_binArray, i, (j - i) + 1);
     }
}catch(UnsupportedEncodingException e){
      e.printStackTrace();
}
return s;

  到此,就解决了utf-8编码的上传问题。接下来是utf-8编码的下载问题,修改如下:

 (1)增加一个方法Utf8String(String s)

 

/**
             * 将文件名中的汉字转为UTF8编码的串,以便下载时能正确显示另存的文件名.
             * sinye修改于2010年1月29日
             * @param s 原文件名
             * @return 重新编码后的文件名
             */
            public static  String Utf8String(String s)
        	{
        		StringBuffer sb = new StringBuffer();
        		for (int i=0;i<s.length();i++)
        		{
        				char c = s.charAt(i);
        				if (c >= 0 && c <= 255)
        				{
        					sb.append(c);
        				}
        				else
        				{
        						byte[] b;
        						try
        						{
        							b = Character.toString(c).getBytes("utf-8");
        						}
        						catch (Exception ex)
        						{
        							System.out.println(ex);
        							b = new byte[0];
        						}
        						for (int j = 0; j < b.length; j++)
        						{
        							int k = b[j];
        							if (k < 0) k += 256;
        							sb.append("%" + Integer.toHexString(k).toUpperCase());
        						}
        				}
        		}
        			return sb.toString();
        	}

 

  (2)修改downloadFile(String s, String s1, String s2, int i)方法,注意加粗部分

 

public void downloadFile(String s, String s1, String s2, int i) throws ServletException, IOException, SmartUploadException {
/* 528*/        if (s == null) {
/* 528*/            throw new IllegalArgumentException("File '" + s + "' not found (1040).");
                }
/* 531*/        if (s.equals("")) {
/* 531*/            throw new IllegalArgumentException("File '" + s + "' not found (1040).");
                }
/* 534*/        if (!isVirtual(s) && m_denyPhysicalPath) {
/* 535*/            throw new SecurityException("Physical path is denied (1035).");
                }
/* 539*/        if (isVirtual(s)) {
/* 539*/            s = m_application.getRealPath(s);
                }
/* 544*/        File file = new File(s);
/* 545*/        FileInputStream fileinputstream = new FileInputStream(file);
/* 547*/        long l = file.length();
/* 548*/        boolean flag = false;
/* 549*/        int k = 0;
/* 550*/        byte abyte0[] = new byte[i];
/* 553*/        if (s1 == null) {
/* 554*/            m_response.setContentType("application/x-msdownload");
                } else
/* 555*/        if (s1.length() == 0) {
/* 556*/            m_response.setContentType("application/x-msdownload");
                } else {
/* 558*/            m_response.setContentType(s1);
                }
/* 561*/        m_response.setContentLength((int)l);
/* 563*/        m_contentDisposition = m_contentDisposition != null ? m_contentDisposition : "attachment;";
/* 567*/        if (s2 == null) {
/* 567*/            m_response.setHeader("Content-Disposition", m_contentDisposition + " filename=" + Utf8String(getFileName(s)));
                } else
/* 569*/        if (s2.length() == 0) {
/* 570*/            m_response.setHeader("Content-Disposition", m_contentDisposition);
                } else {
/* 572*/            m_response.setHeader("Content-Disposition", m_contentDisposition + " filename=" + Utf8String(s2));
                }
/* 577*/        while ((long)k < l)  {
/* 577*/            int j = fileinputstream.read(abyte0, 0, i);
/* 578*/            k += j;
/* 579*/            m_response.getOutputStream().write(abyte0, 0, j);
                }
/* 581*/        fileinputstream.close();
            }

 这样,下载时的utf-8编码问题就解决了。然后自己再打成jar文件就ok了。

 

 

另外,我们在下载的时候会出现这种错误:org.apache.jasper.JasperException: java.lang.IllegalStateException: getOutputStream() has already been called for this response

解决办法,见代码:

 

String file = request.getParameter("file"); 
su.downloadFile(file); 


//解决 java.lang.IllegalStateException: 
//getOutputStream() has already been called for this response 
//这个问题 
out.clear(); 
out=pageContext.pushBody(); 

 由于jsp container在处理完成请求后会调用releasePageContet方法释放所用的PageContext object,
并且同时调用getWriter方法,由于getWriter方法与在jsp页面中使用流相关的getOutputStream方法冲突,所以会造成这种异常,解决 办法是:只需要在jsp页面的最后加上两条语句:  
out.clear();
out=pageContext.pushBody();即可(其中out,pageContext均为jsp内置对象!)

 

分享到:
评论
5 楼 zhyouyouzh 2010-04-15  
老大,我试过了你的jar包,还是会乱码。
反编译也是一样,迷茫中!
4 楼 sinye 2010-02-01  
georgejay 写道
感谢楼主的无私分享,  现在怎么都下载不了东西? 这个老提示5秒钟未必也太欺骗人了!
jiang-jay818@163.com 不知百忙之中是否可以发我一份, 不胜感激!

试了下,还真是不行,不知道为什么提示后,却不跳转,邮件已发送了,希望对你能有帮助!
3 楼 sinye 2010-02-01  
魔力猫咪 写道
第一,该组件早已经停止开发,网站也已经关闭。需要这类开源组件可以选择Apache的。
第二,虽然该组件可以免费使用,但是并不是开源软件。反编译涉嫌违法。

也是,当时没想到那么多,只想着能帮助大家解决使用这个组件的utf-8编码问题,毕竟大家都是奔着简单,好用的原则去用的,如果有这一点瑕疵,为什么不去改正它呢!
2 楼 魔力猫咪 2010-01-31  
第一,该组件早已经停止开发,网站也已经关闭。需要这类开源组件可以选择Apache的。
第二,虽然该组件可以免费使用,但是并不是开源软件。反编译涉嫌违法。
1 楼 georgejay 2010-01-31  
不胜感激!

相关推荐

    jspsmartupload支持UTF-8版本

    相信很多朋友都用过这个上传下载组件,但是,这个组件默认使用的编码是gb2312的,因此,如果你的页面是utf-8的,就会出现乱码等问题。在这里,修改了一些方法,这样可以支持utf-8编码的上传和下载。

    jspSmartupload组件

    原始的jspSmartupload组件不支持中文上传,所以我把这个源码进行修改后,现在可以支持中文上传了,你的工程也必须是在统一的utf-8的编码环境下,因为我做的修改,就是将中文用utf-8进行编码。 我只测试了上传功能,...

    (支持UTF-8)---- jspSmartUpload.jar

    支持UTF-8的上传附件的jar包;支持UTF-8,附件上传的jar包。

    jspsmartupload支持中文下载

    原jspSmartUpload组件对返回的文件未作任何处理,现在做了编码的转换工作,将文件名转换为utf-8形式的编码形式从而修复了了原jar包在下载时对中文乱码的问题。 经测试修改后的jar包名为utf8jspsmartupload.jar,使用...

    jspsmartupload上传下载,已修改过源代码!

    觉得是jspSmartUpload组件对中文支持不足的问题。 http://ru-yi86.javaeye.com/blog/368553 Java代码 1. public void upload() 2. throws SmartUploadException, IOException, ServletException 3. { 4. ...

    支持中文的jspsmartupload

    下载后,放到tomcat等服务器的lib目录中即可,也可以改名为jspsmartupload.jar(或任意名字) 使用说明: 默认编码为UTF-8,若您的系统是其他编码的,请选用我的指定编码函数: 1.上传默认方法upload(),指定编码...

    jspsmartupload.rar

    支持UTF-8编码的smartupload

    jspsmartupload.jar

    经过自己一段时间的深入了解smartupload,得知官方的不支持utf-8版,故自己更改,可以完全的解决smartupload乱码的问题。

    jspSmartUpload.jar

    绝对支持中文,包括utf-8!在网上找了好久,拿来分享哈!

    SmartUpload 用于JDK1.4(中文终极解决版,包括中文文件名上传后乱码,中文参数,下载)

    下载后,放到tomcat等服务器的lib目录中即可,也可以改名为jspsmartupload.jar(或任意名字) 使用说明: 默认编码为UTF-8,若您的系统是其他编码的,请选用我的指定编码函数: 1.上传默认方法upload(),指定编码...

    jsp文件上传插件smartupload

    jsp文件上传插件smartupload(支持两种格式UTF-8&GBK;版)

    SmartUpload 用于JDK1.5(中文终极解决版,包括中文文件名上传后乱码,中文参数,下载)

    下载后,放到tomcat等服务器的lib目录中即可,也可以改名为jspsmartupload.jar(或任意名字) 使用说明: 默认编码为UTF-8,若您的系统是其他编码的,请选用我的指定编码函数: 1.上传默认方法upload(),指定编码...

    jsp上传组件smartUpload_auto_jar包

    jsp上传组件smartUpload_auto_jar包,解决任意思中文编码的乱码问题,在调用该组件的地方设置一下编码与页面的编码格式一致即,支持gb2312,gbk,utf-8等中文编码格式,从而可以解决多个编码方式造成的上传乱码

    可以显示中文名称的下载组件

    jspsmartupload.jar组件大家都知道,但是它本身自带的download功能并不支持中文名称的文件,在下载的时候会出现乱码,我自己编写了一个FileDownload类,放到了这个jar包中,这个类用的UTF-8编码方式,所以可以对中文...

    修改过的jspsmart包

    可支持多种编码格式,设置如下 SmartUpload su=new SmartUpload(); su.setCharset("GBK"); 默认编码格式是UTF-8

    163编辑器带上传本地图片功能JSP版

    把此文件夹放在根目录下直接运行就行了,采用UTF-8编码,支持全中文图片名称,上传采用SMARTUPLOAD组件 支持本地图片上传,点编辑器中的添加连接就行了,就是把添加添连接改成添加本地图片上传了 完全兼容IE678,...

Global site tag (gtag.js) - Google Analytics