jspSmartUpload组件上传下载文件时中文乱码问题(转)

    这次用jspSmartUpload做文件上传下载,该组件默认是GBK编码,当上传的文件名为中文时,我将文件名getBytes()下,将GBK改成 UTF-8。测试了下,貌似没问题,突然有一次上传一文件时,发现最后几个字乱码,一直是??。在拿些文件测试,后来知道了是当文件名为中文奇数时,会乱 码,而且还上传不了。再做测试,找原因,查看字符的长度,转成16进制看结果。觉得是jspSmartUpload组件对中文支持不足的问题。

    在网上找解决方法。下了几个jar,都说解决中文乱码,彻底解决中文乱码!!!用到我这就都不管用,而且也看到很多回复说解决不了,乱码问题还是依旧!!!

    这次自己找到jspSm的源代码,参考了网上的意见,自己稍微修改了下。发现能解决中文乱码问题,分享给和我遇到同样问题的朋友们!!!

 

        1:当页面上传有参数时,中文会乱码,解决页面上中文参数乱码方法:

            修改类SmartUpload()中的upload()方法         

public void upload() throws ServletException,IOException,SmartUploadException {
        int i = 0;
        //boolean flag = false;
        boolean flag1 = false;
        //boolean flag2 = false;
        long l = 0L;
        //String s = "";//new String();
        //String s2 = "";//new String();
        String s4 = ""; //new String();
        String s5 = ""; //new String();
        String s6 = ""; //new String();
        String s7 = ""; //new String();
        String s8 = ""; //new String();
        String s9 = ""; //new String();
        String s10 = ""; //new String();
        m_totalBytes = m_request.getContentLength();
        m_binArray = new byte[m_totalBytes];
        int j;
        for(;i < m_totalBytes;i += j)
        {
            try
            {
                m_request.getInputStream();
                j = m_request.getInputStream().read(m_binArray,i,m_totalBytes - i);
            }
            catch(Exception exception)
            {
                throw new SmartUploadException("Unable to upload.");
            }
        }

        for(;!flag1 && m_currentIndex < m_totalBytes;m_currentIndex++)
        {
            if(m_binArray[m_currentIndex] == 13)
            {
                flag1 = true;
            }
            else
            {
                m_boundary = m_boundary + (char)m_binArray[m_currentIndex];
            }
        }
        if(m_currentIndex == 1)
        {
            return;
        }
        for(m_currentIndex++;m_currentIndex < m_totalBytes;m_currentIndex = m_currentIndex + 2)
        {
            String s1 = getDataHeader();
            m_currentIndex = m_currentIndex + 2;
            boolean flag3 = s1.indexOf("filename") > 0;
            String s3 = getDataFieldValue(s1,"name");
            if(flag3)
            {
                s6 = getDataFieldValue(s1,"filename");
                s4 = getFileName(s6);
                s5 = getFileExt(s4);
                s7 = getContentType(s1);
                s8 = getContentDisp(s1);
                s9 = getTypeMIME(s7);
                s10 = getSubTypeMIME(s7);
            }
            getDataSection();
            if(flag3 && s4.length() > 0)
            {
                if(m_deniedFilesList.contains(s5))
                {
                    throw new SecurityException("The extension of the file is denied to be uploaded (1015).");
                }
                if(!m_allowedFilesList.isEmpty() && !m_allowedFilesList.contains(s5))
                {
                    throw new SecurityException("The extension of the file is not allowed to be uploaded (1010).");
                }
                if(m_maxFileSize > 0L && (long)((m_endData - m_startData) + 1) > m_maxFileSize)
                {
                    throw new SecurityException("Size exceeded for this file : " + s4 + " (1105).");
                }
                l += (m_endData - m_startData) + 1;
                if(m_totalMaxFileSize > 0L && l > m_totalMaxFileSize)
                {
                    throw new SecurityException("Total File Size exceeded (1110).");
                }
            }
            if(flag3)
            {
                SmartFile file = new SmartFile();
                file.setParent(this);
                file.setFieldName(s3);
                file.setFileName(s4);
                file.setFileExt(s5);
                file.setFilePathName(s6);
                file.setIsMissing(s6.length() == 0);
                file.setContentType(s7);
                file.setContentDisp(s8);
                file.setTypeMIME(s9);
                file.setSubTypeMIME(s10);
                if(s7.indexOf("application/x-macbinary") > 0)
                {
                    m_startData = m_startData + 128;
                }
                file.setSize((m_endData - m_startData) + 1);
                file.setStartData(m_startData);
                file.setEndData(m_endData);
                m_files.addFile(file);
            }
            else
            {
                /**
                 * 原来的代码,当页面上要传入中文参数时,乱码。
                 */
                //String s11 = new String(m_binArray,m_startData,(m_endData - m_startData) + 1);
         
                /**
                  * 2008-10-28 carton修改,解决页面传入中文参数乱码
                  */

                String s11 = new String(m_binArray,m_startData,(m_endData - m_startData) + 1, "utf-8");
                m_formRequest.putParameter(s3,s11);
            }
            if((char)m_binArray[m_currentIndex + 1] == '-')
            {
                break;
            }
        }
    }

 

 

 

    2:当上传时文件名为中文时,解决中文乱码

 

    修改类SmartUpload()中的getDataHeader()方法

