Java 網路編程之 (基於 TCP 的遠端檔案傳輸)

來源:互聯網
上載者:User

伺服器端:

 

import java.io.BufferedInputStream;<br />import java.io.BufferedOutputStream;<br />import java.io.FileOutputStream;<br />import java.net.ServerSocket;<br />import java.net.Socket;<br />//使用TCP協議寫一個可以上傳檔案的伺服器和用戶端。<br />public class UpLoad {<br />public static void main(String[] args) throws Exception {<br />ServerSocket ss = new ServerSocket(3000);<br />Socket socket = ss.accept();<br />new Thread(new Receive(socket)) {<br />}.start();<br />}<br />}<br />class Receive implements Runnable {<br />private Socket socket;<br />public Receive(Socket socket) {<br />this.socket = socket;<br />}<br />public void run() {<br />try {<br />BufferedInputStream bis = new BufferedInputStream(socket<br />.getInputStream());<br />byte[] bFileName = new byte[255];<br />int len = bis.read(bFileName);<br />String fileName = new String(bFileName, 0, len).trim();<br />byte[] bytes = new byte[1024];<br />FileOutputStream fos = new FileOutputStream("d://" + fileName);<br />BufferedOutputStream bos = new BufferedOutputStream(fos);<br />len = 0;<br />while ((len = bis.read(bytes)) != -1) {<br />bos.write(bytes, 0, len);<br />}<br />bos.close();<br />fos.close();<br />bis.close();<br />socket.close();<br />} catch (Exception e) {<br />e.printStackTrace();<br />}<br />}<br />}<br />

 

 

用戶端:

 

 

import java.io.BufferedOutputStream;<br />public class UpLoadClient {<br />public static void main(String[] args) throws Exception {<br />BufferedReader br = null;<br />String path = null;<br />String fileName = null;<br />String ip = null;<br />while (true) {<br />System.out.println("請輸入檔案路徑:");<br />br = new BufferedReader(new InputStreamReader(System.in));<br />path = br.readLine();<br />File file = new File(path);<br />fileName = path.substring(path.lastIndexOf("//") + 1);<br />if (file.isDirectory() || !file.exists())<br />System.out.println("路徑不正確!");<br />else<br />break;<br />}<br />System.out.println("請輸入伺服器位址:");<br />br = new BufferedReader(new InputStreamReader(System.in));<br />ip = br.readLine();<br />System.out.println("確認上傳:" + path + "檔案嗎(y/n)?");<br />br = new BufferedReader(new InputStreamReader(System.in));<br />String result = br.readLine();<br />if ("n".equalsIgnoreCase(result)) {<br />br.close();<br />return;<br />}<br />Socket socket = new Socket(ip, 3000);<br />FileInputStream fs = new FileInputStream(path);<br />byte[] bytes = new byte[1024];<br />BufferedOutputStream bos = new BufferedOutputStream(socket<br />.getOutputStream());<br />while (true) {<br />if (fileName.getBytes().length < 255)<br />fileName += "/u0000";<br />else<br />break;<br />}<br />bos.write(fileName.getBytes());<br />bos.flush();<br />int len = 0;<br />while ((len = fs.read(bytes)) != -1) {<br />bos.write(bytes, 0, len);<br />}<br />bos.close();<br />fs.close();<br />br.close();<br />socket.close();<br />System.out.println("檔案上傳完畢!");<br />}<br />}<br />

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.