当前页面: 开发资料首页 → JSP 专题 → 如何解决jsp上传附件中的附件名称中文问题
如何解决jsp上传附件中的附件名称中文问题
摘要: 如何解决jsp上传附件中的附件名称中文问题
近期马上移交一个项目,就差附件上传得时候中文的附件名称显示出来老是乱码,请高手解决 多谢
前提我用得是UTF-8编码:在jsp页面头部有<%@ page contentType="text/html; charset=UTF-8"%>
相关代码如下:
其中我也设置了:request.setCharacterEncoding("gbk");
但是用起来不管用
RequestContext requestContext = new ServletRequestContext(request);
if(FileUpload.isMultipartContent(requestContext)){
//get the uploading execel file, and write to temp dir .
FileItemFactory filefactory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(filefactory);
//upload.setHeaderEncoding("GBK");
List items = upload.parseRequest(request);
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
//if it is file field, save the uploading file
if (!item.isFormField()) {
String fieldName = item.getFieldName();
if ( fieldName.equalsIgnoreCase( "order_attached_file_url" ) ) {
upfilename = item.getName() ;
//process orderAttached
if ( upfilename != null && ! upfilename.trim().equals( "" ) ) {
int k = -1 ;
k = upfilename.lastIndexOf( File.separator ); //File.separator
if ( k == -1 ) {
attachedFileName = upfilename.trim() ;
}
else {
attachedFileName = upfilename.substring( k + 1 ).trim() ;
}
}
else { // no file attached
attachedFileName = "" ;
}
attachedFileName = attachedFileName.replace(':', '_' ) ;
attachedFileName = attachedFileName.replace( File.separatorChar , '_' ) ;
if ( attachedFileName != null && ! attachedFileName.trim().equals("") ) {
//save to temp file
realServerPathFileName = request.getRealPath( File.separatorChar + "ecatic" + File.separatorChar + "temp" + File.separatorChar + attachedFileName );
File newFile = new File( realServerPathFileName );
item.write(newFile);
}
order_attached_file_url = realServerPathFileName ;
}
在线等 :
速请高手指点阿
在ServletFileUpload 创建实例时是否可以通过他的参数来控制中文乱码的出现阿
我们公司这套系统倒是有文件上传的功能
没有出现乱码的问题
QQ:24699227
我看过你这一段代码
和我的那个代码做法不太一样
你看加我qq交流吧
哦 是么?
可是我们公司上班不让开QQ 等有机会我加你咱们在具体聊
真是多谢了
到这里看看嘛http://www.jspcn.net/htmlnews/20050115.html
FileItemFactory filefactory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(filefactory);
upload.setHeaderEncoding("UTF-8");//一定要设定
多谢kkgogo(kkgogo) 真灵 多谢多谢
是否可把QQ留下 便以后交流 QQ:174672028