private String getDataHeader()
    {
        //boolean flag = false;
        int i = m_currentIndex;
        int j = 0;
        for(boolean flag1 = false;!flag1;)
        {
            if(m_binArray[m_currentIndex] == 13 && m_binArray[m_currentIndex + 2] == 13)
            {
                flag1 = true;
                j = m_currentIndex - 1;
                m_currentIndex = m_currentIndex + 2;
            }
            else
            {
                m_currentIndex++;
            }
        }

        /**
         * 原代码,当上传文件名为中文时,乱码
        */

        //String s = new String(m_binArray,i,(j - i) + 1);
        //return s;

        /**
         * 2008-10-28 carton 修改,解决上传文件中文乱码问题
         */
        Stirng s = null;
        try {
             s = new String(m_binArray,i,(j - i) + 1, "utf-8");
        } catch (UnsupportedEncodingException e) {
             e.printStackTrace();
        }
        return s;
    }

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
真正解决jspSmartUpload组件上传下载文件中文乱码问题。以前在网上也找过!!!下载了些,都没能解决中文乱码问题!自己改了源代码,并做成jar包,直接用就可以。 另,我把File()类 改成了 SmartFile()类。详情请下载。 另我的Blog有详细描述 http://blog.csdn.net/cartonwang/archive/2008/10/28/3168114.aspx 用朋友问到:译后的结果啊,怎么用啊 ------------------------------------------- 我看了下,给的是jar包。直接用就好了。用法和原版的差不多。稍微有点不同。原版:com.jspsmart.upload.File 我的:com.jspsmart.upload.SmartFile 原版的是File类,我的是SmartFile类。另,http://download.csdn.net/source/796632 中有上传文件不刷新页面的方法。我浦发银行的项目就是用这些方法。很好用。 ------------------------------------------------ gylsm发表的评论 真是晕死,用原版的上传还是支持中文的,你的连上传都成了乱码了,又没讲下你的用法和原版的区别,都不知道可不可以用 --------------------------------------------------------------------------- 我上面已经讲了吧,其实和原版的没有什么区别的,唯一一个地方要注意的是File()类我改成SmartFile()了,你用该类,改成SmartFile()类就好了。 如果还不能用,很可能是你没有把原来的Jar包给删除。 间:2009-04-09 18:45:48 来自:61.142.100.* ming100star发表的评论 谢谢伟大的楼主!! 间:2009-04-06 15:48:04 来自:220.249.99.* zwei27发表的评论 朋友太谢谢你了,我到网上怎么都找不到,你帮我搞定,SKS
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值