jspsmartupload 亂碼

在用jspsmartupload組件進行文件上傳下載的時候,如果用戶選擇的是含有中文名字的文件名或是文件路徑含有中文,則會出現亂碼.經過一段時間的調試,本人已經初步解決了這個問題.現將解決的代碼貼出來.


1.上傳(經過測試絕對成功)

  在SmartUpload.java文件中,增加一個屬性private String charset用於進行字符編碼轉換,相應的有兩個方法: 
public void setCharset(String charset)
 {
  this.charset = charset;
 }
 public String getCharset()
 {
  return this.charset;
 }
另外改動二個地方:
在upload()方法中,將
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,this.getCharset());
這個時候我們應該在進行處理上傳的jsp中進行設置SmartUpload su = new SmartUpload();su.setCharset("UTF-8");就可以了.(這步是在上傳的時候設置它的編碼)
在getDataHeader()方法中,將
String s = new String(m_binArray, i, (j - i) + 1);
改爲
String s;
try
  {
   s = new String(m_binArray, i, (j - i) + 1,this.getCharset());
  }
  catch(Exception e)
  {
   s = "";
  }
  在SmartFile.java文件中,增加一個屬性private String charset用於進行字符編碼轉換,相應的有兩個方法: 
public void setCharset(String charset)
 {
  this.charset = charset;
 }
 public String getCharset()
 {
  return this.charset;
 }
另外需要改動一個地方
在getContentString()方法中,將

String s = new String(m_parent.m_binArray,m_startData,m_size);
改爲
String s;
     try
     {
      s = new String(m_parent.m_binArray,m_startData,m_size,this.getCharset());
     }
     catch(Exception e)
     {
      s = "";
     }
對於SmartFile.java文件中,本人認爲可改可不改,不會對上傳有什麼影響.
經過如此改動源代碼後,對於中文亂碼問題有很好的解決能力.

2.下載

  在SmartUpload.java文件中,將downloadFile(String s, String s1, String s2, int i)方法改爲
public void downloadFile(String s, String s1, String s2, int i)
throws ServletException, IOException, SmartUploadException
{
if(s == null)
throw new IllegalArgumentException("File '" + s +
"' not found (1040).");
if(s.equals(""))
throw new IllegalArgumentException("File '" + s +
"' not found (1040).");
if(!isVirtual(s) && m_denyPhysicalPath)
throw new SecurityException("Physical path is
denied (1035).");
if(isVirtual(s))
s = m_application.getRealPath(s);
java.io.File file = new java.io.File(s);
FileInputStream fileinputstream = new FileInputStream(file);
long l = file.length();
boolean flag = false;
int k = 0;
byte abyte0[] = new byte[i];
if(s1 == null)
m_response.setContentType("application/x-msdownload");
else
if(s1.length() == 0)
m_response.setContentType("application/x-msdownload");
else
m_response.setContentType(s1);
m_response.setContentLength((int)l);
m_contentDisposition = m_contentDisposition != null ?
m_contentDisposition : "attachment;";
if(s2 == null)
m_response.setHeader("Content-Disposition",
m_contentDisposition + " filename=" +
toUtf8String(getFileName(s)));
else
if(s2.length() == 0)
m_response.setHeader("Content-Disposition",
m_contentDisposition);
else
m_response.setHeader("Content-Disposition",
m_contentDisposition + " filename=" + toUtf8String(s2));
while((long)k < l)
{
int j = fileinputstream.read(abyte0, 0, i);
k += j;
m_response.getOutputStream().write(abyte0, 0, j);
}
fileinputstream.close();
}
 
另外需要增加一個獲得漢字字符的UTF-8編碼的方法
/**
* 將文件名中的漢字轉爲UTF8編碼的串,以便下載時能正確顯示另存的文件名.
* 縱橫軟件製作中心雨亦奇2003.08.01
* @param s 原文件名
* @return 重新編碼後的文件名
*/
public static String toUtf8String(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();
}
將這個增加到SmartUpload.java文件中,下載時的另存中文名亂碼問題便不會出現了.


本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/shenzhen_mydream/archive/2010/06/02/5643198.aspx

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章