Socket套接字发送图片

package net.paoyun.day21.url;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;


import org.junit.Test;


/**
 * 向服务器上传图片
 * 
 * @author 11297
 *
 */
public class SocketTest3 {


@Test
public void serverTest() {
ServerSocket serverSocket = null;
Socket socket = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
InputStream is = null;
byte[] buff = new byte[512];
int len;
try {
serverSocket = new ServerSocket(3839);
socket = serverSocket.accept();
String line;
is = socket.getInputStream();
bis = new BufferedInputStream(is);
String ip=socket.getLocalAddress().getHostAddress();
System.out.println(ip);
bos=new BufferedOutputStream(new FileOutputStream(ip+".jpg"));


while ((len = bis.read(buff)) != -1) {


bos.write(buff, 0, len);
}
bos.flush();


} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}


@Test
public void clientTest() {


// 向服务器端发送一个文本文件(套接字的输出流)
BufferedInputStream bis = null;// 读文件
BufferedOutputStream socketBos = null;// 绑定并包裹套接字输出流
BufferedInputStream socketBis = null;
Socket socket = null;
OutputStream socketOs = null;
byte[] buf = new byte[512];
int len;
try {
// 1.客户端的磁盘上读入一个文本文件
bis = new BufferedInputStream(new FileInputStream("C:\\Users\\11297\\Desktop\\wallpaper2.jpg"));
// 2.把读入文件发送到服务器端
// 2.1定义套接字
socket = new Socket("192.168.0.141", 3839);
// 2.2 通过套接字获取输出流
socketOs = socket.getOutputStream();
socketBos = new BufferedOutputStream(socketOs);// 字节流——>字符流
// 2.3 发送
while ((len = bis.read(buf)) != -1) {
socketBos.write(buf, 0, len);


}
socketBos.flush();// 带缓冲流,需要刷新
socket.shutdownOutput();// 告诉服务器输出完毕。


socketBis = new BufferedInputStream(socket.getInputStream());


while ((len = socketBis.read(buf)) != -1) {
System.out.println(new String(buf, 0, len));
}


} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 3.释放资源
if (socketBos != null) {
/*
* try { // socketBw.close(); } catch (IOException e) { // TODO
* Auto-generated catch block e.printStackTrace(); }
*/
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值