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

📄 convertutil.java

📁 简单的视频上传系统
💻 JAVA
字号:
package cn.com.csuinfosoft.video;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.List;

public class ConvertUtil {

	private static ConvertUtil convertUtil = null;
	
	private ConvertUtil() {
		
	}
	
	public static synchronized ConvertUtil newInstance() {
		if(convertUtil == null) convertUtil = new ConvertUtil();
		return convertUtil;
	}
	
	
	/**
	 * 判断所给的路径是否有效
	 * @param path
	 * @return boolean
	 */
	public boolean checkfile(String path) {
		File file=new File(path);
	     if(!file.isFile()){
	      return false;
	     }
	     return true;		
	}
	
	/**
	 * 通过类加载器获取命令文件所在路径
	 * @param exePath
	 * @return String
	 */
	public String commandFilePath(String exePath) {
        //根据ClassPath上下文路径转换
        URL url = this.getClass().getClassLoader().getResource(exePath);
        String commandPath = url.getFile();
        try {
        	commandPath = java.net.URLDecoder.decode(commandPath,"utf-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		commandPath = commandPath.substring(1, commandPath.length());
		return commandPath;
	}	
	
	/**
	 * 根据传入的子进程执行命令,通过Process对子进程进行调度
	 * @param command
	 */
	public boolean processCommand(List<String> command) {
            ProcessBuilder builder = new ProcessBuilder();
            builder.command(command);
            try {
            final Process process = builder.start();	//开启子进程
            
            /* 创建一个线程单独处理子进程的输入流信息 */
            Thread tmpThread = new Thread(new Runnable() {
            	public void run() {
            		BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
            		try {
						while(br.readLine() != null);
					} catch (IOException e) {
						e.printStackTrace();
					} finally {
						if(br != null) {
							try {
								br.close();
							} catch (IOException e) {
								e.printStackTrace();
							}
						}
					}
            	}
            });
            tmpThread.setDaemon(true);
            tmpThread.start();
            
            /* 创建一个线程单独处理子进程的错误输入流信息(获取输出缓存信息) */
            BufferedReader br2 = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            try {
            String line = null;
            StringBuilder buf = new StringBuilder();
            while((line = br2.readLine()) != null)buf.append(line);
            System.out.println("输出结果为:" + buf);
            } finally {
            	if(br2 != null) {
					try {
						br2.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
            }
            process.waitFor();	//等待子进程执行结束     
            process.destroy();	//子进程执行完后关闭子进程
            return true;
            } catch (IOException ex) {
            	ex.printStackTrace();
            	return false;
            } catch (InterruptedException e) {
				e.printStackTrace();
				return false;
			}
	}
	
}

⌨️ 快捷键说明

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