sinye 发表于 2013-2-6 10:56:04

jspsmartupload utf8支持

相信很多朋友都用过这个上传下载组件,但是,这个组件默认使用的编码是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 staticString 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;      }      for (int j = 0; j < b.length; j++)      {      int k = b;      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;/* 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内置对象!)
 
页: [1]
查看完整版本: jspsmartupload utf8支持