用jspSmartUpload做文件上传下载,该组件默认是不是utf-8编码,对中文支持不好,当文件名或参数有中文时候就会出现乱码。在google一番后找到一种目前比较好的解决方案
修改源码(听说是侵权,但....)
找到源码,并且参考网上的讲法,进行修改,发现可以解决
1)当页面上传有参数时,中文会乱码,解决页面上中文参数乱码方法:
修改类SmartUpload()中的upload()方法
(可以按照关键字 application/x-macbinary 定位到)
public void upload()
throws SmartUploadException, IOException, ServletException
{
int totalRead = 0;
int readBytes = 0;
long totalFileSize = 0L;
boolean found = false;
String dataHeader = new String();
String fieldName = new String();
String fileName = new String();
String fileExt = new String();
String filePathName = new String();
String contentType = new String();
String contentDisp = new String();
String typeMIME = new String();
String subTypeMIME = new String();
boolean isFile = false;
m_totalBytes = m_request.getContentLength();
m_binArray = new byte[m_totalBytes];
for(; totalRead < m_totalBytes; totalRead += readBytes)
try
{
m_request.getInputStream();
readBytes = m_request.getInputStream().read(m_binArray, totalRead, m_totalBytes - totalRead);
}
catch(Exception e)
{
throw new SmartUploadException("Unable to upload.");
}
for(; !found && m_currentIndex < m_totalBytes; m_currentIndex++)
if(m_binArray[m_currentIndex] == 13)
found = true;
else
m_boundary = m_boundary + (char)m_binArray[m_currentIndex];
if(m_currentIndex == 1)
return;
m_currentIndex++;
do
{
if(m_currentIndex >= m_totalBytes)
break;
dataHeader = getDataHeader();
m_currentIndex = m_currentIndex + 2;
isFile = dataHeader.indexOf("filename") > 0;
fieldName = getDataFieldValue(dataHeader, "name");
if(isFile)
{
filePathName = getDataFieldValue(dataHeader, "filename");
fileName = getFileName(filePathName);
fileExt = getFileExt(fileName);
contentType = getContentType(dataHeader);
contentDisp = getContentDisp(dataHeader);
typeMIME = getTypeMIME(contentType);
subTypeMIME = getSubTypeMIME(contentType);
}
getDataSection();
if(isFile && fileName.length() > 0)
{
if(m_deniedFilesList.contains(fileExt))
throw new SecurityException("The extension of the file is denied to be uploaded (1015).");
if(!m_allowedFilesList.isEmpty() && !m_allowedFilesList.contains(fileExt))
throw new SecurityException("The extension of the file is not allowed to be uploaded (1010).");
if(m_maxFileSize > (long)0 && (long)((m_endData - m_startData) + 1) > m_maxFileSize)
throw new SecurityException(String.valueOf((new StringBuffer("Size exceeded for this file : ")).append(fileName).append(" (1105).")));
totalFileSize += (m_endData - m_startData) + 1;
if(m_totalMaxFileSize > (long)0 && totalFileSize > m_totalMaxFileSize)
throw new SecurityException("Total File Size exceeded (1110).");
}
if(isFile)
{
com.jspsmart.upload.File newFile = new com.jspsmart.upload.File();
newFile.setParent(this);
newFile.setFieldName(fieldName);
newFile.setFileName(fileName);
newFile.setFileExt(fileExt);
newFile.setFilePathName(filePathName);
newFile.setIsMissing(filePathName.length() == 0);
newFile.setContentType(contentType);
newFile.setContentDisp(contentDisp);
newFile.setTypeMIME(typeMIME);
newFile.setSubTypeMIME(subTypeMIME);
if(contentType.indexOf("application/x-macbinary") > 0)
m_startData = m_startData + 128;
newFile.setSize((m_endData - m_startData) + 1);
newFile.setStartData(m_startData);
newFile.setEndData(m_endData);
m_files.addFile(newFile);
} else
{
// 源码为:
// String value = new String(m_binArray, m_startData, (m_endData - m_startData) + 1);
// 在原来的基础上定义字符编码utf-8
String value = new String(m_binArray, m_startData, (m_endData - m_startData) + 1, "UTF-8");
m_formRequest.putParameter(fieldName, value);
}
if((char)m_binArray[m_currentIndex + 1] == '-')
break;
m_currentIndex = m_currentIndex + 2;
} while(true);
}
2) 当上传时文件名为中文时,解决中文乱码方法
修改类SmartUpload()中的getDataHeader()方法
private String getDataHeader()
{
int start = m_currentIndex;
int end = 0;
int len = 0;
boolean found = false;
while(!found)
if(m_binArray[m_currentIndex] == 13 && m_binArray[m_currentIndex + 2] == 13)
{
found = true;
end = m_currentIndex - 1;
m_currentIndex = m_currentIndex + 2;
} else
{
m_currentIndex++;
}
String dataHeader = null;
/**
* 源码为:
* dataHeader = new String(m_binArray, start, (end - start) + 1);
*/
try {
// 指定编码方式为utf-8
dataHeader = new String(m_binArray, start, (end - start) + 1,"utf-8");
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
return dataHeader;
}
目前上面的方法可以满足自己在项目中的需求,有兴趣的朋友可以自己下载自己看看源码,或者直接用我改好的先用用
分享到:
相关推荐
最近实验室做了一个项目,使用jspsmartupload来实现的文件的上传下载,原来在windows平台运行的挺好,但是后来系统移植到linux平台上,结果在上传或下载的文件名中有中文时就会出现乱码。后来查了网上一些人的解决...
jspSmartUpload.jar, 这个经本人测试修改,已可真正解决中文乱码问题,包括中文参数和含中文路径及中文文件名乱码问题。
修改过的jspSmartUpload.jar,上传中文文件名不会乱码,表单中文参数不乱码
jspsmart上传组件,解决中文标题乱码,已修改源码为utf-8
原jspSmartUpload组件对返回的文件未作任何处理,现在做了编码的转换工作,将文件名转换为utf-8形式的编码形式从而修复了了原jar包在下载时对中文乱码的问题。 经测试修改后的jar包名为utf8jspsmartupload.jar,使用...
采用jspsmartupload上传文件时,如果文件名含有中文,在服务器端取得文件名是会出现乱码。如果表单项中填写了中文,一样会有乱码问题。看了下jspsmartupload的源码,改了两个地方,现在可以没有乱码问题了。 第一...
经过呕心沥血的一天一夜,终于搞定SmartUpload组件的所有中文问题,为大家去除有史以来最头痛的中文问题,包括中文参数,中文文件名上传后服务器端文件名乱码问题,以及中文文件名下载问题。 这是我的中文解决终极...
经过呕心沥血的一天一夜,终于搞定SmartUpload组件的所有中文问题,为大家去除有史以来最头痛的中文问题,包括中文参数,中文文件名上传后服务器端文件名乱码问题,以及中文文件名下载问题。 这是我的中文解决终极...
经过呕心沥血的一天一夜,终于搞定SmartUpload组件的所有中文问题,为大家去除有史以来最头痛的中文问题,包括中文参数,中文文件名上传后服务器端文件名乱码问题,以及中文文件名下载问题。 这是我的中文解决终极...
经过呕心沥血的一天一夜,终于搞定SmartUpload组件的所有中文问题,为大家去除有史以来最头痛的中文问题,包括中文参数,中文文件名上传后服务器端文件名乱码问题,以及中文文件名下载问题。 这是我的中文解决终极...
jsp SmartUpload 组件,经过测试,保证解决上传中文文件名乱码的问题。里面有六个.java文件(ServletUpload.java;SmartFile.java;SmartFiles.java;SmartRequest.java;SmartUpload.java;SmartUploadException.java) ...
jspsmartupload 文件上传工具,避免上传带有中文名的文件出现文件名乱码!为避免乱码问题,该jar包为改过源码后的jar 。可直接在项目中使用。
经过呕心沥血的一天一夜,终于搞定SmartUpload组件的所有中文问题,为大家去除有史以来最头痛的中文问题,包括中文参数,中文文件名上传后服务器端文件名乱码问题,以及中文文件名下载问题。 这是我的中文解决终极...
修改了中文文件名乱码的问题,在附件上传的时候非常有用。
这个组件原本是不可以下载中文名字的资源的。 下载下来为乱码的文件名。 经过修改源码修复了这个BUG。
支持中文文件名的smartupload的jsp上传组件。 个人做了一点改进,改变了下载中文文件名乱码问题。
jspsmartupload.jar组件大家都知道,但是它本身自带的download功能并不支持中文名称的文件,在下载的时候会出现乱码,我自己编写了一个FileDownload类,放到了这个jar包中,这个类用的UTF-8编码方式,所以可以对中文...
文件上传,下载,对jspSmartUpload组件的SmartUpload类做了升级处理,在提示另存的文件名时,不会显示一堆中文乱码