环境:jdk 1.5,tomcat5.0,struts-1.2.9,WinXP
我在jsp中用<html:file property="theFile"/>选择上传的文件。
然后在ACTION中用下面的代码获取文件名:
FormFile file = formbean.getTheFile();
String fileName = new String(file.getFileName().getBytes(), "utf-8");
当文件的后缀名是txt,rar等的时候,中英文文件名均显示正常;
但是,当后缀名是doc,xls等MS OFFIC文件时,文件名的最后一个字符会变成乱码,导致文件不能上传(如 工作表.xls 会变为 工作??.xls)。
请问大家这问题如何解决,谢谢!
我在jsp中用<html:file property="theFile"/>选择上传的文件。
然后在ACTION中用下面的代码获取文件名:
FormFile file = formbean.getTheFile();
String fileName = new String(file.getFileName().getBytes(), "utf-8");
当文件的后缀名是txt,rar等的时候,中英文文件名均显示正常;
但是,当后缀名是doc,xls等MS OFFIC文件时,文件名的最后一个字符会变成乱码,导致文件不能上传(如 工作表.xls 会变为 工作??.xls)。
请问大家这问题如何解决,谢谢!
public class SetCharacterEncodingFilter implements Filter { protected FilterConfig filterConfig;
protected String encodingName;
protected boolean enable; public SetCharacterEncodingFilter() {
this.encodingName = "utf-8";
this.enable = false;
} public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
loadConfigParams();
} private void loadConfigParams() {
//encoding
this.encodingName = this.filterConfig.getInitParameter("encoding");
//filter enable flag...
String strIgnoreFlag = this.filterConfig.getInitParameter("enable");
if (strIgnoreFlag.equalsIgnoreCase("true")) {
this.enable = true;
} else {
this.enable = false;
}
} public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if(this.enable) {
request.setCharacterEncoding(this.encodingName);
}
System.out.println("-------------------------SetCharacterEncodingFilter---------------------------");
chain.doFilter(request, response);
} public void destroy() {
}}
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.bnusri.orims.school.course.courseware.tool.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>enable</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String path = getServlet().getServletContext().getRealPath("/"); FileUploadForm fuf = (FileUploadForm) form;
FormFile file = fuf.getFile(); String fname = file.getFileName();
int t = 0;
for (int i = 0; i < fname.length(); i++) {
if (fname.charAt(i) == '.') {
t = i;
}
}
/**
* 文件名
*/
String filename = null;
if (t > 0) {
filename = fname.substring(0, t);
}
String urlFileName = "2006_" + fuf.getLx();
urlFileName += fname.substring(t);
/**
* 保存文件路径
*/
String filepath = "/pages/lawcase/wenshu/" + urlFileName; /**
* 先生成文件保存的路径
*/
if (!(new File(path + "/pages/lawcase/wenshu/")).exists()) {
(new File(path + "/pages/lawcase/wenshu/")).mkdirs();
}
/**
* 创建文件
*/
OutputStream os = new FileOutputStream(path + "/pages/lawcase/wenshu/"
+ urlFileName); int readBytes = 0;
byte buffer[] = new byte[8192];
InputStream stream = file.getInputStream();
while ((readBytes = stream.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, readBytes);
}
os.close();
stream.close();
file.destroy();
request.setAttribute("message", "上传成功!");
return mapping.findForward("success");
}
>>>String urlFileName = "2006-11-13";
转换编码后,楼上兄弟的和我的效果一样,即上传一些txt,rar时,中文名没问题,但遇到.doc .xls 等等offic文件就不行了。
但无论如何,在下都非常感谢楼上的热心帮助!!
问题还未解决,还请大家继续赐教!
页面编码 用
<%@ page pageEncoding="GBK" contentType="text/html; charset=GBK"%>什么都不用做
直接上传就是拉
我已经用这个做了一个文件上传没有出现任何问题
newxy网站:http://www.newxy.net