TCP
package Sever;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class PictureSever {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket=new ServerSocket(10086);
Socket socket=serverSocket.accept();
InputStream inputStream=socket.getInputStream();
FileOutputStream fileOutputStream=new FileOutputStream("fj.jpg");
int temp=0;
while ((temp=inputStream.read())!=-1){
fileOutputStream.write(temp);
}
socket.shutdownInput();
OutputStream outputStream=socket.getOutputStream();
outputStream.write("上传成功".getBytes());
socket.shutdownOutput();
outputStream.close();
fileOutputStream.close();
inputStream.close();
socket.close();
serverSocket.close();
}
}
package Cilent;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class PictureCilent {
public static void main(String[] args) throws Exception {
FileInputStream fileInputStream=new FileInputStream("1.jpg");
Socket socket=new Socket("127.0.0.1",10086);
OutputStream outputStream=socket.getOutputStream();
int temp=0;
while((temp=fileInputStream.read())!=-1){
outputStream.write(temp);
}
socket.shutdownOutput();