中文JAVA技術(shù)平等自由協(xié)作創(chuàng)造

          Java專(zhuān)題文章博客和開(kāi)源

          常用鏈接

          統(tǒng)計(jì)

          最新評(píng)論

          Java實(shí)現(xiàn)的網(wǎng)絡(luò)文件傳送

            FileUpload包下放了三個(gè)類(lèi):
            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 {
            // 選擇打開(kāi)文件
            public String getFile() {
            String fileName="";
            FileDialog fd = new FileDialog(this, "請(qǐng)選擇要傳給zpc的文件", FileDialog.LOAD);
            fd.setDirectory("C:\\");
            fd.setVisible(true);
            // 獲取此文件對(duì)話框選定的文件
            if (fd.getFile()!= null) {
            fileName=fd.getDirectory() + fd.getFile();
            System.out.println("已選擇打開(kāi) " +fileName );
            }
            return fileName;
            }
            // 保存文件
            public OutputStream saveFile(String fileName) {
            OutputStream os=null;
            try {
            FileDialog fd = new FileDialog(this, "保存文件", FileDialog.SAVE);
            // 對(duì)話框打開(kāi)時(shí)顯示默認(rèn)的目錄
            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, "文件保存發(fā)生錯(cuò)誤,已停止");
            }
            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.*;
            //每接受一個(gè)客戶(hù)端請(qǐng)求單開(kāi)一個(gè)線程處理
            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());// 和復(fù)制文件不同的是網(wǎng)絡(luò)上傳文件是從Socket中獲取的I/O流對(duì)象
            String fileName = fis.readUTF();
            DataOutputStream fos1= new DataOutputStream(s.getOutputStream());//寫(xiě)回信息;
            if ((JOptionPane.showOptionDialog(null,
            "您想接受文件" + fileName + "嗎?", "消息",
            JOptionPane.YES_NO_OPTION, 0, null, null, null)) == 0) {
            System.out.println("我已選擇接受文件!");
            String message1="服務(wù)器已接收!";
            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="提示:對(duì)方已拒絕接受!";
            fos1.writeUTF(message2);
            }
            } catch (Exception e) {
            System.out.println("接受文件失??!");
            }
            }
            }
            public class TCPServer {
            public static void main(String[] args) throws IOException {
            // 建立服務(wù)器端的socket對(duì)象ServerSocket
            ServerSocket ss = new ServerSocket(10001);
            while (true) {
            Socket s = ss.accept();
            // 每個(gè)客戶(hù)端的請(qǐng)求創(chuàng)建一個(gè)Socket對(duì)象,單開(kāi)一個(gè)線程處理
            new Thread(new UploadProcess(s))。start();
            }
            }
            }
            3、TCPClient .java
            /*
            * Author:zpc
            * Function:上傳文件給用戶(hù)zpc
            * Time:2014-5-1
            */
            package FileUpload;
            import java.io.*;
            import java.net.*;
            public class TCPClient {
            public static void main(String[] args) throws UnknownHostException{
            // 進(jìn)行文件的讀取。數(shù)據(jù)源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("未選中文件!");
            }
            // 數(shù)據(jù)目的地是另一臺(tái)主機(jī),通過(guò)Socket獲取I/O流
            if (in != null) {
            Socket s = null;
            try {
            s = new Socket("127.0.0.1", 10001);//這里寫(xiě)好目標(biāo)機(jī)器IP地址和任意一個(gè)開(kāi)放的未被其它程序占用的端口號(hào)
            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("文件讀取失?。╥n = null)!");
            }
            }

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


          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          PK10開(kāi)獎(jiǎng) PK10開(kāi)獎(jiǎng)
          主站蜘蛛池模板: 江油市| 泗水县| 黄陵县| 库尔勒市| 广河县| 武宁县| 桂林市| 务川| 乌拉特后旗| 烟台市| 泽库县| 西畴县| 石嘴山市| 贺兰县| 左贡县| 平武县| 宁化县| 霍邱县| 苗栗市| 泾阳县| 南漳县| 锦州市| 旬阳县| 彭泽县| 察雅县| 台安县| 阿瓦提县| 德安县| 柞水县| 左权县| 华蓥市| 卢龙县| 平湖市| 上林县| 越西县| 阿勒泰市| 二手房| 贵溪市| 江永县| 威远县| 新津县|