上传文件!

import com.qeweb.util.upload.SingleFileUpload;



//上传文件
        SingleFileUpload upload=new SingleFileUpload();
        upload.setSizeMax(1024*1024);//设置上传文件大小上限为1M
       
        try {
            upload.parseRequest(request);
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }
       
        String fileName=upload.getFileItem().getName();
        if (fileName==null&&fileName.length()==0) {
           
            return mapping.getInputForward();
        }
       
        ActionMessages msgs = new ActionMessages();
       
        String[] allowedSuffix={"gif","jpg"};
        String ext=FileHelper.extractFileExt(fileName, true);
        //判断文件类型是否有效
        boolean flag=false;
       
        for (int i = 0; i < allowedSuffix.length; i++) {
            if (FileHelper.isValidFileExt(fileName, allowedSuffix[i])) {
                flag=true;
                break;
            }
        }
        if (!flag) {
            addActionMessageX(WebConstants.ERROR, request, msgs, "label.systemmgt.sysorganizationmaint.fileExtensionerror");
            saveMessages(request, msgs);
            logMsg = "上传文件类型错误";
            return mapping.getInputForward();
        }
        //文件路径
        String orgID=RightsSessionDataHelper.getCurrentOrganizationId(request).toString();
       
        String fileServerPath=WebUtilInternal.getRealPath(request, UPLOAD_FOLDER)+orgID+"/"+"logo."+ext;
        FileHelper.makeSureDirExist(fileServerPath);
       
        File tempFile=new File(fileServerPath);
        File parentFile=tempFile.getParentFile();
        File[]lists=parentFile.listFiles();
        for (int i = 0; i < lists.length&&parentFile.isDirectory(); i++) {
            lists[i].delete();
        }
        try {
            upload.upload(fileServerPath);   
        } catch (Exception ex) {
            logMsg = "上传文件失败";
            log.error("Could not upload file to "+fileServerPath,ex);
            return mapping.getInputForward();
        }
       
   
        setOptions(request);
        //success
       
        return mapping.findForward("load");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值