Ext.ux.UploadDialog上传文件成功后显示“上传失败”及中文文件名乱码问题

xiaoyao6650 2009-03-12 03:41:07
从网上找来Ext.ux.UploadDialog上传文件的例子,但是上传文件后页面显示“上传失败”,而且上传中文文件会出现文件名乱码的现象。请各位大侠帮忙解决一下。谢谢了。
<html>
<head>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link rel="stylesheet" type="text/css" href="ext-2.2/resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css" href="UploadDialog/css/Ext.ux.UploadDialog.css" />
<script type="text/javascript" src="ext-2.2/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="ext-2.2/ext-all.js"></script
<script type="text/javascript" src="UploadDialog/Ext.ux.UploadDialog.packed.js"></script>
<script>
Ext.onReady(function() {
var dialog = new Ext.ux.UploadDialog.Dialog({
autoCreate: true,
closable: true,
collapsible: false,
draggable: true,
minWidth: 400,
minHeight: 200,
width: 400,
height: 350,
permitted_extensions:['JPG','jpg','jpeg','JPEG','GIF','gif','pdf','txt','jar'],
proxyDrag: true,
resizable: true,
constraintoviewport: true,
title: "文件上传",
url: 'uploadJSPBack.jsp',
reset_on_hide: false,
allow_close_on_upload: true
});
dialog.show('show-button');
});
</script>
</head>
<body>
</body>
</html>


uploadJSPBack.jsp
<%@ page language="java" import="java.util.*,java.io.*,org.apache.commons.fileupload.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'uploadJSPBack.jsp' starting page</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<%
response.setCharacterEncoding("utf-8");
String uploadPath = "d:\\upload\\";
String tempPath = "d:\\";
try {
System.out.println("开始进行文件上传");
DiskFileUpload fu = new DiskFileUpload();
fu.setSizeMax(4194304); // 设置最大文件尺寸,这里是4MB
fu.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
fu.setRepositoryPath(tempPath); // 设置临时目录
List fileItems = fu.parseRequest(request); // 得到所有的文件:
Iterator i = fileItems.iterator();
// 依次处理每一个文件:
while (i.hasNext()) {
FileItem fi = (FileItem)i.next();
String fileName = fi.getName();// 获得文件名,这个文件名包括路径:
System.out.println("name==="+fileName);
if (fileName != null) {
// 在这里可以记录用户和文件信息
// 此处可以定义一个接口(CallBack),用于处理后事。
// 写入文件a.txt,你也可以从fileName中提取文件名:
String name = fileName.substring(0,fileName.indexOf("."));
System.out.println("name==="+name);
String extfile = fileName.substring(fileName.indexOf("."));

//上传时间作为文件名,用以防止重复上传
//Timestamp now = new Timestamp((new java.util.Date()).getTime());
// SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
// String pfileName= fmt.format(now).toString().trim();
System.out.println(uploadPath+name+extfile);
fi.write(new File(uploadPath + name + extfile));
}
}

System.out.println("上传成功");

response.setContentType("text/html;charset=utf-8");
response.getWriter().print("{success:true,message:'上传成功'}");
// 跳转到上传成功提示页面
} catch (Exception e) {
e.printStackTrace();
response.getWriter().print("{success:flase,message:'False'}");
// 可以跳转出错页面
}
%>
</head>

<body>
This is my JSP page. <br>
</body>
</html>


...全文
849 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
q1553737306 2012-08-07
  • 打赏
  • 举报
回复
怎么我的也是上传成功但显示失败可是不是那问题啊怎么办
Ghost_520 2009-03-13
  • 打赏
  • 举报
回复
dialog 有很多事件,如下英文。例子中使用了uploadsuccess事件

filetest - fires before file is added into the queue, parameters:
dialog - reference to dialog
filename - file name
If handler returns false then file will not be queued.

fileadd - fires when file is added into the queue, parameters:
dialog - reference to dialog
filename - file name

fileremove - fires when file is removed from the queue, parameters:
dialog - reference to dialog
filename - file name
record - file record

