1.获取本地主机的IP地址
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.*;
import javax.swing.*;
@SuppressWarnings("serial")
//如果编译器出现这样的警告信息:The serializable class WmailCalendar does not declare a static final serialVersionUID field of type long
//使用这个注释将警告信息去掉
public class GetLocalHostIpFrame extends JFrame{
private JTextField tf_ip;//单行文本输入框
public static void main(String args[]){
EventQueue.invokeLater(new Runnable(){
public void run(){
try{
GetLocalHostIpFrame frame=new GetLocalHostIpFrame();
frame.setVisible(true);
}catch(Exception e){
e.printStackTrace();
}
}
});
}
//创建窗口
public GetLocalHostIpFrame(){
super();
getContentPane().setLayout(null);
setTitle("获取本地主机的IP地址");
setBounds(100,100,340,211);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JButton button=new JButton();
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try{
InetAddress inetAddr=InetAddress.getLocalHost();
//创建本地主机的InetAddress对象
String ip=inetAddr.getHostAddress();
//获得本地主机的IP地址
tf_ip.setText(ip);
}catch(UnknownHostException e1){
e1.printStackTrace();
//命令行打印异信息程序错位置及原因
}
}
});
button.setText("获取IP地址");
button.setBounds(29,113,106,28);
getContentPane().add(button);
final JLabel label=new JLabel();
label.setForeground(new Color(0,0,255));
label.setFont(new Font("",Font.BOLD,16));
label.setText("获取本地主机的IP地址");
label.setBounds(73,22,171,35);
getContentPane().add(label);
final JLabel label_1=new JLabel();
label_1.setText("IP地址:");
label_1.setBounds(29,75,66,18);
getContentPane().add(label_1);
tf_ip=new JTextField();
tf_ip.setBounds(88,73,199,22);
getContentPane().add(tf_ip);//在文本框中显示ip地址
final JButton button_1=new JButton();
button_1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
button_1.setText("退出系统");
button_1.setBounds(181,113,106,28);
getContentPane().add(button_1);
}
}
2.通过IP地址获取域名和主机名
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.*;
@SuppressWarnings("serial")
public class ByIpGainDomainFrame extends JFrame{
JTextField tf_ip;
JTextField tf_canonical;
JTextField tf_host;
public static void main(String args[]){
EventQueue.invokeLater(new Runnable(){
public void run(){
try{
ByIpGainDomainFrame frame=new ByIpGainDomainFrame();
frame.setVisible(true);
}catch(Exception e){
e.printStackTrace();
}
}
});
}
public ByIpGainDomainFrame(){
super();
this.getContentPane().setLayout(null);
this.setTitle("通过IP地址获得域名和主机名");
this.setBounds(100,100,333,218);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button=new JButton();
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try{
String ip=tf_ip.getText();//获取IP地址
// System.out.println(ip);
String[] ipStr=ip.split("[.]");
byte[] ipBytes=new byte[4];
for(int i=0;i<4;i++){
int m=Integer.parseInt(ipStr[i]);
byte b=(byte)(m&0xFF);//byte是个八个字节
ipBytes[i]=b;
}
InetAddress inetAddr=InetAddress.getByAddress(ipBytes);
String canonical=inetAddr.getCanonicalHostName();//获取域名
String host=inetAddr.getHostName();//获取主机名
tf_canonical.setText(canonical);
tf_host.setText(host);
}catch(UnknownHostException e1){
e1.printStackTrace();
}
}
});
button.setText("获取域名和主机名");
button.setBounds(28,136,150,28);
this.getContentPane().add(button);
JLabel label=new JLabel();
label.setForeground(new Color(0,0,255));
label.setFont(new Font("",Font.BOLD,16));
label.setText("通过IP地址获得域名和主机名");
label.setBounds(51,10,223,35);
this.getContentPane().add(label);
JLabel label_1=new JLabel();
label_1.setText("主 机 名");
label_1.setBounds(28,110,66,18);
this.getContentPane().add(label_1);
tf_host=new JTextField();
tf_host.setBounds(87,108,199,22);
this.getContentPane().add(tf_host);
JButton button_1=new JButton();
button_1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
button_1.setText("退出系统");
button_1.setBounds(191,136,95,28);
this.getContentPane().add(button_1);
JLabel label_2=new JLabel();
label_2.setText("域 名");
label_2.setBounds(28,82,66,18);
this.getContentPane().add(label_2);
tf_canonical=new JTextField();
tf_canonical.setBounds(87,80,199,22);
this.getContentPane().add(tf_canonical);
JLabel label_3=new JLabel();
label_3.setText("输入IP地址:");
label_3.setBounds(10,51,84,18);
this.getContentPane().add(label_3);
tf_ip=new JTextField();
tf_ip.setBounds(87,52,199,22);
this.getContentPane().add(tf_ip);
}
}
3.获得内网所有IP地址
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.util.*;
public class GainAllIpFrame extends JFrame{
JTextArea ta_allIp;
public Hashtable<String,String> pingMap;
public static void main(String args[]){
GainAllIpFrame frame=new GainAllIpFrame();
frame.setVisible(true);
}
public void gainAllIp() throws Exception{//获得所有IP并在文本域中显示
InetAddress host=InetAddress.getLocalHost();//获取本机的InetAddress对象
String hostAddress=host.getHostAddress();//获得本机的IP地址
System.out.println("本机IP地址是: "+hostAddress);
int pos=hostAddress.lastIndexOf(".");//IP地址中最后一个.的位置
String wd=hostAddress.substring(0,pos+1);//对IP地址进行截取 获取网段
for(int i=1;i<=255;i++){
String ip=wd+i;//生成ip地址
PingIpThread thread=new PingIpThread(ip);//创建进程对象
thread.start();
}
Set<String> set=pingMap.keySet();//获得集合中键的SET视图
Iterator<String> it=set.iterator();//获得迭代器对象
while(it.hasNext()){
String key=it.next();
String value=pingMap.get(key);//获得指定建的值
if(value.equals("true")){
ta_allIp.append(key+"\n");//追加显示IP地址
}
}
}
public GainAllIpFrame(){
super();
addWindowListener(new WindowAdapter(){
public void windowOpened(WindowEvent e){
try{
gainAllIp();
ta_allIp.setText(null);
}catch(Exception e1){
ta_allIp.setText(null);
}
}
});
setTitle("获得内网的所有IP地址");
this.setBounds(400,200,270,375);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane scrollPane=new JScrollPane();
getContentPane().add(scrollPane,BorderLayout.CENTER);
ta_allIp=new JTextArea();
this.getContentPane().add(ta_allIp);
JPanel panel=new JPanel();
this.getContentPane().add(panel,BorderLayout.NORTH);
JButton button=new JButton();
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try{
System.out.println("按下显示所有IP");
ta_allIp.setText(null);
gainAllIp();
}catch(Exception e1){
ta_allIp.setText(null);
JOptionPane.showMessageDialog(null,"应用程序异常");
}
}
});
button.setText("显示所有IP");
panel.add(button);
JButton button_1=new JButton();
button_1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
button_1.setText("退 出");
panel.add(button_1);
// JButton button_2=new JButton();
// button_2.setText("New JButton");
// panel.add(button_2);
pingMap=new Hashtable<String,String>();
}
class PingIpThread extends Thread{
public String ip;
public PingIpThread(String ip){
this.ip=ip;
}
public void run(){
try{
Process process=Runtime.getRuntime().exec(
"ping"+ip+" -w 10 -n 1");
InputStream is=process.getInputStream();
InputStreamReader isr=new InputStreamReader(is);
BufferedReader in=new BufferedReader(isr);
String line=in.readLine();
while(line!=null){
if(line!=null&&!line.equals("")){
if(line.substring(0,2).equals("来自")
||(line.length()>10&&line.substring(0,10).equals("Reply from"))){
pingMap.put(ip,"true");
}
}
line=in.readLine();
}
}catch(IOException e){
}
}
}
}
4.网络资源的单线程下载
import javax.swing.*;
import org.omg.CORBA_2_3.portable.InputStream;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileOutputStream;
import java.net.*;
public class SingleThreadDownloadFrame extends JFrame{
private JTextField tf_address;
public static void main(String args[]){
EventQueue.invokeLater(new Runnable(){
public void run(){
try{
SingleThreadDownloadFrame frame=new SingleThreadDownloadFrame();
frame.setVisible(true);
}catch(Exception e){
e.printStackTrace();
}
}
});
}
public SingleThreadDownloadFrame(){
super();
this.getContentPane().setLayout(null);
this.setTitle("网络资源单线程下载");
this.setBounds(100,100,500,237);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label=new JLabel();
label.setText("网络资源的网址: ");
label.setBounds(10,88,118,18);
this.getContentPane().add(label);
tf_address=new JTextField();
tf_address.setBounds(117,86,357,22);
this.getContentPane().add(tf_address);
JButton button=new JButton();
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String address=tf_address.getText().trim();//获得网址 去除空格
download(address);
System.out.println("开始下载了: "+address);
}
});
button.setText("单击开始下载");
button.setBounds(41,144,145,28);
this.getContentPane().add(button);
JButton button_1=new JButton();
button_1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
tf_address.setText(null);
tf_address.requestFocus();//文本框获得焦点
}
});
button_1.setText("清 空");
button_1.setBounds(204,144,106,28);
this.getContentPane().add(button_1);
JButton button_2=new JButton();
button_2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
button_2.setText("退 出");
button_2.setBounds(328,144,106,28);
this.getContentPane().add(button_2);
JLabel label_1=new JLabel();
label_1.setForeground(new Color(0,0,255));
label_1.setFont(new Font("",Font.BOLD,24));
label_1.setText("网络资源的单线程下载");
label_1.setBounds(117,21,301,48);
this.getContentPane().add(label_1);
}
public void download(String urlAddr){
try{
URL url=new URL(urlAddr);
URLConnection urlConn=url.openConnection();//获得连接对象
urlConn.connect();
java.io.InputStream in=urlConn.getInputStream();
String filePath=url.getFile();
int pos=filePath.lastIndexOf("/");
String fileName=filePath.substring(pos+1);
System.out.println("fileName: "+fileName);
FileOutputStream out=new FileOutputStream("C:/Users/Administrator/Desktop/"+fileName);
byte[] bytes=new byte[1024];
int len=in.read(bytes);
while(len!=-1){
out.write(bytes,0,len);
len=in.read(bytes);
}
out.close();
in.close();
JOptionPane.showMessageDialog(null, "下载完毕");
}catch(Exception e){
e.printStackTrace();
}
}
}
5.网络资源的多线程下载
给资源设置断点 分给不同的线程进行下载
MultiThreadDownFrame.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.HttpURLConnection;
import java.net.URL;
public class MultiThreadDownFrame extends JFrame{
private JTextField tf_address;
public static void main(String args[]){
EventQueue.invokeLater(new Runnable(){
public void run(){
try{
MultiThreadDownFrame frame=new MultiThreadDownFrame();
frame.setVisible(true);
}catch(Exception e){
e.printStackTrace();
}
}
});
}
public MultiThreadDownFrame(){
super();
this.getContentPane().setLayout(null);
this.setBounds(100,100,482,189);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button=new JButton();
button.setBounds(10,95,106,28);
this.getContentPane().add(button);
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try{
String address=tf_address.getText();
int pos=address.lastIndexOf("/");
String fileName=address.substring(pos+1);
download(address,"c:\\"+fileName,2);
}catch(Exception e1){
e1.printStackTrace();
}
}
});
button.setText("下 载");
JLabel label=new JLabel();
label.setText("网络资源的地址: ");
label.setBounds(10,44,106,18);
this.getContentPane().add(label);
tf_address=new JTextField();
tf_address.setBounds(114,42,341,22);
this.getContentPane().add(tf_address);
JButton button_1=new JButton();
button_1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
tf_address.setText(null);
}
});
button_1.setText("清 空");
button_1.setBounds(179,95,106,28);
this.getContentPane().add(button_1);
JButton button_2=new JButton();
button_2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
button_2.setText("退 出");
button_2.setBounds(349, 95, 106, 28);
getContentPane().add(button_2);
}
public void download(String url,String dest,int threadNum) throws Exception{
URL downURL=new URL(url);
HttpURLConnection conn=(HttpURLConnection) downURL.openConnection();
//打开网络连接
long fileLength=-1;//用于存储文件长度
int stateFlagCode=conn.getResponseCode();//获得连接状态标记码
if(stateFlagCode==200){
fileLength=conn.getContentLength();
conn.disconnect();
}
if(fileLength>0){
long byteCounts=fileLength/threadNum+1;//计算每个线程的字节数
File file=new File(dest);
for(int i=0;i<threadNum;i++){
long startPosition=byteCounts*i;
long endPosition=byteCounts*(i+1);
if(i==threadNum-1){
DownMultiThread fileThread=new DownMultiThread(url,file,startPosition,0);
new Thread(fileThread).start();
}else {
DownMultiThread fileThread=new DownMultiThread(url,file,startPosition,endPosition);
new Thread(fileThread).start();
}
}
JOptionPane.showMessageDialog(null,"完成网络资源的下载");
}
}
}
DownMultiThread.java
import java.io.*;
import java.net.*;
import javax.swing.JOptionPane;
public class DownMultiThread implements Runnable{
private String sUrl="";//网络资源地址
private File desFile;//需要写入的目标文件对象
private long startPos;//写入的开始位置
private long endPos;
public DownMultiThread(String sUrl,File desFile,long startPos,long endPos){
this.sUrl=sUrl;
this.desFile=desFile;
this.startPos=startPos;
this.endPos=endPos;
}
@Override
public void run() {
// TODO Auto-generated method stub
try{
URL url=new URL(sUrl);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setRequestProperty("User-Agent", "NetFox");//设置断点续传
String rangeProperty="bytes="+startPos+"-";
if(endPos>0){
rangeProperty="bytes="+startPos+"-"+endPos;
}
conn.setRequestProperty("RANGE", rangeProperty);
RandomAccessFile out=new RandomAccessFile(desFile,"rw");
out.seek(startPos);
InputStream in=conn.getInputStream();
BufferedInputStream bin=new BufferedInputStream(in);
byte[] buff=new byte[2048];
int len=-1;
len=bin.read(buff);
while(len!=-1){
out.write(buff,0,len);
len=bin.read(buff);
}
out.close();
bin.close();
conn.disconnect();
}catch(Exception e)
{
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
}