<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                1. Runtime.getRuntime().exec(cmd) > 1. 這個方法執行外部命令,會生成一個新的進程去運行調用的程序,并返回一個java.lang.Process對象,該對象可以得到之前開啟的進程的運行結果,還可以操作進程的輸入輸出流。 > 2. 注意在被調用腳本中,其中的一些路徑問題,有的時候在腳本的環境中,配置的路徑執行可能沒問題,在調用時某些配置文件不寫全路徑可能出現問題 ~~~ package JPythons; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * Created by dailin on 2017/11/6. */ public class JavaCallPython { /** * * @param file 腳本路徑 * @param param 調用腳本函數 * @throws IOException */ public void callPython(String file,String param) throws IOException { String cmd = "python " + file + " " + param; Process proc=Runtime.getRuntime().exec(cmd); //執行py文件 InputStreamReader stdin=new InputStreamReader(proc.getInputStream(),"GBK"); BufferedReader input = new BufferedReader(stdin); String line = null; while((line=input.readLine())!=null ){ System.out.println(line);//得到輸出 } } } ~~~ 2. Process對象有以下幾個方法: > 1. destroy()      殺死這個子進程 > 2. exitValue()      得到進程運行結束后的返回狀態 > 3. waitFor()       得到進程運行結束后的返回狀態,如果進程未運行完畢則等待知道執行完畢 > 4. getInputStream()  得到進程的標準輸出信息流 > 5. getErrorStream()  得到進程的錯誤輸出信息流 > 6. getOutputStream() 得到進程的輸入流 > 現在來講講exitValue(),當線程沒有執行完畢時調用此方法會跑出IllegalThreadStateException異常,最直接的解決方法就是用waitFor()方法代替。 > 但是waitFor()方法也有很明顯的弊端,因為java程序給進程的輸出流分配的緩沖區是很小的,有時候當進程輸出信息很大的時候回導致緩沖區被填滿,如果不及時處理程序會阻塞。如果程序沒有對進程的輸出流處理的會就會導致執行exec()的線程永遠阻塞,進程也不會執行下去直到輸出流被處理或者java程序結束。 > 解決的方法就是處理緩沖區中的信息,開兩個線程分別去處理標準輸出流和錯誤輸出流。 public class ExecTest { public static void main(String[] args) throws IOException, InterruptedException {   String cmd = "cmd /c dir c:\\windows";   final Process process = Runtime.getRuntime().exec(cmd);   printMessage(process.getInputStream());   printMessage(process.getErrorStream());   int value = process.waitFor();   System.out.println(value); } private static void printMessage(final InputStream input) {   new Thread(new Runnable() {   public void run() {     Reader reader = new InputStreamReader(input);     BufferedReader bf = new BufferedReader(reader);     String line = null;     try {     while((line=bf.readLine())!=null) {     System.out.println(line);     }     } catch (IOException e) {     e.printStackTrace();     }  }   }).start(); } } > 如上程序,讀取進程的輸出信息并打印到控制臺就不會發生阻塞,程序能正常的結束。cmd命令不能需要加上cmd /c才能執行,不然java會去path中找dir.exe.在windows一般字符集編碼為GBK,需要在轉換成Reader的時候指定為GBK編碼. 2. python腳本 ~~~ #coding=utf-8 import redis import re from config import * from fdfs_client.client import * import pymysql import traceback # 先把fastdfs導入到本地文件列表 def allFiles(): path = FDFS_STOREPATH rounds = 1 fdfspath = 'group1/M00' with open(LOCAL_FILE,'w') as file_url: for dirpath, dirnames, filenames in os.walk(path): if rounds == 1: rounds+=1 elif (dirpath == path + '/sync'): continue else: for file in filenames: try: paths = re.search(REGEX,dirpath).group(1) fullpath = os.path.join(fdfspath + paths, file) print(fullpath) file_url.write(fullpath + '\n') except: pass rounds+=1 file_url.close() # 把本地文件導入到Redis def toRedis(): redis_client = getRedisClient() with open(LOCAL_FILE, 'r') as logfile: for line in logfile: print(line) redis_client.sadd(FDFS_REDIS_KEY,line.replace('\n', '')) logfile.close() # 直接把fastdfs數據導入到Redis def allFilesToRedis(): redis_client = getRedisClient() path = FDFS_STOREPATH rounds = 1 fdfspath = 'group1/M00' for dirpath, dirnames, filenames in os.walk(path): if rounds == 1: rounds += 1 elif (dirpath == path + '/sync'): continue else: for file in filenames: try: paths = re.search(REGEX, dirpath).group(1) fullpath = os.path.join(fdfspath + paths, file) print(fullpath) redis_client.sadd(FDFS_REDIS_KEY,fullpath) except: pass rounds += 1 # 求交集,并保存為一個set def intersection(): print('==========求交集==========') redis_client = getRedisClient() redis_client.sinterstore(SINTER_KEY,FDFS_REDIS_KEY,MYSQL_REDIS_KEY) # 求差集,并保存為一個set def difference(): redis_client = getRedisClient() redis_client.sdiffstore(DIFF_KEY,FDFS_REDIS_KEY,MYSQL_REDIS_KEY) def getRedisClient(): return redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT,password=REDIS_PASSWORD) # 從Redis中查詢差集,并在fastdfs中刪除文件 def deleteFdfs(): redis_client = getRedisClient() print('刪除%s'%DIFF_KEY) results = redis_client.smembers(DIFF_KEY) fdfs_client = Fdfs_client(FDFS_CLIENT_CONF) print('1') for file in results: print('2') file_str = file.decode() print(file_str) fdfs_client.delete_file(file_str) def info(): client = getRedisClient() mysql_num = client.scard(MYSQL_REDIS_KEY) fdfs_num = client.scard(FDFS_REDIS_KEY) sinter_num = client.scard(SINTER_KEY) diff_num = client.scard(DIFF_KEY) print("mysql中圖片:%d" %mysql_num) print("fastdfs中圖片:%d" %fdfs_num) print("mysql與fastdfs交集中圖片:%d" %sinter_num) print("mysql與fastdfs差集圖片:%d" %diff_num) def mysqlToRedis(): db = pymysql.connect(MYSQL_HOST,MYSQL_USER,MYSQL_PASSWORD,MYSQL_DB) cursor = db.cursor() sql = ('select * from %s' %MYSQL_VIEW) redis_client = getRedisClient() try: cursor.execute(sql) results = cursor.fetchall() for row in results: url = row[0] if (len(url)!=0): print(url) redis_client.sadd(MYSQL_REDIS_KEY,url) except : traceback.print_exc() db.close() # if __name__ == '__main__': # if(sys.argv[1] == 'filetolocal'): # allFiles() # elif(sys.argv[1] == 'fdfs-toredis'): # allFilesToRedis() # elif(sys.argv[1] == 'sinter'): # intersection() # elif(sys.argv[1] == 'delete'): # deleteFdfs() # elif(sys.argv[1] == 'info'): # info() # elif(sys.argv[1] == 'diff'): # difference() # elif (sys.argv[1] == 'mysql-redis'): # mysqlToRedis() # else: # print("USAGE:filetolocal|fdfs-redis|sinter|diff|delete|mysql-redis|info") def main(): if (sys.argv[1] == 'filetolocal'): allFiles() elif (sys.argv[1] == 'fdfs-toredis'): allFilesToRedis() elif (sys.argv[1] == 'sinter'): intersection() elif (sys.argv[1] == 'delete'): deleteFdfs() elif (sys.argv[1] == 'info'): info() elif (sys.argv[1] == 'diff'): difference() elif (sys.argv[1] == 'mysql-redis'): mysqlToRedis() else: print("USAGE:filetolocal|fdfs-redis|sinter|diff|delete|mysql-redis|info") main() ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看