resetqueue - fires when upload queue is resetted, parameters:
dialog - reference to dialog

beforefileuploadstart - fires when file as about to start uploading:
dialog - reference to dialog
filename - uploaded file name
record - file record
If handler returns false then file upload will be canceled.

fileuploadstart - fires when file has started uploading:
dialog - reference to dialog
filename - uploaded file name
record - file record

uploadsuccess - fires when file is successfuly uploaded, parameters:
dialog - reference to dialog
filename - uploaded file name
data - js-object builded from json-data returned from upload handler response.
record - file record

uploaderror - fires when file upload error occured, parameters:
dialog - reference to dialog
filename - uploaded file name
data - js-object builded from json-data returned from upload handler response.
record - file record

uploadfailed - fires when file upload failed, parameters:
dialog - reference to dialog
filename - failed file name
record - file record

uploadcanceled - fires when file upload canceled, parameters:
dialog - reference to dialog
filename - failed file name
record - file record

uploadstart - fires when upload process starts, parameters:
dialog - reference to dialog

uploadstop - fires when upload process stops, parameters:
dialog - reference to dialog

uploadcomplete - fires when upload process complete (no files to upload left), parameters:
dialog - reference to dialog
Ghost_520 2009-03-13
  • 打赏
  • 举报
回复
楼主,我的上传已经做好了,找出你的几个 bug 了:
1. 中文问题,确实将 UTF-8 改成 GBK 就可以传入中文的文件名
2. fi.write(new File(name + extfile)); 去掉 uploadPath ,不然会找不到文件的。
3. 你之所以上传成功了,但是还是返回上传错误,就是你的 JSON 返回值有问题。你把
<body>
This is my JSP page. <br> // 这行也算在 JSON 返回值里了,你把这行代码去掉就可以了,我也是刚刚才发现这个问题的
</body>


现贡献我的代码:

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<link rel="stylesheet" type="text/css" href="ext2.2/resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css" href="ext2.2/examples/shared/examples.css" />
<link rel="stylesheet" type="text/css" href="upload/css/Ext.ux.UploadDialog.css" />
<script type="text/javascript" src="ext2.2/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="ext2.2/ext-all-debug.js"></script>
<script type="text/javascript" src="upload/Ext.ux.UploadDialog.packed.js"></script>
<script type="text/javascript" src="upload/Ext.ux.UploadDialog.js"></script>
</head>
<script type="text/javascript">
Ext.onReady(function() {
var dialog = new Ext.ux.UploadDialog.Dialog({
title: '文件上传__上传过程中关闭窗口,不影响上传_wayfoon',
url:'uploadJSPBack.jsp',
width : 450,
height : 300,
minWidth : 450,
minHeight : 300,
draggable : true,
resizable : true,
//autoCreate: true,
constraintoviewport: true,
permitted_extensions:['JPG','jpg','jpeg','JPEG','GIF','gif','bmp','BMP'],
modal: true,
//post_var_name: 'mms',
reset_on_hide: false,
allow_close_on_upload: true //关闭上传窗口是否仍然上传文件
// upload_autostart: false //是否自动上传文件
});

dialog.show();//'show-button'
dialog.on('uploadsuccess', onUploadSuccess);

// 上传成功后的回调成功的函数
function onUploadSuccess(dialog, filename, resp_data, record)
{
// alert(resp_data);
}
});
</script>
<body>

</body>
</html>



<%@ page language="java"
import="java.util.*,java.io.*,org.apache.commons.fileupload.*"
pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'uploadJSPBack.jsp' starting page</title>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />

<%
response.setCharacterEncoding("GBK");
String uploadPath = "d:\\upload\\";
String tempPath = "d:\\";
try {
System.out.println("开始进行文件上传");
DiskFileUpload fu = new DiskFileUpload();
fu.setSizeMax(4194304); // 设置最大文件尺寸,这里是4MB
fu.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
fu.setRepositoryPath(tempPath); // 设置临时目录
List fileItems = fu.parseRequest(request); // 得到所有的文件:
Iterator i = fileItems.iterator();
// 依次处理每一个文件:
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
String fileName = fi.getName();// 获得文件名,这个文件名包括路径:
if (fileName != null) {
// 在这里可以记录用户和文件信息
// 此处可以定义一个接口(CallBack),用于处理后事。
// 写入文件a.txt,你也可以从fileName中提取文件名:
String name = fileName.substring(0, fileName
.indexOf("."));
String extfile = fileName.substring(fileName
.indexOf("."));

//上传时间作为文件名,用以防止重复上传
//Timestamp now = new Timestamp((new java.util.Date()).getTime());
//SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
//String pfileName= fmt.format(now).toString().trim();
System.out.println(name + extfile);
fi.write(new File(name + extfile));
}
}

System.out.println("上传成功");

response.setContentType("text/html;charset=utf-8");
response.getWriter().print("{success:true,message:'上传成功'}");
// 跳转到上传成功提示页面
} catch (Exception e) {
e.printStackTrace();
response.getWriter().print("{success:false,message:'上传失败'}");
// 可以跳转出错页面
}
%>
</head>

<body>
</body>
</html>
dsk22657 2009-03-13
  • 打赏
  • 举报
回复
还有,楼主的代码异常处理那里要改下
response.getWriter().print("{success:flase,message:'False'}");
为: response.getWriter().print("{success:false,message:'False'}");
dsk22657 2009-03-13
  • 打赏
  • 举报
回复
上传提示失败,把
response.getWriter().print("{success:true,message:'成功'}");
改为:
String msg="{success:true,message:'成功'}";
out.print(msg);
试试看。
dsk22657 2009-03-13
  • 打赏
  • 举报
回复
乱码问题:在JSP页面前面加上 request.setCharacterEncoding("UTF-8");
xiaoyao6650 2009-03-13
  • 打赏
  • 举报
回复
谢谢Ghost_520
终于弄出来了
真是差一点都不行啊
太感谢了!!!
Ghost_520 2009-03-12
  • 打赏
  • 举报
回复

我这里是用你的代码,你确定你两个页面上所有 UTF-8 地方都改成 GBK 了?
xiaoyao6650 2009-03-12
  • 打赏
  • 举报
回复
还是显示“上传失败”,这种方法我试过。
xiaoyao6650 2009-03-12
  • 打赏
  • 举报
回复
不对,改成"gbk”后上传的文件名还是乱码。
Ghost_520 2009-03-12
  • 打赏
  • 举报
回复
你把:

response.getWriter().print("{success:true,message:'上传成功'}");


换成:

response.getWriter().print("{'success':true,'message':'上传成功'}");


注意引号,看能不能正常显示。
Ghost_520 2009-03-12
  • 打赏
  • 举报
回复
“我这里改成“GBK”后出现java.lang.StringIndexOutOfBoundsException: String index out of range: -1错误。 ”

你哪里用到了字符串数组吗?
xiaoyao6650 2009-03-12
  • 打赏
  • 举报
回复
我这里改成“GBK”后出现java.lang.StringIndexOutOfBoundsException: String index out of range: -1错误。
我这里就是上传成功但显示上传失败,不知道怎么回事。
Ghost_520 2009-03-12
  • 打赏
  • 举报
回复

乱码的问题你就把所有的 UTF-8 的地方换成 "GBK" 就可以了。
Ghost_520 2009-03-12
  • 打赏
  • 举报
回复

LZ ,你把我上面的代码换一下,我这里可以上传成功,但是就算上传成功,ext 界面上显示的是上传失败,你是否也是这个问题?
Ghost_520 2009-03-12
  • 打赏
  • 举报
回复


fi.write(new File(uploadPath + name + extfile));


改为:

fi.write(new File(name + extfile));


再试试。
Ghost_520 2009-03-12
  • 打赏
  • 举报
回复

看看后台报什么错误,我把你的环境搭建起来了,我们一起做。呵呵。

52,785

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