文件上传 [bsp 1.1 ]

来源:互联网 发布:mac版pscc下载 编辑:程序博客网 时间:2024/05/20 14:18

主方法 1

 @Override    @Transactional(propagation = Propagation.NOT_SUPPORTED)//不支持事务    public void autoKsAttach(String fileDir1, Long associateId) {        /** 1, 连接远程FTP         *  2, 找到指定目录下文件         *  3, 解压缩 文件         *  4, 将附件文件 上传到 BSP ftp 附件池子内         *  5, 数据落地,保存附件 与bsp 会员的关系         **/        if(fileDir1==null){            logger.info("附件地址为空,不执行同步客商附件操作~");            return;        }            try {                //1. 设置FTP 参数,连接远程FTP                //  sunFtp 用于读写文件 , ftpClient 用于获取文件名字                logger.info("ConnectFtp 开始连接ftp....");                //连接ftp 参数                String ftpIp = com.bsteel.util.PropertyUtil.getValue("ks.ftp.ip");                int ftpPort = Integer.parseInt(com.bsteel.util.PropertyUtil.getValue("ks.ftp.port"));                String ftpName = com.bsteel.util.PropertyUtil.getValue("ks.ftp.username");                String ftpPassword = com.bsteel.util.PropertyUtil.getValue("ks.ftp.password");//                FtpClient sunFtp = FtpClient.create();//                sunFtp.setConnectTimeout(2000);// 毫秒为单位//                sunFtp.connect(new InetSocketAddress(ftpIp,ftpPort));//                sunFtp.login(ftpName, ftpPassword.toCharArray());//                sunFtp.setBinaryType();//                logger.info("bspConnectFtp 连接成功(sunFtp)");                BSPFtpHelper ftpClient = new BSPFtpHelper(ftpIp,ftpPort,ftpName,ftpPassword);                ftpClient.connectFTPServer();                logger.info("bspConnectFtp 连接成功(ftpClient)");                //---------                if (!"".equals(fileDir1) && fileDir1 != null) {                    try {                        ftpClient.changeDirectory(fileDir1);                        InputStream is = null;                        try {                            logger.info("************************** FTP 上取文件到 NAS 盘** Strat* " );                            // 配置ftp参数                            ftpClient.getFtp().configure(new FTPClientConfig("org.apache.commons.net.ftp.parser.UnixFTPEntryParser"));                            logger.info("************************** FTP 上取文件地址:fileDir1="+fileDir1);                            List<FTPFile>  lists =  ftpClient.getFtpServerFileList(fileDir1);                            logger.info("**************************lists.size()="+lists.size());                        for(int i=0;i<lists.size();i++ ){                            FTPFile ftpfile = lists.get(i);                            String fileDir = fileDir1 + ftpfile.getName();                            logger.info("**********************获取文件地址="+fileDir);                            //获取原文件的文件名字  例如 abc.txt                            String fileDirAttachName = fileDir.substring(fileDir.lastIndexOf("/") + 1, fileDir.length());                            // BSP ftp 文件路劲前缀  例如 /bsp1/bsp1                            String uploadLocalPathFront = com.bsteel.util.PropertyUtil.getValue("attach.localPath.front");                            // BSP ftp 上传本地文件路径  例如 /member/attachment/member/                            String uploadLocalPath = com.bsteel.util.PropertyUtil.getValue("attach.localPath");                            // BSP ftp 按照当前日期分类存放附件  例如 2016/09/                            String currentDate = new SimpleDateFormat("yyyy/MM/dd").format(new Date()).substring(0, 8);                            uploadLocalPath += currentDate;                            // 原文件名 例如 test.txt                            String[] st = fileDirAttachName.split("\\.");                            // 后缀名(.txt)                            String suffixName = "";                            if (st.length - 1 > 0) suffixName = "." + st[st.length - 1];                            // 用作上传文件的别名                            String uploadFileName = Long.toString(new Date().getTime()) + (int) (Math.random() * 1000);                            // 别名也是下载时的文件名  例如 12121212212.txt                            String downloadFileName = uploadFileName += suffixName;                            System.out.println("uploadFileName=" + uploadFileName);                            System.out.println("downloadFileName=" + downloadFileName);                            //-----例如-/bsp1/bsp1/member/attachment/member/  +  2016/09/ + 12121212212.txt                            String nasUrl = uploadLocalPathFront + uploadLocalPath + uploadFileName;                            logger.info("客商对应文件名为: " + fileDirAttachName);                            //客商同步附件在bsp nas 盘存放地址  abc.txt  -->  nasUrl = /member/member/2016/09/4645464646.txt                           // String nasUrl = takeNasUpUrl(null, fileDirAttachName);                            BufferedOutputStream output = null;                            logger.info("BSP 存入路径为: " + nasUrl);                            try {                                File localFilePath = new File(nasUrl);                                output = new BufferedOutputStream(new FileOutputStream(localFilePath));                                if (output == null) {                                    logger.info("**************************获取文件流失败...");                                    continue;                                }                                boolean success = ftpClient.getFtp().retrieveFile(new String((ftpfile.getName()).getBytes("GBK"), "iso-8859-1"), output);                                System.out.println("下载附件结果:" + ftpfile.getName() + ":" + success);                            }catch (Exception e){                                ControllerUtil.printStackTrace(e);                            }finally {                                if(output !=null){                                    output.close();                                }                            }                            logger.info("*************************** FTP 上取文件到 NAS 盘** END **");                            //保存客商附件关系 保存类型[非压缩文件,和 压缩文件]                            logger.info("读取文件:[uploadLocalPath]="+uploadLocalPath);                            if(!nasUrl.endsWith(".zip") && !nasUrl.endsWith(".rar")){                                saveKS_Attach_ralation(associateId,fileDirAttachName,uploadFileName,uploadLocalPath);                            }                            if (nasUrl.endsWith(".zip")) { //解压缩 ZIP 到当前文件夹                                try {                                    logger.info("*************************** 开始读取NAS盘文件,如果是 .ZIP 文件需要解压缩 **");                                    //解压缩                                    String unZipUrl = uploadLocalPathFront + uploadLocalPath + nasUrl.substring(nasUrl.lastIndexOf("/") + 1, nasUrl.lastIndexOf("."));                                    //unZipUrl.replaceAll("/",File.separator);                                    logger.info("*****.zip 解压缩路径= unZipUrl=" + unZipUrl + "------文件地址nasUrl=" + nasUrl);                                    Map<String,String> mapNames=new HashMap <String,String>();                                    BSPUnZipUtil.unzip(nasUrl, unZipUrl,mapNames);                                    //压缩文件附件表存放 downLoadUrl  用于存附件表                                    String downLoadUrl = uploadLocalPath + nasUrl.substring(nasUrl.lastIndexOf("/") + 1, nasUrl.lastIndexOf("."));                                    logger.info(" 读取解压缩后的文件夹 ");                                    diguiDir(unZipUrl, downLoadUrl, associateId,mapNames);                                }catch (Exception e){                                    ControllerUtil.printStackTrace(e);                                }                            }                            if (nasUrl.endsWith(".rar")) { //解压缩 rar 到当前文件夹                                try {                                    logger.info("*************************** 开始读取NAS盘文件,如果是 .rar 文件需要解压缩 **");                                    //解压缩                                    String unRarUrl = uploadLocalPathFront + uploadLocalPath + nasUrl.substring(nasUrl.lastIndexOf("/") + 1, nasUrl.lastIndexOf("."));                                    // unRarUrl.replaceAll("/",File.separator);                                    logger.info("*****.unrar 解压缩路径= unRarUrl=" + unRarUrl + "------文件地址nasUrl=" + nasUrl);                                    Map<String,String> mapNames=new HashMap <String,String>();                                    BSPUnZipUtil.unrar(nasUrl, unRarUrl + File.separator , mapNames);                                    logger.info(" 读取解压缩后的文件夹 ");                                    //压缩文件附件表存放 downLoadUrl  用于存附件表                                    String downLoadUrl = uploadLocalPath + nasUrl.substring(nasUrl.lastIndexOf("/") + 1, nasUrl.lastIndexOf("."));                                    diguiDir(unRarUrl, downLoadUrl, associateId , mapNames);                                }catch (Exception e){                                    ControllerUtil.printStackTrace(e);                                }                            }                        }                        } catch (FtpProtocolException e) {                            logger.info("连接路径不存在!");                            ControllerUtil.printStackTrace(e);                        } catch (IOException e) {                            ControllerUtil.printStackTrace(e);                        }                    } catch (FtpProtocolException e1) {                        logger.info("请检查输入的FTP地址、账号、密码是否正确!");                        ControllerUtil.printStackTrace(e1);                    } catch (IOException e1) {                        ControllerUtil.printStackTrace(e1);                    }finally {                        ftpClient.closeFTPClient();                    }                }//IF end            } catch (Exception e1) {                logger.info("连接超时,请稍后再试!");                ControllerUtil.printStackTrace(e1);            }    }    /**     *     * @param file File     * @param  nameStr 直接传如文件名,优先引用     * @return     */    private  String takeNasUpUrl(File file,String nameStr){        String retString = "";//返回地址        //路径        UploadResultModel resultModel = new UploadResultModel();        // 文件路劲前缀        String uploadLocalPathFront = com.bsteel.util.PropertyUtil.getValue("attach.localPath.front");        // 上传本地文件路径        String uploadLocalPath = com.bsteel.util.PropertyUtil.getValue("attach.localPath");        // 按照当前日期分类存放附件        String currentDate = new SimpleDateFormat("yyyy/MM/dd").format(new Date()).substring(0, 8);        uploadLocalPath += currentDate;        // 原文件名 test.txt        String[] st =null;        if (nameStr !=null){            st = nameStr.split("\\.");        }else{            st = file.getName().split("\\.");        }        // 后缀名(.txt)        String suffixName = "";        if (st.length - 1 > 0) suffixName = "." + st[st.length - 1];        // 用作上传文件的别名        String uploadFileName = Long.toString(new Date().getTime()) + (int) (Math.random() * 1000);        // 别名也是下载时的文件名        String downloadFileName = uploadFileName += suffixName;        System.out.println("uploadFileName=" + uploadFileName);        System.out.println("downloadFileName=" + downloadFileName);        String localAttachPath = uploadLocalPathFront + uploadLocalPath + uploadFileName;        logger.info("#AttachOperationServiceImpl#uploadAttach#上传路径localAttachPath==" + localAttachPath);        retString = localAttachPath;        return retString;    }    /***     * 保存客商代码 和 附件 的关系     * @param associateId  客商代码     * @param orgFileName  原文件名     * @param downLoadFileName  下载文件名     * @param downLoadUrl 存储nas 盘地址     */    private void saveKS_Attach_ralation(Long associateId, String orgFileName,String downLoadFileName,String downLoadUrl) {        //附件全路径为 unZipUrl + orgFileName        logger.info("保存客商附件关系--start");        TmumMemberAttach m2 = new TmumMemberAttach();        m2.setAssociateId(associateId);        m2.setOriginalFileName(orgFileName);        m2.setAttachCheck("0");        m2.setDownloadFileName(downLoadFileName);        m2.setDownloadUrl(downLoadUrl);        m2.setAssociateTable("TAKAK01");        m2.setIsDeleted("0");        m2.setCreateTime(new Date());        mumMemberAttachDao.save(m2);        logger.info("保存客商附件关系--end------TmumMemberAttach1={" + m2.toString() + "}");    }    private void diguiDir(String unZipUrl,String downLoadUrl,Long associateId,Map<String,String> mapNames){        File file2 = new File(unZipUrl);        String[] fileStrings2 = file2.list();        logger.info("解压缩文件地址,用于存附件表 downLoadUrl= "+downLoadUrl);        for (String s2 : fileStrings2) {            //遍历解压缩后的文件夹  ---判断是否是目录            //1判断当前 s2 资源 是不是目录            File file3 = new File(unZipUrl+ File.separator +s2);            logger.info("unZipUrl+ /+s2="+unZipUrl+File.separator+s2);            if(file3.isDirectory()){                diguiDir(unZipUrl+File.separator+s2,downLoadUrl+File.separator+s2,associateId,mapNames);            }else {                //保存客商附件关系                logger.info("读取文件:[downLoadUrl+s2]="+downLoadUrl+File.separator+s2);                String orgname = s2;                if(mapNames!=null) {                     orgname = mapNames.get(s2);                }                saveKS_Attach_ralation(associateId,orgname , s2,downLoadUrl+"/");            }        }    }

