⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mmshandle.java

📁 数据刷新程序,用于不同数据库之间德数据传递
💻 JAVA
字号:
package com.main.apps.handle;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;

import com.main.apps.common.FileUtil;
import com.main.apps.common.ToolsKit;
import com.main.apps.control.Config;
import com.main.apps.message.MMSInfo;
import com.main.apps.message.MMSInfoList;

public class MMSHandle extends Thread
{

    public MMSHandle()
    {
        this.srcDir = Config.srcDir + File.separatorChar + "MMS";
        this.destDir = Config.rootDir + File.separatorChar + "mms";
        this.tempDir = Config.tempDir + File.separatorChar + "mms";
        this.list = MMSInfoList.getInstance();
        this.buffer = new StringBuffer();
    }

    public void run()
    {
        System.out.println("彩信处理线程启动...");
        String[] fileList = null;
        String fileName = null;
        File srcFile = new File(srcDir);
        while (!Thread.interrupted())
        {
            try
            {
                fileList = srcFile.list();

                if (fileList == null)
                {
                    Thread.sleep(1000);
                    continue;
                }
                for (int i = 0; i < fileList.length; i++)
                {
                    fileName = fileList[i];
                    if ((fileName.startsWith("mms") || fileName.startsWith("MMS"))
                       && (fileName.endsWith(".zip") || fileName.endsWith(".ZIP")))
                    {
                        String infoCode = handleMMS(fileName);
                        if (infoCode == null)
                        {
                            infoCode = "ERROR";
                        }
                        moveFile(fileName, infoCode);
                    }
                }
                Thread.sleep(5000);
            }
            catch (Exception e)
            {
                FileUtil.printError(e, "", Config.errorFile);
            }
        }
    }

    /**
     * 移动文件
     * @param fileName, 要移动的文件名
     */
    private void moveFile(String fileName, String infoCode)
    {
        buffer.setLength(0);
        buffer.append(srcDir);
        buffer.append(File.separatorChar);
        buffer.append(fileName);
        String srcFile = buffer.toString();
        buffer.setLength(0);
        buffer.append(Config.backupDir);
        buffer.append(File.separatorChar);
        buffer.append("mms");
        buffer.append(File.separatorChar);
        buffer.append(infoCode);
        // 保证目录存在
        File dir = new File(buffer.toString());
        if (!dir.exists())
        {
            dir.mkdirs();
        }
        buffer.append(File.separatorChar);
        buffer.append(fileName);
        String destFile = buffer.toString();
        ToolsKit.move(srcFile, destFile);
    }
    
    /**
     * 处理彩信
     * 
     * @param mmsFile
     * @return MMSInfo
     * @throws IOException
     */
    private String handleMMS(String mmsFile)
    {
        boolean hasSmil = false;
        try
        {
            MMSInfo info = null;
            buffer.setLength(0);
            buffer.append(srcDir);
            buffer.append(File.separatorChar);
            buffer.append(mmsFile);
            String fileName = buffer.toString();
            ToolsKit.unzip(fileName, tempDir);
            File unzipDir = new File(tempDir);
            String[] fileList = unzipDir.list();
            if (fileList == null) // 目录下没文件,不处理
            {
                return null;
            }
            
            info = new MMSInfo();
            String str = null;
            Vector fileNames = new Vector(); // 各种WAP文件路径
            for (int i = 0; i < fileList.length; i++)
            {
                fileName = fileList[i];
                if (fileName.equals("1_.txt") || fileName.equals("1_.TXT")) // 读取标题
                {
                    buffer.setLength(0);
                    buffer.append(tempDir);
                    buffer.append(File.separatorChar);
                    buffer.append(fileName);
                    fileName = buffer.toString();
                    BufferedReader in = new BufferedReader(new FileReader(fileName));
                    buffer.setLength(0);
                    while ((str = in.readLine()) != null)
                    {
                        buffer.append(str);
                    }
                    in.close();
                    info.setTitle(buffer.toString().trim());
                    new File(fileName).delete(); // 处理完后删除掉
                }
                else if (fileName.equals("1_.ini") || fileName.equals("1_.INI")) // 读取配置信息
                {
                    buffer.setLength(0);
                    buffer.append(tempDir);
                    buffer.append(File.separatorChar);
                    buffer.append(fileName);
                    fileName = buffer.toString();
                    int lines = 0;
                    BufferedReader in = new BufferedReader(new FileReader(fileName));
                    while ((str = in.readLine()) != null)
                    {
                        lines++;
                        switch (lines)
                        // 一般有4列
                        {
                            case 1:
                                // 活动代码没有,错误不处理
                                if (str == null || str.equals(""))
                                {
                                    return null;
                                }
                                info.setInfoCode(str.trim());
                                break;
                            case 2:
                                info.setKeyword1(str.trim());
                                break;
                            case 3:
                                info.setKeyword2(str.trim());
                                break;
                            case 4:
                                info.setKeyword3(str.trim());
                                break;
                            default:
                        // do nothing
                        }
                    }
                    in.close();
                    new File(fileName).delete(); // 处理完后删除掉
                } 
                else if (fileName.equals("1_.smil") || fileName.equals("1_.SMIL"))
                {
                    hasSmil = true;
                    fileNames.add(fileName);
                }
                else // 多媒体内容路径组合
                {
                    fileNames.add(fileName);
                }
            }
            // 移动资源文件
            PathInfo paths = moveFiles(fileNames, mmsFile, info.getInfoCode(), hasSmil); 
            if (paths == null)
            {
                return null;
            }
            info.setSmilPath(paths.smilPath);
            info.setMutiMediaPath(paths.mutiMediaPath);
            list.add(info);
            return info.getInfoCode();
        }
        catch (Exception e)
        {
            FileUtil.printError(e, "", Config.errorFile);
        }
        return null;
    }

