用SmartUpload写的上传下载文件,带中文的文件名下载不了 如题,当下载带中文名的文件,出现The requested resource (/oa/upload/JSP%E7%AC%94%E8%AE%B0.doc) is not available.此文件中文名是JSP笔记.doc,而下载不带中文的文件名时是正常的,哪位请帮忙解答下,感谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如何下载中文文件 jspSmartUpload虽然能下载文件,但对中文支持不足。若下载的文件名中有汉字,则浏览器在提示另存的文件名时,显示的是一堆乱码,很扫人兴。上面的例子就是这样。(这个问题也是众多下载组件所存在的问题,很少有人解决,搜索不到相关资料,可叹!) 为了给jspSmartUpload组件增加下载中文文件的支持,我对该组件进行了研究,发现对返回给浏览器的另存文件名进行UTF-8编码后,浏览器便能正确显示中文名字了。这是一个令人高兴的发现。于是我对jspSmartUpload组件的SmartUpload类做了升级处理,增加了 toUtf8String这个方法,改动部分源码如下: 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(); } /** * 将文件名中的汉字转为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(); } 注意源码中粗体部分,原jspSmartUpload组件对返回的文件未作任何处理,现在做了编码的转换工作,将文件名转换为UTF-8形式的编码形式。 UTF-8编码对英文未作任何处理,对中文则需要转换为%XX的形式。toUtf8String方法中,直接利用Java语言提供的编码转换方法获得汉字字符的UTF-8编码,之后将其转换为%XX的形式。 将源码编译后打包成jspSmartUpload.jar,拷贝到Tomcat的shared/lib目录下(可为所有WEB应用程序所共享),然后重启Tomcat服务器就可以正常下载含有中文名 在用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 isdenied (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");elseif(s1.length() == 0)m_response.setContentType("application/x-msdownload");elsem_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)));elseif(s2.length() == 0)m_response.setHeader("Content-Disposition",m_contentDisposition);elsem_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文件中,下载时的另存中文名乱码问题便不会出现了. 为了安全我一般使用时间来做文件名然后save到数据库对应中文问题防不胜防 那个东西好像是有问题.试试 apache upload组件? 一般是将附件在服务器的path存在DB里面附件本身就是在服务器的某一个文件夹里 tomcat服务能否在程序中启动 weblogic与tomcat的web.xml文件配置有什么不同 web加入hibernate 毕业设计等着救命!!!jsp连接数据库问题 求助乱码问题!大家进来看看 为什么我在编译的时候说找不到javax.servlet包? java web xml的命名空间 菜鸟求救!~~哪里有java的中文的API下载 jsp中如何实现文件的上传?或者说怎样接收input type=file的提交? struts2返回JSON数据问题求解 javascript 验证跳转问题。 数据库插入后出现的问题..
jspSmartUpload虽然能下载文件,但对中文支持不足。若下载的文件名中有汉字,则浏览器在提示另存的文件名时,显示的是一堆乱码,很扫人兴。上面的例子就是这样。(这个问题也是众多下载组件所存在的问题,很少有人解决,搜索不到相关资料,可叹!)
为了给jspSmartUpload组件增加下载中文文件的支持,我对该组件进行了研究,发现对返回给浏览器的另存文件名进行UTF-8编码后,浏览器便能正确显示中文名字了。这是一个令人高兴的发现。于是我对jspSmartUpload组件的SmartUpload类做了升级处理,增加了 toUtf8String这个方法,改动部分源码如下:
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();
}
/**
* 将文件名中的汉字转为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();
}
注意源码中粗体部分,原jspSmartUpload组件对返回的文件未作任何处理,现在做了编码的转换工作,将文件名转换为UTF-8形式的编码形式。 UTF-8编码对英文未作任何处理,对中文则需要转换为%XX的形式。toUtf8String方法中,直接利用Java语言提供的编码转换方法获得汉字字符的UTF-8编码,之后将其转换为%XX的形式。
将源码编译后打包成jspSmartUpload.jar,拷贝到Tomcat的shared/lib目录下(可为所有WEB应用程序所共享),然后重启Tomcat服务器就可以正常下载含有中文名
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文件中,下载时的另存中文名乱码问题便不会出现了.
中文问题防不胜防
附件本身就是在服务器的某一个文件夹里