Administrator
2022-09-14 58d006e05dcf2a20d0ec5367dd03d66a61db6849
提交 | 用户 | 时间
58d006 1 package com.mandi.servlet.file.impl;
A 2
3 import java.io.File;
4 import java.io.InputStream;
5 import java.net.URL;
6 import java.nio.ByteBuffer;
7 import java.nio.channels.Channels;
8 import java.nio.channels.ReadableByteChannel;
9 import java.util.ArrayList;
10 import java.util.List;
11 import java.util.Map;
12 import java.util.concurrent.locks.Lock;
13 import java.util.concurrent.locks.ReentrantLock;
14
15 import org.apache.log4j.Logger;
16 import org.dom4j.Document;
17 import org.dom4j.DocumentException;
18 import org.dom4j.Element;
19 import org.dom4j.io.SAXReader;
20 import org.springframework.context.annotation.Scope;
21 import org.springframework.stereotype.Service;
22
23
24
25
26 @Service("flashpaper")
27 @Scope(value="singleton")
28 public class Flashpaper extends Thread{
29     private Logger log=Logger.getLogger(Flashpaper.class);
30     private List<File> list=null;
31     private Lock lock=null;
32     private Object obj=null;
33     private boolean executeflag=false;
34     private String flashpaperurl=null;
35     private String findflashpaper()
36     {
37         @SuppressWarnings("rawtypes")
38         Class tc=Flashpaper.class;
39         URL turl=tc.getResource("/");//get the root path of class ,like the location where struts.xml is
40         if(turl==null)
41             return null;
42         File file=new File(turl.getPath()+"flashpaper.xml");
43         if(!file.exists())
44             return null;
45         SAXReader reader=new SAXReader();
46         try {
47             Document doc=reader.read(file);
48             Element ele=doc.getRootElement();
49             if(ele==null)
50                 return null;
51             Element ele1=ele.element("flashpaher");
52             if(ele1==null)
53                 return null;
54             String path=ele1.getTextTrim();
55             if(path!=null&&!path.isEmpty())
56                 return path;
57         } catch (DocumentException e) {
58             log.error("findflashpaper error");
59         }
60         return null;
61     }
62     public Flashpaper() {
63         flashpaperurl=this.findflashpaper();
64         log.info(flashpaperurl);
65         list=new ArrayList<File>();
66         lock=new ReentrantLock();
67         obj=new Object();
68         executeflag=true;
69         this.start();
70     }
71     public boolean printFile(File file)
72     {
73         if(!file.exists())
74             return false;
75         lock.lock();
76         this.list.add(file);
77         lock.unlock();
78         synchronized (obj) {
79             obj.notify();
80         }
81         return true;
82     }
83     @Override
84     public void run() {
85         // TODO Auto-generated method stub
86         log.info("flashpaper start thread!!");
87 //        final Logger log1=this.log;
88         ProcessBuilder processbuilder=new ProcessBuilder();
89         Map<String, String> envmap=processbuilder.environment();
90         envmap.clear();
91         envmap.putAll(System.getenv());
92         while(executeflag)
93         {
94             File file=null;
95             lock.lock();
96             if(list.size()>0)
97             {
98                 file=list.get(0);
99                 list.remove(0);
100             }
101             lock.unlock();
102             if(file==null)
103             {
104                 synchronized (obj) {
105                     try {
106                         log.info("start waiting ...");
107                         obj.wait();
108                         log.info("waking...");
109                     } catch (InterruptedException e) {
110                         // TODO Auto-generated catch block
111                         e.printStackTrace();
112                     }
113                 }
114                 continue;
115             }
116             String cmd=this.flashpaperurl;
117             List<String> command = new java.util.ArrayList<String>(); 
118             command.add(cmd);
119             command.add(file.getPath());
120             command.add("-o");
121             command.add(file.getPath()+".swf");
122             cmd=cmd+" "+file.getPath()+" -o "+file.getPath()+".swf"; 
123             Process process=null;
124             File fileparent=file.getParentFile();
125             try {
126                 if(fileparent.exists()&& fileparent.isDirectory())
127                     processbuilder.directory(fileparent);
128                 process=processbuilder.command(command).start();
129                 if(process==null)
130                     log.warn("can not start flashpapper");
131                 final InputStream is=process.getInputStream();
132                 final InputStream error=process.getErrorStream();
133                 final ByteBuffer bytebuffer=ByteBuffer.allocate(1024);
134                 final ByteBuffer bytebuffer1=ByteBuffer.allocate(1024);
135                 Thread th=new Thread(){
136                     @Override
137                     public void run() {
138                         // TODO Auto-generated method stub
139                         ReadableByteChannel channel=Channels.newChannel(is);
140                         try {
141                             while(channel.read(bytebuffer)!=-1)
142                             {
143                                 bytebuffer.flip();
144                                 bytebuffer.clear();
145                             }
146                         } catch (Exception e) {
147                             // TODO: handle exception
148                             log.error("can't convert");
149                             e.printStackTrace();
150                         }
151                     }
152                 };
153                 Thread th1=new Thread(){
154                     
155                     @Override
156                     public void run() {
157                         // TODO Auto-generated method stub
158                         ReadableByteChannel channel1=Channels.newChannel(error);
159                         try {
160                             while(channel1.read(bytebuffer)!=-1)
161                             {
162                                 bytebuffer1.flip();
163                                 bytebuffer1.clear();
164                             }
165                         } catch (Exception e) {
166                             // TODO: handle exception
167                             log.error("can't convert");
168                             e.printStackTrace();
169                         }
170                     }
171                 };
172                 th.setDaemon(true);
173                 th.start();
174                 th1.setDaemon(true);
175                 th1.start();
176                 try{
177                     int value=process.waitFor();
178                     log.info("process waitfor code is:"+value);
179                 }catch (Exception e) {
180                     // TODO: handle exception
181                     log.error("waitfor error"+e.getMessage());
182                 }
183             }catch (Exception e) {
184                 // TODO: handle exception
185                 log.error("flashpaper can't convert the file"+e.getMessage());
186             } finally{
187                 if(process!=null)
188                     process.destroy();
189             }
190         }
191         if(executeflag==false)
192         {
193             this.list.clear();
194         }
195     }
196     
197     public void stopExecute()
198     {
199         this.executeflag=false;
200     }
201     public String getFlashpaperurl() {
202         return flashpaperurl;
203     }
204     public void setFlashpaperurl(String flashpaperurl) {
205         this.flashpaperurl = flashpaperurl;
206     }
207 }