辅助方法2(   解,压缩 方法)

package com.bsteel.common.util;import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.util.Date;import java.util.Enumeration;import java.util.Map;import com.bsteel.common.Constants;import com.bsteel.util.ControllerUtil;import com.github.junrar.Archive;import com.github.junrar.rarfile.FileHeader;import org.apache.log4j.Logger;import org.apache.tools.ant.Project;import org.apache.tools.ant.taskdefs.Expand;import org.apache.tools.zip.ZipEntry;import org.apache.tools.zip.ZipFile;//import de.innosystec.unrar.Archive;//import de.innosystec.unrar.rarfile.FileHeader;/** * Created by Sotiy-mtf on 2016/9/13. */public class BSPUnZipUtil {    static Logger logger = Logger.getLogger(BSPUnZipUtil.class);    /**     * 解压zip格式压缩包     * 对应的是ant.jar     */    public static void unzip(String sourceZip,String destDir,Map<String,String> mapNames) throws Exception{        try{            String zipFilePath = sourceZip;//"/bsp1/bsp1/member/attachment/member/2016/09/ziptest.zip";            System.out.println(zipFilePath);            String outputDirectory = destDir;//"/bsp1/bsp1/member/attachment/member/2016/09/qyr";            boolean flag = false;            try {                ZipFile zipFile = new ZipFile(zipFilePath,"GB18030");                Enumeration e = zipFile.getEntries();                ZipEntry zipEntry = null;                createDirectory(outputDirectory, "");                while (e.hasMoreElements()) {                    zipEntry = (ZipEntry) e.nextElement();                    System.out.println("unzip " + zipEntry.getName());                    if (zipEntry.isDirectory()) {                        continue;                    } else {                        String fileName = zipEntry.getName();                        String[] entrypathNames = fileName.split("/");                        fileName = entrypathNames[entrypathNames.length-1];                        fileName = fileName.replace('\\', '/');                        String downloadFileName =getDownName(fileName);                        mapNames.put(downloadFileName,fileName);                        File f = new File(outputDirectory + File.separator                                + downloadFileName);                        f.createNewFile();                        InputStream in = zipFile.getInputStream(zipEntry);                        FileOutputStream out1 = new FileOutputStream(f);                        byte[] by = new byte[1024];                        int c;                        while ((c = in.read(by)) != -1) {                            out1.write(by, 0, c);                        }                        in.close();                        out1.close();                    }                }                flag = true;                System.out.println("unzip finished");            } catch (Exception ex) {                ex.printStackTrace();            }        }catch (Exception e){            ControllerUtil.printStackTrace(e);        }    }    public static String getDownName(String entrypath) {        // 原文件名 例如 test.txt        String[] st = entrypath.split("\\.");        // 后缀名(.txt)        String suffixName = "";        if (st.length - 1 > 0) suffixName = "." + st[st.length - 1];        // 用作上传文件的别名        String uploadFileName = Long.toString(new Date().getTime()) + (int) (Math.random() * 1000);        // 别名也是下载时的文件名  例如 12121212212.txt        String downloadFileName = uploadFileName + suffixName;        logger.info("上传文件名="+entrypath+"|| 别名="+downloadFileName);        return downloadFileName;    }    public static void createDirectory(String directory, String subDirectory) {        String dir[];        File fl = new File(directory);        try {            if (subDirectory == "" && fl.exists() != true) {                fl.mkdir();            } else if (subDirectory != "") {                dir = subDirectory.replace('\\', '/').split("/");                for (int i = 0; i < dir.length; i++) {                    File subFile = new File(directory + File.separator + dir[i]);                    if (subFile.exists() == false) {                        subFile.mkdir();                    }                    directory += File.separator + dir[i];                }            }        } catch (Exception ex) {            System.out.println(ex.getMessage());        }    }    /**     * 解压rar格式压缩包。     * 对应的是java-unrar-0.3.jar,但是java-unrar-0.3.jar又会用到commons-logging-1.1.1.jar     */    public static void unrar(String sourceRar,String destDir , Map<String,String> mapNames) throws Exception{        Archive a = null;        FileOutputStream fos = null;        try{            logger.info("***__unrar__start");            logger.info("***__unrar__start___sourceRar="+sourceRar);            logger.info("***__unrar__start___destDir="+destDir);            a = new Archive(new File(sourceRar));            FileHeader fh = a.nextFileHeader();            while(fh!=null){                String entrypath = "";                if(fh.isUnicode()){//解決中文乱码                    entrypath = fh.getFileNameW().trim();                }else{                    entrypath = fh.getFileNameString().trim();                }               // entrypath = entrypath.replaceAll("\\\\", File.separator);                String[] entrypathNames = entrypath.split("\\\\");                entrypath = entrypathNames[entrypathNames.length-1];                logger.info("***__unrar__start___(destDir + entrypath)=" + destDir + entrypath);                String downloadFileName =getDownName(entrypath);;                logger.info("上传文件名="+entrypath+"|| 别名="+downloadFileName);                mapNames.put(downloadFileName,entrypath);                File file = new File(destDir + downloadFileName);                if(fh.isDirectory()){                    file.mkdirs();                }else{                    File parent = file.getParentFile();                    if(parent!=null && !parent.exists()){                        parent.mkdirs();                    }                    fos = new FileOutputStream(file);                    a.extractFile(fh, fos);                    fos.close();                }                fh = a.nextFileHeader();            }            logger.info("***__unrar__end");            a.close();            a = null;        }catch(Exception e){            throw e;        }finally{            if(fos!=null){                try{fos.close();fos=null;}catch(Exception e){e.printStackTrace();}            }            if(a!=null){                try{a.close();a=null;}catch(Exception e){e.printStackTrace();}            }        }    }    /**     * 解压缩     */    public static void deCompress(String sourceFile,String destDir) throws Exception{        //保证文件夹路径最后是"/"或者"\"        char lastChar = destDir.charAt(destDir.length()-1);        if(lastChar!='/'&&lastChar!='\\'){            destDir += File.separator;        }        //根据类型,进行相应的解压缩        String type = sourceFile.substring(sourceFile.lastIndexOf(".")+1);        if(type.equals("zip")){            //BSPUnZipUtil.unzip(sourceFile, destDir);        }else if(type.equals("rar")){           // BSPUnZipUtil.unrar(sourceFile, destDir);        }else{            throw new Exception("只支持zip和rar格式的压缩包!");        }    }    public static void main(String[] args) throws Exception {        Boolean s = Constants.CHANNEL_COLLECT_B0003.contains("ZSMJYGB");        System.out.println(s);//        String sourceFile = "F:\\test\\loaclload\\213131.rar";//        String destDir = "F:\\test\\upload\\213131\\";//        BSPUnZipUtil.unrar(sourceFile, destDir);    }}



0 0
原创粉丝点击