This example shares with you the method of sending files to the server for your reference. The specific content is as follows
/* *Send files to the server, mainly IO streams. */ import java.io.*; import java.net.*; class send2 { public static void main(String[] args) throws Exception { Socket s = new Socket("192.168.33.1",10005);//Create a service BufferedReader bufr = new BufferedReader(new FileReader("io.java"));//Read the IO.JAVA file PrintWriter pw = new PrintWriter(s.getOutputStream(),true);//Write the read to the server String line = null; while((line = bufr.readLine())!=null) { pw.println(line); } // pw.println("over");// Mark the end position s.shutdownOutput(); BufferedReader bufin = new BufferedReader(new InputStreamReader(s.getInputStream()));// Read the data returned by the server String str = bufin.readLine(); System.out.println(str); bufr.close(); s.close(); } } class receive2 { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(10005);//Create service Socket s = ss.accept();//Receive data BufferedReader bufin = new BufferedReader(new InputStreamReader(s.getInputStream()));//Read received data PrintWriter out = new PrintWriter(new FileWriter("io2.txt"),true);//Write to IO.TXT text String line = null; while((line = bufin.readLine())!= null)//Read a line and write a line { // if("over".equals(line)) // break; out.println(line); } PrintWriter pw = new PrintWriter(s.getOutputStream(),true); pw.println("Uploaded successfully! "); out.close(); s.close(); s.close(); } }result:
The above is all the content. I hope you can give you a reference and I hope you can support Wulin.com more.