package mynet;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class TcpClient {
/****************************************************
* TCP图片客户端 *
* 1.创建图片客户端Socket,建议指定服务器和服务器端口 *
* 2.连接建立成功,数据通道建立。可以使用Socket流传输数据。 *
* 3.使用输出流把数据传输到服务器端 *
* 4.关闭资源 *
*/
public static void main(String[] args) throws IOException {
// 1.创建一个图片客户端
Socket socket = new Socket("127.0.0.1", 12345);
// 2.获取Socket输出流
OutputStream socketOut = socket.getOutputStream();
// //验证要上传的文件
File file = new File("141.mp3");
if (!file.exists())
throw new RuntimeException("文件不存在");
// 获取文件后缀
String suffix = getFileSuffix(file.getName());
if (suffix.equals("false"))
throw new RuntimeException("只能上传图片文件");
// 将文件后缀发送给服务器
socketOut.write(suffix.getBytes());
socketOut.flush();
// 3.创建一个用于读取本地图片的字节流
FileInputStream fis = new FileInputStream(file);
// 4.自定义缓冲区
byte[] buf = new byte[1024];
int len = 0;
// 循环读取图片,循环输出图片
while ((len = fis.read(buf)) != -1) {
socketOut.write(buf, 0, len);
socketOut.flush();
}
// 告诉服务器输出完毕
socket.shutdownOutput();
// 5.获取Socket读取流,接收从服务器返回的消息。
InputStream socketIn = socket.getInputStream();
len = socketIn.read(buf);
System.out.println(new String(buf, 0, len));
fis.close();
// 关闭客户端
socket.close();
}
/**
* 获取文件后缀,并判断是否满足指定格式。
*
* @param 文件名字符串
* @return 返回文件后缀,如果不匹配指定格式,返回字符串"false"
*/
private static String getFileSuffix(String name) {
String[] suffixs = { ".jpg", ".png", ".gif", ".mp3" };
for (String suffix : suffixs) {
if (name.endsWith(suffix))
return suffix;
}
return "false";
}
}
package mynet;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServer {
/********************************************************************
* TCP图片服务器 *
* 1.创建服务端Socke,使用ServerSocket对象。监听一个端口,否则客户端无法链接。 *
* 2.获取链接到服务器端的客户端对象。 *
* 3.获取到客户端,就可以通过Socket流,读取客户端发来的数据。 *
* 4.关闭资源,关闭客户端,关闭服务端。 *
* 5.服务器开启多线程,线程任务,获取Socket流,接收数据。。。 *
********************************************************************/
static int count = 0;
public static void main(String[] args) throws IOException {
// 1.创建一个服务端
ServerSocket server = new ServerSocket(12345);
while (true) {
// 2.获取客户端
Socket socket = server.accept();// 阻塞式
new Thread(new ServerRun(socket)).start();
}
}
}
package mynet;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class ServerRun implements Runnable {
private Socket socket;
public ServerRun(Socket socket) {
this.socket = socket;
}
public void run() {
FileOutputStream fos = null;
try {
// 3.获取Socket读取流
InputStream serverIn = socket.getInputStream();
byte[] buf = new byte[1024];
int len = 0;
// //获取文件后缀
len = serverIn.read(buf);
String suffix = new String(buf, 0, len);
// 4.创建一个文件夹
File dir = new File("C://jpg");
if (!dir.exists())
dir.mkdir();
String prefix = socket.getInetAddress().getHostAddress();
File file = new File(dir, prefix + suffix);
if(file.exists()){
//................
}
// 5.创建一个输出流,用于保存从客户端接收到的图片数据
fos = new FileOutputStream(file);
while ((len = serverIn.read(buf)) != -1) {
fos.write(buf, 0, len);
fos.flush();
}
// 6.获取Socket输出流,将信息反馈个客户端
OutputStream out = socket.getOutputStream();
out.write("文件上传成功".getBytes());
} catch (Exception e) {
// ...
} finally {
try {
fos.close();
socket.close();
} catch (IOException e) {
// ...
}
}
}
}