- 实现步骤:
- 1.创建一个本地字节收入流 FileInputStream 对象,构造方法中绑定读取的数据源
- 2.创建一个socket对象,构造方法中绑定 服务器的IP地址和端口号
- 3.使用socket 中的方法 getoutputStream 获取网络字节输出流
- 4.使用本地字节输入流 中的read方法,读取本地文件
- 5.使用网络输入流中write方法,把读取得到的文件上传到服务器
- 6.使用socket中的输入流,获取输入流对象
- 7.通过网络输入流对象中的read方法,读取回写数据
- 8.释放资源
package Demo01FileUpload;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class TcpClient {
public static void main(String []args) throws IOException {
FileInputStream fis=new FileInputStream("C:\\Users\\LEGION\\Pictures\\Saved Pictures\\9999.jpg");
Socket socket =new Socket("127.0.0.1",8888);
OutputStream os=socket.getOutputStream();
int len=0;
byte[] bytes =new byte[1024];
while((len=fis.read(bytes))!=-1) {
os.write(bytes,0,len);
}
socket.shutdownInput();
InputStream is=socket.getInputStream();
while((len=is.read(bytes))!=-1) {
System.out.println(new String(bytes,0,len));
}
fis.close();
socket.close();
}
}
package Demo01FileUpload;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServer {
public static void main(String []args) throws IOException {
ServerSocket server=new ServerSocket(8888);
Socket socket=server.accept();
InputStream is=socket.getInputStream();
File file=new File("D:\\daring");
if(!file.exists()) {
file.mkdirs();
}
FileOutputStream fos=new FileOutputStream(file+"\\1.jpg");
int len=0;
byte[] bytes=new byte[1024];
while((len=is.read(bytes))!=-1) {
fos.write(bytes,0,len);
}
socket.getOutputStream().write("图片上传成功".getBytes());
fos.close();
socket.close();
server.close();
}
}
package Demo03BS;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServer {
public static void main(String[] args)throws IOException {
ServerSocket server=new ServerSocket(9999);
Socket socket=server.accept();
InputStream is=socket.getInputStream();
byte[]bytes=new byte[1024];
int len=0;
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String line=br.readLine();
System.out.println(line);
String [] arr=line.split("");
String htmlpath=arr[1];
FileInputStream fis=new FileInputStream(System.getProperty("user.dir")+htmlpath);
OutputStream os=socket.getOutputStream();
os.write("HTTP/1.1 200 OK\r\n".getBytes());
os.write("Content-Type:text/html\r\n".getBytes());
os.write("\r\n".getBytes());
while((len=fis.read(bytes))!=-1){
os.write(bytes, 0, len);
}
fis.close();
socket.close();
server.close();
}
}
package Demo03BS;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServerThread {
public static void main(String[] args)throws IOException {
ServerSocket server=new ServerSocket(9999);
while(true) {
Socket socket=server.accept();
new Thread(new Runnable() {
public void run() {
try {
InputStream is=socket.getInputStream();
byte[]bytes=new byte[1024];
int len=0;
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String line=br.readLine();
System.out.println(line);
String [] arr=line.split("");
String htmlpath=arr[1];
FileInputStream fis=new FileInputStream(System.getProperty("user.dir")+htmlpath);
OutputStream os=socket.getOutputStream();
os.write("HTTP/1.1 200 OK\r\n".getBytes());
os.write("Content-Type:text/html\r\n".getBytes());
os.write("\r\n".getBytes());
while((len=fis.read(bytes))!=-1){
os.write(bytes, 0, len);
}
fis.close();
socket.close();
}catch(IOException e) {
System.out.println(e);
}
}
}).start();
}
}
}