jsp SmartUpload 中文乱码问题解决
在用jspsmartupload组件进行文件上传下载的时候,如果用户选择的是含有中文名字的文件名或是文件路径含有中文,则会出现乱码.经过一段时间的调试,本人已经初步解决了这个问题.现将解决的代码贴出来.
一、上传
在SmartUpload.java文件中,增加一个属性privateStringcharset用于进行字符编码转换,相应的有两个方法:
publicvoidsetCharset(Stringcharset)
{
this.charset=charset;
}
publicStringgetCharset()
{
returnthis.charset;
}
另外改动二个地方:
在upload()方法中,将Strings11=newString(m_binArray,m_startData,(m_endData-m_startData)+1);改为
Strings11=newString(m_binArray,m_startData,(m_endData-m_startData)+1,this.getCharset());
这个时候我们应该在进行处理上传的jsp中进行设置
SmartUploadsu=newSmartUpload();
su.setCharset("UTF-8");就可以了.
在getDataHeader()方法中,将Strings=newString(m_binArray,i,(j-i)+1);改为
Strings;
try
{
s=newString(m_binArray,i,(j-i)+1,this.getCharset());
}
catch(Exceptione)
{
s="";
}
在SmartFile.java文件中,增加一个属性privateStringcharset用于进行字符编码转换,相应的有两个方法:
publicvoidsetCharset(Stringcharset)
{
this.charset=charset;
}
publicStringgetCharset()
{
returnthis.charset;
}
另外需要改动一个地方
在getContentString()方法中,将Strings=newString(m_parent.m_binArray,m_startData,m_size);改为
Strings;
try
{
s=newString(m_parent.m_binArray,m_startData,m_size,this.getCharset());
}
catch(Exceptione)
{
s="";
}
对于SmartFile.java文件中,本人认为可改可不改,不会对上传有什么影响.
经过如此改动源代码后,对于中文乱码问题有很好的解决能力.
二、下载
在SmartUpload.java文件中,将downloadFile(Strings,Strings1,Strings2,inti)方法改为
publicvoiddownloadFile(Strings,Strings1,Strings2,inti)
throwsServletException,IOException,SmartUploadException
{
if(s==null)
thrownewIllegalArgumentException("File'"+s+
"'notfound(1040).");
if(s.equals(""))
thrownewIllegalArgumentException("File'"+s+
"'notfound(1040).");
if(!isVirtual(s)&&m_denyPhysicalPath)
thrownewSecurityException("Physicalpathis
denied(1035).");
if(isVirtual(s))
s=m_application.getRealPath(s);
java.io.Filefile=newjava.io.File(s);
FileInputStreamfileinputstream=newFileInputStream(file);
longl=file.length();
booleanflag=false;
intk=0;
byteabyte0[]=newbyte[i];
if(s1==null)
m_response.setContentType("application/x-msdownload");
elseif(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)
{
intj=fileinputstream.read(abyte0,0,i);
k+=j;
m_response.getOutputStream().write(abyte0,0,j);
}
fileinputstream.close();
} 另外需要增加一个获得汉字字符的UTF-8编码的方法
/**
*将文件名中的汉字转为UTF8编码的串,以便下载时能正确显示另存的文件名.
*雨亦奇2003.08.01
*@params原文件名
*@return重新编码后的文件名
*/
publicstaticStringtoUtf8String(Strings){
StringBuffersb=newStringBuffer();
for(inti=0;i<s.length();i++){
charc=s.charAt(i);
if(c>=0&&c<=255){
sb.append(c);
}else{
byte[]b;
try{
b=Character.toString(c).getBytes("utf-8");
}catch(Exceptionex){
System.out.println(ex);
b=newbyte[0];
}
for(intj=0;j<b.length;j++){
intk=b[j];
if(k<0)k+=256;
sb.append("%"+Integer.toHexString(k).toUpperCase());
}
}
}
returnsb.toString();
}
将这个增加到SmartUpload.java文件中,下载时的另存中文名乱码问题便不会出现了,处理完了,希望能给大家一个参考,也希望大家多多支持毛票票。