輸入輸出流核心代碼
所有的文件傳輸都是靠流,其中文件復制最具代表性.輸入流和輸出流,從輸入流中讀取數據寫入到輸出流中。
InputStream in = 輸入源; OutputStream os = 輸出目的;
byte[] bytes = new byte[1024]; int len = 0; while((len = in.read(bytes))!=-1){ //讀取到文件末尾的話可以讀到-1 os.write(bytes,0,len); }
os.close(); in.close();
TCP圖片上傳客戶端
1.Socket套接字連接服務器,指定ip和端口
Socket socket = Socket(String host, int port);
2.通過Socket獲取字節輸出流,向服務器寫圖片
OutputStream os = socket.getOutputStream();
3.創建輸入流對象,讀取圖片數據源
FileInputStream fis = new FileInputStream(filePath);
4.讀取圖片,使用字節輸出流,將圖片寫到服務器,采用字節數組進行緩沖
int len = 0;
byte[] bytes = new byte[1024];
while ((len = fis.read(bytes)) != -1){
os.write(bytes,0,len);
}
socket.shutdownOutput();//文件讀完時給服務器寫終止序列
5.通過Socket套接字獲取字節輸入流,讀取服務器發回來的上傳成功
InputStream inputStream = socket.getInputStream();
len = inputStream.read(bytes);
System.out.println("服務器返回:"+new String(bytes,0,len));
6.關閉資源
socket.close();
/** * 實現TCP圖片上傳客戶端 * 實現步驟: * 1.Socket套接字連接服務器,指定ip和端口 * Socket socket = Socket(String host, int port); * 2.通過Socket獲取字節輸出流,寫圖片 * OutputStream os = socket.getOutputStream(); * 3.使用自己的流對象,讀取圖片數據源 * FileInputStream fis = new FileInputStream(filePath); * 4.讀取圖片,使用字節輸出流,將圖片寫到服務器,采用字節數組進行緩沖 * int len = 0; * byte[] bytes = new byte[1024]; * while ((len = fis.read(bytes)) != -1){ * os.write(bytes,0,len); * } * 5.通過Socket套接字獲取字節輸入流,讀取服務器發回來的上傳成功 * InputStream inputStream = socket.getInputStream(); * len = inputStream.read(bytes); * System.out.println("服務器返回:"+new String(bytes,0,len)); * 6.關閉資源 * socket.close(); * * Created by Autumn on 2018/2/5. */ public class TCPClient { public static void main(String[] args) throws IOException { String ip = "127.0.0.1"; int port = 8000; File fileFolder = new File("D:\\Users\\Autumn\\Pictures"); uploadPic(ip,port,fileFolder+File.separator+"tiger.jpg"); uploadPic(ip,port,fileFolder+File.separator+"water.jpg"); uploadPic(ip,port,fileFolder+File.separator+"sunset.jpg"); } /** * 上傳圖片 * @param ip 服務器ip地址 * @param port 服務器端口號 * @param filePath 文件路徑 * @throws IOException */ public static void uploadPic(String ip,int port,String filePath) throws IOException{ //創建客戶端Socket Socket socket = new Socket(ip,port); //根據Socket獲取字節輸出流,用此流將圖片寫到服務器 OutputStream os = socket.getOutputStream(); //創建字節輸入流,讀取本機上的數據源圖片 FileInputStream fis = new FileInputStream(filePath); //開始讀寫字節數組,從輸入流中讀取到輸出流 int len = 0; byte[] bytes = new byte[1024]; while ((len = fis.read(bytes)) != -1){ os.write(bytes,0,len); } //文件讀完時給服務器寫終止序列 socket.shutdownOutput(); //獲取字節輸入流,讀取服務器的上傳成功 InputStream inputStream = socket.getInputStream(); len = inputStream.read(bytes); System.out.println("服務器返回:"+new String(bytes,0,len)); socket.close(); } }
TCP圖片上傳服務器端
1.ServerSocket套接字對象,監聽8000
ServerSocket serverSocket = ServerSocket(int port);
2.方法accept()獲取客戶端的連接對象
Socket socket = serverSocket.accept();
3.客戶端連接對象獲取字節輸入流,讀取客戶端發送圖片
InputStream in = socket.getInputStream();
4.創建File對象,綁定上傳文件夾。判斷文件夾存在,不存在則創建文件夾
File upload = new File("d:\\upload");
if (!upload.exists()){
upload.mkdirs();
}
5.創建字節輸出流,數據目的File對象所在文件夾
String fileName = "pic"+System.currentTimeMillis()+ new Random().nextInt(9999)+".jpg";
FileOutputStream fos = new FileOutputStream(upload+File.separator+fileName);
6.字節流讀取圖片,字節流將圖片寫入到目的文件中
byte[] bytes = new byte[1024];
int len = 0;
while((len = in.read(bytes))!=-1){ //讀的是客戶端發過來圖片的字節數組,只有讀文件能讀到-1,所以這里永遠讀不到-1,read()會一直阻塞。(需要客戶端發送結束標志)
fos.write(bytes,0,len);
}
7.將上傳成功回寫給客戶端
socket.getOutputStream().write(("上傳到服務器"+socket.getLocalAddress().toString()+"成功").getBytes());
8.關閉資源
fos.close();
socket.close();
/** * 多線程接收圖片 * Created by Autumn on 2018/2/5. */ public class TCPThreadServer { public static void main(String[] args) throws IOException { //服務端不用指定ip地址,只需指定端口號 ServerSocket serverSocket = new ServerSocket(8000); while(true){ //獲得一個客戶端Socket對象,開啟一個線程 Socket socket = serverSocket.accept(); //線程阻塞 new Thread(new Upload(socket)).start(); } } } /** * TCP圖片上傳服務器 * 1.ServerSocket套接字對象,監聽8000 * ServerSocket serverSocket = ServerSocket(int port); * 2.方法accept()獲取客戶端的連接對象 * Socket socket = serverSocket.accept(); * 3.客戶端連接對象獲取字節輸入流,讀取客戶端發送圖片 * InputStream in = socket.getInputStream(); * 4.創建File對象,綁定上傳文件夾。判斷文件夾存在,不存在則創建文件夾 * File upload = new File("d:\\upload"); * if (!upload.exists()){ * upload.mkdirs(); * } * 5.創建字節輸出流,數據目的File對象所在文件夾 * String fileName = "pic"+System.currentTimeMillis()+ new Random().nextInt(9999)+".jpg"; * FileOutputStream fos = new FileOutputStream(upload+File.separator+fileName); * 6.字節流讀取圖片,字節流將圖片寫入到目的文件中 * byte[] bytes = new byte[1024]; * int len = 0; * while((len = in.read(bytes))!=-1){ //讀的是客戶端發過來圖片的字節數組,只有讀文件能讀到1,所以永遠讀不到-1 * fos.write(bytes,0,len); * } * 7.將上傳成功回寫給客戶端 * socket.getOutputStream().write(("上傳到服務器"+socket.getLocalAddress().toString()+"成功").getBytes()); * 8.關閉資源 * fos.close(); * socket.close(); * Created by Autumn on 2018/2/5. */ public class Upload implements Runnable { private Socket socket; public Upload(Socket socket){ this.socket = socket; } @Override public void run() { try { //獲取輸入流 InputStream in = socket.getInputStream(); //創建文件夾 File upload = new File("d:\\upload"); if (!upload.exists()){ upload.mkdirs(); } String fileName = "pic"+System.currentTimeMillis()+ new Random().nextInt(9999)+".jpg"; //創建字節輸出流,將圖片寫入到目的文件夾 upload:d://upload FileOutputStream fos = new FileOutputStream(upload+File.separator+fileName); //讀寫字節數組 byte[] bytes = new byte[1024]; int len = 0; while((len = in.read(bytes))!=-1){ //讀的是客戶端發過來圖片的字節數組,只有讀文件能讀到1,所以永遠讀不到-1 fos.write(bytes,0,len); } System.out.println("成功接收來自"+socket.getInetAddress()+socket.getPort()+"的圖片!"); //通過客戶端連接對象獲取字節輸出流 //上傳成功寫會客戶端 socket.getOutputStream().write(("上傳到服務器"+socket.getLocalAddress().toString()+"成功").getBytes()); fos.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } }