验证中...
语言: Java
分类: 编程语言基础
最后更新于 2018-05-17 11:00
片段 1 片段 2 片段 3
发送端
原始数据 复制代码
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/**
* 发送端
* @author 龚贞斌
*
*/
public class Test {
public static void main(String[] args) {
DatagramSocket datagramSocket=null;
try {
//创建一个套接字
datagramSocket=new DatagramSocket(8888);
String str="我爱你";
try {
datagramSocket.send(new DatagramPacket(str.getBytes(),str.getBytes().length,InetAddress.getByName("127.0.0.1"),10000));
// datagramSocket.send(new DatagramPacket(str.getBytes(),str.getBytes().length,InetAddress.getByName("127.0.0.1"),10000));
}catch(Exception e) {
e.printStackTrace();
}
}catch(Exception e) {
e.printStackTrace();
}finally {
try {
if(datagramSocket!=null){
datagramSocket.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
接收端
原始数据 复制代码
import java.net.DatagramPacket;
import java.net.DatagramSocket;
/**
* 接收端
* @author 龚贞斌
*
*/
public class Test2 {
public static void main(String[] args) {
DatagramSocket datagramSocket=null;
try {
datagramSocket=new DatagramSocket(10000);
try {
byte [] data=new byte[1024];
DatagramPacket packet=new DatagramPacket(data, data.length);
datagramSocket.receive(packet);
byte [] bys=packet.getData();
String str=new String(bys);
System.out.println(str);
}catch(Exception e) {
e.printStackTrace();
}
}catch(Exception e) {
e.printStackTrace();
}finally {
try {
if(datagramSocket!=null){
datagramSocket.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
URL的初步应用
原始数据 复制代码
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
/**
* URL的简单应用----------图片下载
* @author 龚贞斌
*
*/
public class Test5 {
public static void main(String[] args) {
try {
Down.down("https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=%E8%9C%80%E5%B1%B1%E5%9B%BE%E7%89%87&step_word=&hs=0&pn=34&spn=0&di=71780645440&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=2&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=-1&cs=390910560%2C1882616410&os=2253801521%2C3500278093&simid=4109411019%2C493796734&adpicid=0&lpn=0&ln=1967&fr=&fmq=1526522985673_R&fm=result&ic=0&s=undefined&se=&sme=&tab=0&width=&height=&face=undefined&ist=&jit=&cg=&bdtype=13&oriquery=&objurl=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F6f061d950a7b020855a9e07c68d9f2d3562cc8cf.jpg&fromurl=ippr_z2C%24qAzdH3FAzdH3Fooo_z%26e3Bq7wg3tg2_z%26e3Bv54AzdH3Ft42k7yAzdH3Frim0mm-raaa8m_z%26e3Bip4sa&gsm=0&rpstart=0&rpnum=0&islist=&querylist=", "t.jpg", "E:\\图片");
} catch (Exception e) {
}
}
}
class Down{
public static void down(String urlstring,String filename,String savepath) throws IOException{
//链接
URL url=new URL(urlstring);
URLConnection connection=url.openConnection();
InputStream is=connection.getInputStream();
//字节数组
byte [] buff=new byte[1024];
int len=0;
//文件
File file=new File(savepath);
if(!file.exists()) {
file.mkdirs();
}
//写:下载
OutputStream os=new FileOutputStream(file.getPath()+"/"+filename);
while((len=is.read(buff))!=-1) {
os.write(buff, 0, len);
}
os.close();
is.close();
}
}

评论列表( 0 )

你可以在登录后,发表评论

搜索帮助