从客户端上传到服务器端,其实本质上也就是复制!
package july76net;
//上传文件(文本)
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Demo14 {
public static void main(String[] args) throws Exception {
Socket s = new Socket("localhost",12362);
BufferedReader br = new BufferedReader(new FileReader("E:/你好.txt"));
PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
String line;
while((line = br.readLine()) != null){
pw.println(line);
}
s.shutdownOutput();//阻塞式方法的应对,否则会一直等待!
BufferedReader br2 = new BufferedReader(new InputStreamReader(s.getInputStream()));
String str = br2.readLine();//服务器端反馈的数据
System.out.println(str);
br.close();
s.close();
}
}
服务器端
package july76net;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Demo15 {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(12362);
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+".....connected!");
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter pw = new PrintWriter(new FileWriter("E:/你好2.txt"),true);//建议打印时都用打印流
String line;
while((line = br.readLine()) != null){
pw.println(line);//不可以写成是write();
}
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
out.println("上传成功!");
s.close();
ss.close();
}
}
上传图片(注意图片是2进制文件,必须是字节流,否则读取不出来!);
客户端
package july76net;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class Demo16 {
public static void main(String[] args) throws Exception {
Socket s = new Socket("localhost",10256);
FileInputStream fis = new FileInputStream("E:/DSC_2451.jpg");
OutputStream os = s.getOutputStream();
byte []buf = new byte[1024];
int len;
while((len = fis.read(buf)) != -1){
os.write(buf);
}
s.shutdownOutput();
InputStream in = s.getInputStream();
byte []b = new byte[1024];
int i = in.read(b);
System.out.println(new String(b,0,i));
fis.close();
s.close();
}
}
服务器端
package july76net;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Demo17 {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(10256);
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+".........connected");
InputStream is = s.getInputStream();
FileOutputStream fos = new FileOutputStream("E:/我的照片0.jpg");
byte []b = new byte[1024];
int len;
while((len = is.read(b)) != -1){
fos.write(b);
}
OutputStream os = s.getOutputStream();
os.write("上传成功!".getBytes());
s.close();
ss.close();
}
}
我的总结:
对于网络编程而言,重要的是理解其步骤,按照步骤的需要,一步步搭建根基!
客户端和服务端需要交互,那么就要构建相对应的流,供其输入和输出!
对于阻塞式方法,一定要注意,提供停止标签!
对于PrintWriter ,记得用println而不是write;不要忘了加上true,自动刷新!