ext 批量上传文件 完美解决。版本3.x

jsp 除了extext-base.js ext-all.js等基础js,批量上传需要的js(除了swfupload.swf 传附件 其他的不传了)

<script type="text/javascript"  src="<%= request.getContextPath()%>/ext/ux/RowExpander.js"></script>
<script type="text/javascript" src="<%= request.getContextPath()%>/ext/upload/swfupload.js"></script>
<link rel="stylesheet" type="text/css" href="<%= request.getContextPath()%>/ext/resources/css/UploadDialog.css">
<script type="text/javascript"  src="<%= request.getContextPath()%>/ext/upload/UploadDialog.js"></script>

js

//多文件上传 
    // 文件上传窗口
    var dialog = new UploadDialog({
        width: 550, 
        uploadUrl : Ext.contextPath+'/T130400Action_upload.asp',//这里写绝对路
        filePostName : 'files',
        flashUrl : Ext.contextPath + '/ext/upload/swfupload.swf',//这个js附件我后面附带上
        fileSize : '10 MB',
        fileTypes : '*',
        fileTypesDescription : '文本文件(*)',
        title: '头像文件上传',
        scope : this,
        animateTarget: 'upload',
        onEsc: function() {
            this.hide();
        },
        exterMethod: function() {
            
        },
        postParams: {
            txnId: '10205',
            subTxnId: '04'
        }
    });
        };
    var importEmploeeImgs = {
            text: '导入人员头像',
            width: 85,
            disabled: false,
            iconCls: 'edit',
            handler:function() {
                dialog.show();

        }
    };

java 部分 我用的strus2

package com.rail.struts.work.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.huateng.common.Constants;
import com.huateng.common.Operator;
import com.huateng.struts.system.action.BaseAction;
import com.huateng.system.util.ContextUtil;
import com.rail.bo.pub.ResultUtils;
import com.rail.bo.work.T130400BO;

/**
 * 
 * ClassName: T130401Action 
 * @Description: TODO
 * @author syl
 * @date 2019年5月5日
 */
public class T130401Action extends BaseAction {
    
    private Log log = LogFactory.getLog(this.getClass());
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    
    private List<File> files; 
    private String Filename; 
    private Object Upload; 
//    // 上传文件名  
//    private String[] uploadFileName; 
//    // 上传文件的MIME类型  
//    private String[] uploadContentType;  
    
    
    //syl  excel导入上传
    private Map result;
    
    
    //人员信息维护
    private T130400BO t130400BO = (T130400BO) ContextUtil.getBean("T130400BO");
    public T130400BO getT130400BO() {
        return t130400BO;
    }
    public void setT130400BO(T130400BO t130400bo) {
        t130400BO = t130400bo;
    }
        
    @Override
    protected String subExecute() throws Exception {
        System.out.println(getMethod()+"  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        return rspCode;
    }
    
    public void upload() throws Exception {
        operator = (Operator) getSessionAttribute(Constants.OPERATOR_INFO);
        log.info("批量上传图片开始");
        String imgPath = "";
        String format = sdf.format(new Date());
        String suffix = null;
        String fileName = null;
        String fileName2 = null;
        if(null!=files){
            for (int i = 0; i < files.size(); i++) {
                File myfile=files.get(i);
                String myfileName=Filename;
                //String myfileType=uploadContentType[i];
                if(myfile.exists()){
                    log.info("--> File Length: " + myfile.length());
                    //log.info("--> File Tyep: " + myfileType);
                    log.info("--> File Name: " + myfile.getName());
                    log.info("--> File Original: " + myfileName);
                    log.info("========================================");
                    String realPath = ServletActionContext.getServletContext().getRealPath(imgPath);
                    log.info("--> " + realPath);
                    File dir = new File(realPath);
                    if (dir.exists()) {
                        System.out.println("file exists");
                    } else {
                        System.out.println("file not exists, create it ...");
                        try {
                            dir.mkdirs();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }    
                    } 
                    FileInputStream inputStream=new FileInputStream(myfile);
                    int lastIndexOf = myfileName.lastIndexOf(".");
                    suffix = myfileName.substring(lastIndexOf + 1, myfileName.length());
                    fileName2 = UUID.randomUUID().toString().replaceAll("-", "");
                    fileName = myfile.getName() + "." + suffix;
                    log.info("--> File Suffix Name: " + fileName + "---- old Name: " + myfileName);
                    File file = new File(realPath, myfileName);//用原来的 
                    FileOutputStream fos = new FileOutputStream(file);
                    int buffer = 1024; //定义缓冲区的大小
                    int length = 0;
                    byte[] b = new byte[buffer];
                    while ((length = inputStream.read(b)) != -1) {
                        fos.write(b, 0, length); //向文件输出流写读取的数
                    }
                    fos.close();
                   // imgPath = "/upload/"+format +"/"+ fileName;
                    b = null;
                    file = null;
                    inputStream = null;
                    fos = null;
                }
            }
            
            log.info("批量上传图片成功");
        }
        Date date = new Date();
        log.info("------------------批量新增员工图片---------------");
        log("批量导入员工图片成功。操作员编号:" + operator.getOprId());
        result =  ResultUtils.success(1);
        
           PrintWriter printWriter = ServletActionContext.getResponse().getWriter();
            printWriter.write("{'success':true,'msg':'上传成功'}");
            printWriter.flush();
            printWriter.close();
    }
    public Log getLog() {
        return log;
    }
    public void setLog(Log log) {
        this.log = log;
    }
    public List<File> getFiles() {
        return files;
    }
    public void setFiles(List<File> files) {
        this.files = files;
    }
    public String getFilename() {
        return Filename;
    }
    public void setFilename(String filename) {
        Filename = filename;
    }
    public Object getUpload() {
        return Upload;
    }
    public void setUpload(Object upload) {
        Upload = upload;
    }
    public Map getResult() {
        return result;
    }
    public void setResult(Map result) {
        this.result = result;
    }

    public static SimpleDateFormat getSdf() {
        return sdf;
    }

}
 

UploadDialog.js  UploadDialog.css  swfupload.js RowExpander.js

参考 https://blog.csdn.net/somdip/article/details/89843579

猜你喜欢

转载自blog.csdn.net/somdip/article/details/89842350