public class Dati4Server {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(8888);
Socket s = ss.accept();
InputStream is = s.getInputStream();
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("F:\\IdeaProjects\\DayDayUP\\copy.jpg"));
byte[] bys = new byte[1024];
int len;
while ((len = is.read(bys)) != -1) {
bos.write(bys, 0, len);
bos.flush();
}
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bw.write("上传成功!");
bw.newLine();
bw.flush();
bw.close();
is.close();
ss.close();
}
}public class Dati4Client {
public static void main(String[] args) throws IOException {
Socket s = new Socket("127.0.0.1",8888);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\tupian.jpg"));
OutputStream os = s.getOutputStream();
byte[] bys = new byte[1024];
int len;
while ((len=bis.read(bys))!=-1){
os.write(bys,0,len);
}
s.shutdownOutput();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String s1 = br.readLine();
System.out.println("服务器反馈"+s1);
bis.close();
br.close();
s.close();
}
}
|
|