中文JAVA技術平等自由協作創造

          Java專題文章博客和開源

          常用鏈接

          統計

          最新評論

          Java實現的網絡文件傳送

            FileUpload包下放了三個類:
            OpenAndSave.java
            TCPClient .java
            TCPServer.java
            1、OpenAndSave.java
            package FileUpload;
            import java.awt.*;
            import javax.swing.*;
            import java.util.*;
            import java.io.*;
            public class OpenAndSave extends JFrame {
            // 選擇打開文件
            public String getFile() {
            String fileName="";
            FileDialog fd = new FileDialog(this, "請選擇要傳給zpc的文件", FileDialog.LOAD);
            fd.setDirectory("C:\\");
            fd.setVisible(true);
            // 獲取此文件對話框選定的文件
            if (fd.getFile()!= null) {
            fileName=fd.getDirectory() + fd.getFile();
            System.out.println("已選擇打開 " +fileName );
            }
            return fileName;
            }
            // 保存文件
            public OutputStream saveFile(String fileName) {
            OutputStream os=null;
            try {
            FileDialog fd = new FileDialog(this, "保存文件", FileDialog.SAVE);
            // 對話框打開時顯示默認的目錄
            fd.setDirectory("C:\\");
            fd.setFile(fileName);
            fd.setVisible(true);
            if (fd.getFile() != null) {
            File myFile = new File(fd.getDirectory(), fd.getFile());
            os = new FileOutputStream(myFile);
            }
            } catch (IOException ioe) {
            JOptionPane.showMessageDialog(this, "文件保存發生錯誤,已停止");
            }
            return os;
            }
            }
            2、TCPServer.java
            /*
            * Author:zpc
            * Time:2014-5-1
            */
            package FileUpload;
            import java.io.*;
            import java.net.*;
            import java.util.Random;
            import javax.swing.*;
            //每接受一個客戶端請求單開一個線程處理
            class UploadProcess implements Runnable {
            private Socket s;
            public UploadProcess(Socket s) {
            this.s = s;
            }
            public void run() {
            OutputStream fos = null;
            try {
            DataInputStream fis = new DataInputStream(s.getInputStream());// 和復制文件不同的是網絡上傳文件是從Socket中獲取的I/O流對象
            String fileName = fis.readUTF();
            DataOutputStream fos1= new DataOutputStream(s.getOutputStream());//寫回信息;
            if ((JOptionPane.showOptionDialog(null,
            "您想接受文件" + fileName + "嗎?", "消息",
            JOptionPane.YES_NO_OPTION, 0, null, null, null)) == 0) {
            System.out.println("我已選擇接受文件!");
            String message1="服務器已接收!";
            fos1.writeUTF(message1);
            fos = new OpenAndSave()。saveFile(fileName);
            int len = 0;
            byte[] bytes = new byte[1024];
            if (fis != null) {
            while ((len = fis.read(bytes)) != -1) {
            fos.write(bytes, 0, len);
            }
            }
            JOptionPane.showMessageDialog(null, "文件保存成功!");
            if (fis != null)
            fis.close();
            if (fos != null)
            fos.close();
            }
            else {
            System.out.println("我選擇了拒絕接受!");
            String message2="提示:對方已拒絕接受!";
            fos1.writeUTF(message2);
            }
            } catch (Exception e) {
            System.out.println("接受文件失敗!");
            }
            }
            }
            public class TCPServer {
            public static void main(String[] args) throws IOException {
            // 建立服務器端的socket對象ServerSocket
            ServerSocket ss = new ServerSocket(10001);
            while (true) {
            Socket s = ss.accept();
            // 每個客戶端的請求創建一個Socket對象,單開一個線程處理
            new Thread(new UploadProcess(s))。start();
            }
            }
            }
            3、TCPClient .java
            /*
            * Author:zpc
            * Function:上傳文件給用戶zpc
            * Time:2014-5-1
            */
            package FileUpload;
            import java.io.*;
            import java.net.*;
            public class TCPClient {
            public static void main(String[] args) throws UnknownHostException{
            // 進行文件的讀取。數據源SAT答案
            OpenAndSave open = new OpenAndSave();
            String fileName = open.getFile();
            File myFile = new File(fileName);
            InputStream in = null;
            try {
            in = new FileInputStream(myFile);
            } catch (FileNotFoundException e1) {
            System.out.println("未選中文件!");
            }
            // 數據目的地是另一臺主機,通過Socket獲取I/O流
            if (in != null) {
            Socket s = null;
            try {
            s = new Socket("127.0.0.1", 10001);//這里寫好目標機器IP地址和任意一個開放的未被其它程序占用的端口號
            DataOutputStream os = new DataOutputStream(s.getOutputStream());托福答案
            os.writeUTF(fileName);
            byte[] b = new byte[1024];
            int len = 0;
            while ((len = in.read(b)) != -1) {
            os.write(b, 0, len);
            }
            //System.out.println("message");
            DataInputStream fis = new DataInputStream(s.getInputStream());
            String message = fis.readUTF();
            System.out.println(message);
            if (os != null)
            os.close();
            if (in != null)
            in.close();
            } catch (Exception e) {
            System.out.println("貌似zpc不在線,稍后再傳!!");
            }
            } else {
            System.out.println("文件讀取失敗(in = null)!");
            }
            }

          posted on 2014-09-20 15:01 好不容易 閱讀(255) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          PK10開獎 PK10開獎
          主站蜘蛛池模板: 荔浦县| 马尔康县| 沙田区| 石城县| 无锡市| 墨脱县| 荣昌县| 来安县| 元谋县| 桃江县| 房山区| 于田县| 池州市| 黄陵县| 叙永县| 米泉市| 元氏县| 甘洛县| 巴马| 卓资县| 锡林郭勒盟| 阜平县| 沛县| 丽水市| 莱阳市| 和顺县| 伊通| 铜梁县| 怀化市| 冕宁县| 泰宁县| 汽车| 盐山县| 垦利县| 集安市| 宜州市| 连城县| 龙门县| 大英县| 文化| 新干县|