    /**
     * 移动资源文件及生成资源文件路径
     * 
     * @param fileNames,
     *            文件名序列
     * @param wapFile,
     *            正在处理的mms文件
     * @param infoCode,
     *            此mms文件的活动代码
     * @return String, 多媒体资源文件路径组合,以“,”间隔
     */
    private PathInfo moveFiles(Vector fileNames, String mmsFile, String infoCode, boolean hasSmil)
    {
        if (fileNames == null || fileNames.size() == 0)
        {
            return null;
        }
      
        String fileName = null;
        StringBuffer paths = new StringBuffer();
        PathInfo info = new PathInfo();
        buffer.setLength(0);
        buffer.append(infoCode); // 信息代码为一层目录
        buffer.append(File.separatorChar);
        buffer.append(mmsFile.substring(3, 9)); // 日期为一个目录
        buffer.append(File.separatorChar);
        buffer.append(mmsFile.substring(9, mmsFile.indexOf('.'))); // 时间为一层目录
        String canoDestDir = buffer.toString(); // 相对目的路径,数据里需要
        
        if (hasSmil)
        {
            buffer.setLength(0);
            buffer.append(destDir);
            buffer.append(File.separatorChar);
            buffer.append("smil");
            buffer.append(File.separatorChar);
            buffer.append(canoDestDir);
            String absDestDir = buffer.toString(); // 绝对目的路径
            new File(absDestDir).mkdirs(); // 先把目录创建好
            for (int i = 0; i < fileNames.size(); i++)
            {
                fileName = fileNames.get(i).toString();
                buffer.setLength(0);
                buffer.append(tempDir);
                buffer.append(File.separatorChar);
                buffer.append(fileName);
                String moveSrcFile = buffer.toString();
                buffer.setLength(0);
                buffer.append(absDestDir);
                buffer.append(File.separatorChar);
                buffer.append(fileName);
                String moveDestFile = buffer.toString(); // 得出转移后的文件名
                ToolsKit.move(moveSrcFile, moveDestFile); // 转移文件
                buffer.setLength(0);
                buffer.append(canoDestDir);
                buffer.append(File.separatorChar);
                buffer.append(fileName);
                if (fileName.equals("1_.smil") || fileName.equals("1_.SMIL"))
                {
                    info.smilPath = buffer.toString();
                }
                else
                {
                    paths.append(buffer.toString());
                    paths.append(",");
                }
            }
        }
        else
        {
            for (int i = 0; i < fileNames.size(); i++)
            {
                fileName = fileNames.get(i).toString();
                buffer.setLength(0);
                buffer.append(tempDir);
                buffer.append(File.separatorChar);
                buffer.append(fileName);
                String moveSrcFile = buffer.toString();
                buffer.setLength(0);
                buffer.append(destDir);
                buffer.append(File.separatorChar);
                if (fileName.endsWith(".txt") || fileName.endsWith(".TXT"))
                {
                    buffer.append("text");
                }
                else if (fileName.endsWith(".mid") || fileName.endsWith(".MID")
                       || fileName.endsWith(".midi") || fileName.endsWith(".MIDI")
                       || fileName.endsWith(".ime") || fileName.endsWith(".IME")
                       || fileName.endsWith(".amr") || fileName.endsWith(".AMR"))
                {
                    buffer.append("midi");
                }
                else
                {
                    buffer.append("image");
                }
                buffer.append(File.separatorChar);
                buffer.append(canoDestDir);
                String absDestDir = buffer.toString(); // 绝对目的路径
                new File(absDestDir).mkdirs(); // 先把目录创建好
                buffer.append(File.separatorChar);
                buffer.append(fileName);
                String moveDestFile = buffer.toString(); // 得出转移后的文件名
                ToolsKit.move(moveSrcFile, moveDestFile); // 转移文件
                buffer.setLength(0);
                buffer.append(canoDestDir);
                buffer.append(File.separatorChar);
                buffer.append(fileName);
                paths.append(buffer.toString());
                paths.append(",");
            }
        }
        paths.deleteCharAt(paths.lastIndexOf(","));
        info.mutiMediaPath = paths.toString();
        paths = null;
        return info;
    }

    public static void main(String args[])
    {
        new Config();
        new MMSHandle().start();
    }
    
    private String srcDir; // WAP信息文件所在路径

    private String destDir; // 目标文件所在路径

    private String tempDir; // 临时文件路径

    private StringBuffer buffer; // 字符串缓冲区

    private MMSInfoList list; // 彩信信息列表
    
    /*
     * 临时小类
     */
    class PathInfo
    {
        public String smilPath;
        public String mutiMediaPath;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -