在用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文件中,下载时的另存中文名乱码问题便不会出现了。
分享到:
相关推荐
最近实验室做了一个项目,使用jspsmartupload来实现的文件的上传下载,原来在windows平台运行的挺好,但是后来系统移植到linux平台上,结果在上传或下载的文件名中有中文时就会出现乱码。后来查了网上一些人的解决...
用几个简单的java语句操作java.io.BufferedInputStream和java.io.BufferedOutputStream,就能彻底解决jspSmartUpload乱码问题。下载的时候无需再用jspsmartupload。 zip包中包含了jspsmartupload.jar和文件上传、...
jsp SmartUpload 中文乱码问题解决.docx
解决JSPSmartUpload上传过程中中文乱码问题,可以直接使用,简单易用
jspSmartUpload组件是jsp中的文件上传组件,但是其官方网站已经关闭,由于其是外国人开发,故存在中文乱码问题,我将它的源文件重新修改编译,已解决中文乱码问题。
jspsmartupload中文乱码解决,内附struts1.2源码例子,jspsmartupload包及源代码
下载即可运行,但是这个压缩包不支持中文名称的文件下载功能,我上传了一个可以下载中文的不会乱码的jspSmartUpload 组件,网址:http://download.csdn.net/detail/huahuahailang/4265620 照着使用,即可下载中文。
以前遇到jspSmartUpload中文乱码问题,在CSDN上下载了jspSmartUpload的jar包却没解决,所以花了几个小时把问题解决了,已经测试没有问题。直接放入项目的lib目录下使用就可以。
重新编译过,修正了utf-8下取值乱码的问题
通过原代码修改了jspsmartupload解决linux+resin中文乱码
主要介绍jsp中SmartUpload中文乱码问题的解决,比较实用,需要的朋友可以参考下。
JSP SmartUpload上传文件乱码解决纪实+UploadBean上传解决方案.mht 和代码
修改过的jspsmartupload ,可以获取表单中的中文数据,不乱码,可以上传名字含有中文的文件。
真正解决jspSmartUpload组件上传下载文件时中文乱码问题。以前在网上也找过!!!下载了些,都没能解决中文乱码问题!自己改了源代码,并做成jar包,直接用就可以。 另,我把File()类 改成了 SmartFile()类。详情请...
NULL 博文链接:https://liuzidong.iteye.com/blog/807529
eclipse工程实例,包含jspsmartupload,上传文件和图片路径中文无乱码
别嫌贵,就是好用,就是任性。自己做项目的时候碰到上传乱码的问题了。然后通过改源码,重新打包。就是这个资源。绝对有效,设置的编码是utf-8
修改过的jspSmartUpload.jar,上传中文文件名不会乱码,表单中文参数不乱码
jspSmartUpload.jar, 这个经本人测试修改,已可真正解决中文乱码问题,包括中文参数和含中文路径及中文文件名乱码问题。
jspSmartUpload 组件 源代码,并对源代码进行了修改,使其在下载中文文件时,不会出现乱码。使用方法参见http://blog.sina.com.cn/s/blog_4cc16fc50100bwby.html