jsp实现上传和下载 解决smartupload中文乱码问题
来源:互联网 发布:js操作符new 编辑:程序博客网 时间:2024/05/22 16:45
最近做了jsp项目,要用到上传和下载,从网上下了一个smartupload项目文件,封成jar包之后使用发现会出现中文乱码问题,后来网上一查,发现出现这个问题的人还真多,很多博客里面只是说明了如何解决,并没有具体实现。我测试了多种解决该问题的方法,最后终于完美解决了这一问题,并封成jar包,下载地址我会贴在文章后面,欢迎大家使用,希望给广大辛苦的软件开发人员带来方便。(注:smartupload的具体使用您自己网上搜索,这里就不做说明了,步骤也蛮简单,不要忙中出乱就好。)
jsp实现上传和下载 中文乱码修正版下载地址
两外你也可以按照网上说的方法自己修改smartupload的源码来解决这个问题,不过你要先下载一个反编译工具,这个我也下好了,你可以直接下载使用:
jar文件反编译工具下载地址
下载了反编译工具之后,你可以做一下修改:
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文件中,下载时的另存中文名乱码问题便不会出现了.
就这么多了,希望能帮你解决问题...
- jsp实现上传和下载 解决smartupload中文乱码问题
- SmartUpload上传文件中文乱码解决
- smartupload上传中文乱码
- JSP实现文件下载同时解决中文乱码问题
- Java Web中使用JSPSmartUpload控件实现文件的上传和下载(解决了中文乱码问题)(JSP页面采用GBK编码)
- Java Web中使用JSPSmartUpload控件实现文件的上传和下载(解决了中文乱码问题)(JSP页面采用GBK编码)
- Jsp Servlet +SmartUpload 实现文件上传.以及上传乱码问题解决.
- SmartUpload实现文件上传和下载
- SmartUpload控件 中文乱码问题 jsp+servlet解决办法
- 解决SmartUpload的中文乱码问题——超简单!
- 解决smartupload获取中文文件名乱码的问题
- jsp SmartUpload上传和下载代码全攻略
- 解决fckeditor(jsp版)上传中文图片乱码问题
- jsp+servlet 文件上传 解决中文乱码问题
- javaEE项目Multipartfile实现文件上传下载并解决上传与下载文件中文乱码的问题
- SmartUpload上传组件实现文件的上传和下载功能
- JSP实现文件上传,解决上传文件名为乱码问题
- smartupload 上传和下载文件
- Ubuntu安装中文字体
- 渗透技巧不定期更新
- 递归式
- 勤道
- servlet与Struts action线程安全问题分析
- jsp实现上传和下载 解决smartupload中文乱码问题
- 关于线程状态
- bitmap index 的研究
- 动态规划(Dynamic Programming)与贪心算法(Greedy Algorithms)
- 想学jsp的看过来 吐血推荐
- Oracle 归档与非归档的切换
- 考试
- 关于HttpWebRequest和HttpWebResponse的应用
- win2003发布软件