论坛首页 Java企业应用论坛

jspsmartupload utf8支持

浏览 5211 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (10)
作者 正文
   发表时间:2010-01-29   最后修改:2010-02-01

相信很多朋友都用过这个上传下载组件,但是,这个组件默认使用的编码是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内置对象!)

 

   发表时间:2010-01-31   最后修改:2010-04-18
不胜感激!
0 请登录后投票
   发表时间:2010-01-31  
第一,该组件早已经停止开发,网站也已经关闭。需要这类开源组件可以选择Apache的。
第二,虽然该组件可以免费使用,但是并不是开源软件。反编译涉嫌违法。
0 请登录后投票
   发表时间:2010-02-01  
魔力猫咪 写道
第一,该组件早已经停止开发,网站也已经关闭。需要这类开源组件可以选择Apache的。
第二,虽然该组件可以免费使用,但是并不是开源软件。反编译涉嫌违法。

也是,当时没想到那么多,只想着能帮助大家解决使用这个组件的utf-8编码问题,毕竟大家都是奔着简单,好用的原则去用的,如果有这一点瑕疵,为什么不去改正它呢!
0 请登录后投票
   发表时间:2010-02-01  
georgejay 写道
感谢楼主的无私分享,  现在怎么都下载不了东西? 这个老提示5秒钟未必也太欺骗人了!
jiang-jay818@163.com 不知百忙之中是否可以发我一份, 不胜感激!

试了下,还真是不行,不知道为什么提示后,却不跳转,邮件已发送了,希望对你能有帮助!
0 请登录后投票
   发表时间:2010-04-15  
老大,我试过了你的jar包,还是会乱码。
反编译也是一样,迷茫中!
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics