commons-fileupload-1.2.jar加Ext.ux.UploadDialog.js做上传,求高手

用commons-fileupload-1.2.jar加Ext.ux.UploadDialog.js做上传,结果文件是上传成功了,但遇到两个问题,求高人解决


1、上传附件名称为中文的会出现乱码。

2、虽然文件已经上传,但UploadDialog.js控件提示的是上传失败,在网上查了好久,都说要返回一个json给UploadDialog.js,但是我太菜鸟了,没搞明白怎么返回这个值。


贴出我的代码


调用的jsp代码:

 

<input type="file">
<input type="button" id="btnUplod" onclick="uploadSw()" value="上传附件">

 js代码:

function uploadSw() {
    debugger;
    dialog = new Ext.ux.UploadDialog.Dialog({   
        title: '文件上传' ,   

        url:'demo2.jsp' , 
        post_var_name:'files', 
        width : 450,   
        height : 300,   
        minWidth : 450,   
        minHeight : 300,   
        draggable : true ,   
        resizable : true ,   
        // autoCreate: true,
        constraintoviewport: true ,   
        // permitted_extensions:[ 'JPG' , 'jpg' , 'jpeg' , 'JPEG' , 'GIF' ,
        // 'gif' , 'png' , 'PNG' ],
        modal: true ,   
        reset_on_hide: false ,   
        allow_close_on_upload: false ,    // 关闭上传窗口是否仍然上传文件
        // upload_autostart: true //是否自动上传文件
        upload_autostart: false  
  });  
    
  dialog.show();         
  dialog.on( 'uploadsuccess' , onUploadSuccess); // 定义上传成功回调函数
  dialog.on( 'uploadfailed' , onUploadFailed); // 定义上传失败回调函数
  dialog.on( 'uploaderror' , onUploadFailed); // 定义上传出错回调函数
  dialog.on( 'uploadcomplete' , onUploadComplete); // 定义上传完成回调函数
    
}
 
// 文件上传成功后的回调函数
onUploadSuccess = function(dialog, filename, resp_data, record){     
   if(!resp_data.success){  
       alert(resp_data.message);// resp_data是json格式的数据
   }          
};     
    
// 文件上传失败后的回调函数
onUploadFailed = function(dialog, filename, resp_data, record){     
   alert(resp_data.message);     
};     
    
// 文件上传完成后的回调函数
onUploadComplete = function(dialog){       
  dialog.hide();     
};     

 

jsp代码:

demo2.jsp

<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
    String uploadPath = "E:\\temp";
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if (isMultipart == true) {
        try {
            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            List<FileItem> items = upload.parseRequest(request);//得到所有的文件
            Iterator<FileItem> itr = items.iterator();
            while (itr.hasNext()) {//依次处理每个文件
                FileItem item = (FileItem) itr.next();
                String fileName = item.getName();//获得文件名,包括路径
                if (fileName != null) {
                    File fullFile = new File(item.getName());
                    File savedFile = new File(uploadPath, fullFile.getName());
                    item.write(savedFile);
                }
                out.print("'success':true,'fileName':'"+fileName+"','message':'success!'");
            }
            out.print("upload succeed");
            out.print("'success':true,'fileName':'fileName','message':'success!'");
            response.getWriter().print("{success:true,message:'上传成功'}");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    else {
        //out.println("the enctype must be multipart/form-data");
    }
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>File upload</title>
</head>
<body>
</body>
</html>

 

谢谢各位大侠了啊

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