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");就可以了.(這步是在上傳的時候設定它的編碼)
{
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.下載
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檔案中,下載時的另存中文名亂碼問題便不會出現了.