好友
阅读权限25
听众
最后登录1970-1-1
|
前几天用java写的迅雷防踢助手,应网友要求,现将源代码发出,我写的代码不是最漂亮的,所以大神们就绕道吧,和我一样初学java并且喜欢GUI编程的同志可以研究一下,共同学习共同进步,如有更好的建议,请多多指教!
以下是防踢助手代码:
1、寻找迅雷安装路径代码:
[Java] 纯文本查看 复制代码 /**
* 功能:查找文件绝对路径
* 作者:Cary-云飞扬
* 时间:2016年3月28日
*/
package com.cary.service;
import java.io.File;
public class FindThunderPath {
public static String thunderPathName = "";
// public static void main(String[] args) {
// getPath("yanyantan.txt");
// System.out.println("结果为:"+thunderPathName);
//
// }
/**
* 根据指定的文件名查找该文件所在绝对路径
* @param fileName文件名称
* @return
*/
public static String getPath(String fileName){
rootFindPath(fileName);
return thunderPathName;
}
/**
* 根据盘符搜索指定文件名的文件所在的路径
*
* @param fileName文件名称
*/
public static void rootFindPath(String fileName) {
File[] roots = File.listRoots();
for (int i = 0; i < roots.length; i++) {
String rootName = roots[i].toString();
find(rootName, fileName);
if (thunderPathName.length() > 4) {
break;
}
// System.out.println(roots[i].toString() + "已经搜索完毕!");
}
}
/**
* 根据路径名称查找指定文件名的文件所在路径
*
* @param pathname
* 路径名称
* @param fileName
* 文件名称
*/
public static void find(String pathname, String fileName) {
File file = new File(pathname);
File[] fs = file.listFiles();
if (fs == null) {
return;
}
for (File f : fs) {
if (f == null) {
continue;
}
if (f.isFile() && f.getName().equals(fileName)) {
thunderPathName = f.getAbsolutePath();
} else if (f.isDirectory()) {
find(f.getAbsolutePath(), fileName);
}
}
}
}
2、寻找迅雷两个服务进程代码:
[Java] 纯文本查看 复制代码 package com.cary.service;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
public class PidPath {
//定义迅雷thunder.exe与thunderplatform.exe的路径
public String pathName;
static Object obj = new Object();
/**
* 寻找进程路径函数
* @param pidName进程名称,例如:thunder.exe;
* @throws IOException
*/
public void searchPidPath(String pidName) throws IOException {
synchronized (obj) {
String pid = "cmd /c wmic process where name='" + pidName
+ "' get executablepath /value";
Process p = Runtime.getRuntime().exec(pid);
InputStream is = p.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
byte[] buf = new byte[4096];
while (is.read(buf) != -1) {
String tempName = new String(buf).trim();
pathName = tempName.replace("ExecutablePath=", "").replace(
"\\", "\\\\");
}
while(true){
if(pathName.length()<=4){
// System.out.println("进入如果条件不等式");
searchPidPath(pidName);
}else {
// System.out.println("进入其他条件了!!!");
break;
}
}
bis.close();
is.close();
}
}
}
3、写入防踢补丁代码:
[Java] 纯文本查看 复制代码 package com.cary.service;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class WriteFileToPath {
static Object lock = new Object();
/**
* @param pathname 写入文件的位置
* @param fileName 文件名称
* @throws FileNotFoundException
* @throws IOException
*/
public static void InstallPatch(String pathname,String fileName)
throws FileNotFoundException, IOException {
synchronized (lock) {
//被写入文件的路径
String sourceName = "File\\zlib1.dll";
//读取被写入的文件
File pathFile = new File(pathname);
//将文件写入的路径
File sourceFile = new File(sourceName);
//读取被写入文件输入流
FileInputStream fis = new FileInputStream(sourceFile);
//文件输出流,将文件写入目的路径
FileOutputStream fos = new FileOutputStream(pathFile + "\\"
+ fileName);
byte[] buf = new byte[1024];
int len = 0;
while ((len = fis.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fos.close();
fis.close();
}
}
}
4、获得迅雷两个服务进程路径代码:
[Java] 纯文本查看 复制代码 /**
* 功能: 1、获取thunderplatform.exe进程所在路径
* 2、获取Thunder.exe进程所在路径
*/
package com.cary.util;
import java.io.IOException;
import com.cary.service.PidPath;
public class GetPidPathName {
public String platformPath;
public String thunderPath;
public void getPidPath(String pidName1,String pidName2) {
PidPath pid = new PidPath();
try {
pid.searchPidPath(pidName1);
platformPath = pid.pathName.replace("\\\\" + pidName1, "");
} catch (IOException e) {
e.printStackTrace();
}
try {
pid.searchPidPath(pidName2);
if (pid.pathName.endsWith("\\\\thunder.exe")) {
thunderPath = pid.pathName.replace("\\\\thunder.exe", "");
} else {
thunderPath = pid.pathName.replace("\\\\Thunder.exe", "");
}
} catch (IOException e) {
e.printStackTrace();
}
while(true){
if(thunderPath.length()<=4||platformPath.length()<=4){
// System.out.println("双保险条件不等式!");
getPidPath(pidName1, pidName2);
}else {
// System.out.println("双保险其他条件不等式!");
break;
}
}
}
}
5、获得迅雷启动程序路径代码:
[Java] 纯文本查看 复制代码 /**
* 功能:获取迅雷应用程序所在路径
* @author 云飞扬
*
*/
package com.cary.util;
import com.cary.service.FindThunderPath;
public class ThunderPath {
public static String thunderPath(String thunder){
String tempName = FindThunderPath.getPath(thunder);
String pathName = tempName.replace("\\", "\\\\");
return pathName;
}
}
6、启动迅雷与关闭迅雷代码:
[Java] 纯文本查看 复制代码 /**
* 功能: 1、根据Thunder.exe所在路径开启迅雷应用程序
* 2、关闭正在运行的迅雷应用程序
*/
package com.cary.util;
import java.io.IOException;
public class ThunderUtil {
// public static void main(String[] args) {
// String path = ThunderPath.getPath();
// thunderStart(path);
// thunderStop();
//
//
// }
/**
* 启动迅雷函数
* @param thunderPath
*/
public static void thunderStart(String thunderPath) {
try {
Process process = Runtime.getRuntime().exec(thunderPath);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 关闭迅雷函数
*/
public static void thunderStop() {
try {
Process process = Runtime.getRuntime().exec(
"cmd /c taskkill /f /t /im thunder.exe");
} catch (IOException e) {
e.printStackTrace();
}
//关闭后延时5秒,保证迅雷所有服务都关闭
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
7、主界面代码,主界面是由windowbuilder设计的,并不是自己敲的。
[Java] 纯文本查看 复制代码 package com.cary.mainFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import com.cary.service.PidPath;
import com.cary.service.WriteFileToPath;
import com.cary.util.GetPidPathName;
import com.cary.util.ThunderPath;
import com.cary.util.ThunderUtil;
import javax.swing.JLabel;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Point;
public class ThunderFrame extends JFrame implements ActionListener,
MouseListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JButton button;
private JButton button_1;
private JTextArea textArea;
private JScrollPane scrollPane;
private String path;// 迅雷程序所在路径
private JButton button_2;
private String pidPathName1;
private String pidPathName2;
/**
* Create the frame.
*/
public ThunderFrame() {
// ImageIcon icon = new ImageIcon("File\\logo.jpg");
Toolkit kit = Toolkit.getDefaultToolkit();
Image image = kit.getImage("File\\logo.png");
setIconImage(image);
setTitle("\u8FC5\u96F7\u9632\u8E22\u52A9\u624BV1.0\u7248");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 545, 348);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(10, 10, 509, 289);
contentPane.add(panel);
panel.setLayout(null);
JPanel panel_1 = new JPanel();
panel_1.setBorder(new TitledBorder(null, "\u529F\u80FD\u533A",
TitledBorder.LEADING, TitledBorder.TOP, null, null));
panel_1.setBounds(10, 10, 489, 119);
panel.add(panel_1);
panel_1.setLayout(null);
button = new JButton("\u521D\u59CB\u5316\u8FC5\u96F7");
button.addActionListener(this);
button.addMouseListener(this);
button.setBounds(26, 37, 119, 32);
panel_1.add(button);
button_1 = new JButton("\u5B89\u88C5\u9632\u8E22\u8865\u4E01");
button_1.setEnabled(false);
button_1.addActionListener(this);
button_1.addMouseListener(this);
button_1.setBounds(188, 37, 119, 32);
panel_1.add(button_1);
button_2 = new JButton("\u91CD\u542F\u8FC5\u96F7");
button_2.setEnabled(false);
button_2.addActionListener(this);
button_2.setBounds(352, 37, 107, 32);
panel_1.add(button_2);
JLabel author = new JLabel(
" \u4F5C\u8005\uFF1Acary-\u4E91\u98DE\u626C \u543E\u7231\u7834\u89E3www.52pojie.cn");
author.setBounds(26, 79, 433, 30);
// author.getFont();
panel_1.add(author);
JPanel panel_2 = new JPanel();
panel_2.setBorder(new TitledBorder(null, "\u4FE1\u606F\u533A",
TitledBorder.LEADING, TitledBorder.TOP, null, null));
panel_2.setBounds(10, 139, 489, 140);
panel.add(panel_2);
panel_2.setLayout(null);
scrollPane = new JScrollPane();
scrollPane.setBounds(10, 21, 469, 109);
panel_2.add(scrollPane);
textArea = new JTextArea();
scrollPane.setViewportView(textArea);
textArea.setText(this.getTime() + "迅雷防踢助手准备就绪,可以进行初始化!" + "\r\n");
textArea.setEditable(false);
textArea.setCaretPosition(textArea.getText().length());
// getTime();
}
/**
* 获取系统当前时间
*/
public String getTime() {
SimpleDateFormat df = new SimpleDateFormat(
" [yyyy-MM-dd HH:mm:ss] ");// 设置日期格式
String time = df.format(new Date());
return time;
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
//搜索迅雷所在路径
path = ThunderPath.thunderPath("Thunder.exe");
//根据迅雷路径启动迅雷程序
textArea.append(this.getTime() + "迅雷所在目录:" +path+ "\r\n");
//启动迅雷
ThunderUtil.thunderStart(path);
//根据进程名称寻找进程所在路径
String pidName1 = "thunderplatform.exe";
String pidName2 = "thunder.exe";
GetPidPathName pid = new GetPidPathName();
pid.getPidPath(pidName1, pidName2);
pidPathName1 = pid.platformPath;
pidPathName2 = pid.thunderPath;
textArea.append(this.getTime() + "初始化完毕,可以进行安装防踢补丁!" + "\r\n");
textArea.append(this.getTime() + "补丁将安装在以下目录:" + "\r\n"+pidPathName1+ "\r\n"+pidPathName2+ "\r\n");
button.setEnabled(false);
//获取进程路径后关闭迅雷
ThunderUtil.thunderStop();
button_1.setEnabled(true);
} else if (e.getSource() == button_1) {
//安装补丁
String fileName1 = "zlib1.dll";
String fileName2 = "zlibrepair.dll";
try {
WriteFileToPath.InstallPatch(pidPathName1, fileName1);
WriteFileToPath.InstallPatch(pidPathName2, fileName1);
WriteFileToPath.InstallPatch(pidPathName2, fileName2);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
textArea.append(this.getTime() + "防踢补丁安装完毕,可以重启迅雷!" + "\r\n");
button_1.setEnabled(false);
button_2.setEnabled(true);
} else if (e.getSource() == button_2) {
ThunderUtil.thunderStart(path);
textArea.append(this.getTime() + "迅雷重启完毕,祝您下载愉快!" + "\r\n");
button_2.setEnabled(false);
button.setEnabled(true);
}
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
if (e.getSource() == button) {
textArea.append(this.getTime() + "正在初始化迅雷,请稍后..." + "\r\n");
} else if (e.getSource() == button_1) {
textArea.append(this.getTime() + "补丁正在安装中,请稍后..." + "\r\n");
}
}
@Override
public void mouseReleased(MouseEvent e) {
}
}
7、最后,启动主界面代码:
[Java] 纯文本查看 复制代码 package com.cary.app;
import com.cary.mainFrame.ThunderFrame;
public class ThunderApp {
public static void main(String[] args) {
ThunderFrame frame = new ThunderFrame();
frame.setLocation(500, 300);
frame.setVisible(true);
}
}
|
免费评分
-
查看全部评分
|