
今天遇到一个新需求,当从服务器下载文件后用指定的本地程序打开,不知道何时文件下载完成,只能考虑监听文件夹,当有新文件创建的时候打开指定程序。
在此给出一个完整的下载和打开过程:
1、下载文件
jsp页面
<body>
<div>
<a href="<%=basePath%>/user/downLoadFile?fileName=proPlan.DWG" >点击下载</a>
</div>
</body>
java代码
public static void downLoadtFile(HttpServletResponse response, File file) throws IOException
{
response.reset();
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Content-disposition",
"attachment; filename=" + new String(file.getName().getBytes(), "iso-8859-1"));
OutputStream outputStream = response.getOutputStream();
InputStream in = new FileInputStream(file);
byte[] b = new byte[1024];
int len = 0;
while ((len = in.read(b)) > 0)
{
outputStream.write(b, 0, len);
}
outputStream.write(b);
outputStream.flush();
in.close();
}
2、监听文件夹,执行打开程序
package demo; import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; public class FolderListener {
private static ExecutorService fixedThreadPool = Executors.newCachedThreadPool();
private WatchService ws;
private String listenerPath;
private FolderListener(String path) {
try {
ws = FileSystems.getDefault().newWatchService();
this.listenerPath = path;
start();
} catch (IOException e) {
e.printStackTrace();
}
} private void start() {
fixedThreadPool.execute(new Listner(ws,this.listenerPath));
} public static void addListener(String path) throws IOException {
FolderListener resourceListener = new FolderListener(path);
Path p = Paths.get(path);
//注册监听事件,文件的修改、删除和创建
p.register(resourceListener.ws,
StandardWatchEventKinds.ENTRY_MODIFY,
StandardWatchEventKinds.ENTRY_DELETE,
StandardWatchEventKinds.ENTRY_CREATE);
} public static void main(String[] args) throws IOException {
//监听下载目录的变化
FolderListener.addListener("C:\\Users\\Administrator\\Downloads\\");
}
} class Listner implements Runnable {
private WatchService service;
private String rootPath; public Listner(WatchService service,String rootPath) {
this.service = service;
this.rootPath = rootPath;
} public void run() {
try {
while(true){
WatchKey watchKey = service.take();
List<WatchEvent<?>> watchEvents = watchKey.pollEvents();
for(WatchEvent<?> event : watchEvents){
if(event.context().toString().endsWith(".DWG"))
// 根据事件类型采取不同的操作。。。。。。。
try {
System.out.println("["+rootPath+event.context()+"]文件发生了["+event.kind()+"]事件"+ event.count());
String[] cmd = { "D:\\cad\\AutoCAD\\acad.exe", "C:\\Users\\Administrator\\Downloads\\" + event.context().toString() };
Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
e.printStackTrace();
}
}
watchKey.reset();
}
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
System.out.println("fdsfsdf");
try {
service.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
}
补充,除了执行指定exe,打开软件外还可以执行命令行
package demo; import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader; public class Command { public static void main(String[] args) {
String s = exeCmd("ipconfig");
System.out.println(s);
} public static String exeCmd(String commandStr) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
try {
Process p = Runtime.getRuntime().exec(commandStr);
br = new BufferedReader(new InputStreamReader(p.getInputStream(), "gb2312"));
// InputStream in = p.getInputStream();
// byte[] b = new byte[1024];
// int len = 0;
// while((len = in.read(b)) > 0){
// sb.append(new String(b,"gb2312")).append("\n");
// }
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
} catch (Exception e) {
e.printStackTrace();
}
finally
{
if (br != null)
{
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
}