愚人碼頭

          知恥而后勇,知不足而進
          隨筆 - 33, 文章 - 1, 評論 - 26, 引用 - 0
          數據加載中……

          java實現文件傳輸

            1import java.awt.*
            2import java.awt.event.*
            3import javax.swing.*
            4import javax.swing.event.*
            5import java.io.*
            6import java.net.*
            7import javax.swing.filechooser.FileFilter; 
            8
            9public class Chooser extends JLabel 
           10
           11private JButton openButton,saveButton; 
           12JFileChooser fc; 
           13String fileName; 
           14int result; 
           15Chooser() 
           16
           17setLayout(new GridLayout()); 
           18JButton openButton=new JButton("Open"); 
           19openButton.addActionListener(new openFile()); 
           20JButton saveButton=new JButton("Save"); 
           21saveButton.addActionListener(new saveFile()); 
           22add(openButton); 
           23add(saveButton); 
           24}
           
           25
           26class openFile implements ActionListener 
           27
           28public void actionPerformed(ActionEvent e) 
           29
           30fc = new JFileChooser(); 
           31result = fc.showOpenDialog(Chooser.this); 
           32File file = fc.getSelectedFile(); 
           33if(file!=null && result==JFileChooser.APPROVE_OPTION) 
           34
           35fileName = file.getAbsolutePath(); 
           36System.out.println("You chose to open this file: " +fileName); 
           37try 
           38
           39File file1=new File(fileName); 
           40FileInputStream fos=new FileInputStream(file1); 
           41//創建網絡服務器接受客戶請求 
           42ServerSocket ss=new ServerSocket(3108); 
           43Socket client=ss.accept(); 
           44//創建網絡輸出流并提供數據包裝器 
           45OutputStream netOut=client.getOutputStream(); 
           46OutputStream doc=new DataOutputStream(new BufferedOutputStream(netOut)); 
           47//創建文件讀取緩沖區 
           48byte[] buf=new byte[2048]; 
           49int num=fos.read(buf); 
           50while(num!=(-1)) 
           51//是否讀完文件 
           52doc.write(buf,0,num);//把文件數據寫出網絡緩沖區 
           53doc.flush();//刷新緩沖區把數據寫往客戶端 
           54num=fos.read(buf);//繼續從文件中讀取數據 
           55}
           
           56fos.close(); 
           57doc.close(); 
           58}
           
           59catch(Exception ex) 
           60
           61System.out.println(ex); 
           62}
           
           63}
           
           64if(result == JFileChooser.CANCEL_OPTION) 
           65
           66}
           
           67
           68}
           
           69}
           
           70class saveFile implements ActionListener 
           71
           72public void actionPerformed(ActionEvent e) 
           73
           74fc = new JFileChooser(); 
           75result = fc.showSaveDialog(Chooser.this); 
           76File file1 = fc.getSelectedFile(); 
           77fileName = file1.getAbsolutePath(); 
           78System.out.println("fileName:"+fileName); 
           79if (result == JFileChooser.APPROVE_OPTION) 
           80
           81try 
           82
           83File file2=new File(fileName); 
           84file2.createNewFile(); 
           85RandomAccessFile raf=new RandomAccessFile(file2,"rw"); 
           86// 通過Socket連接文件服務器 
           87Socket server=new Socket(InetAddress.getLocalHost(),3108); 
           88//創建網絡接受流接受服務器文件數據 
           89InputStream netIn=server.getInputStream(); 
           90InputStream in=new DataInputStream(new BufferedInputStream(netIn)); 
           91//創建緩沖區緩沖網絡數據 
           92byte[] buf=new byte[2048]; 
           93int num=in.read(buf); 
           94System.out.println("in.read(buf)′length="+num); 
           95while(num!=(-1)) 
           96{//是否讀完所有數據 
           97raf.write(buf,0,num);//將數據寫往文件 
           98raf.skipBytes(num);//順序寫文件字節 
           99num=in.read(buf);//繼續從網絡中讀取文件 
          100}
           
          101in.close(); 
          102raf.close(); 
          103}
           
          104catch(Exception ex) 
          105
          106System.out.println(ex); 
          107}
           
          108
          109}
           
          110if(result == JFileChooser.CANCEL_OPTION) 
          111
          112}
           
          113}
           
          114}
           
          115public static void main(String args[]) 
          116
          117JFrame f=new JFrame(); 
          118f.getContentPane().add(new Chooser()); 
          119f.setSize(250110); 
          120f.setResizable(false); 
          121f.setDefaultCloseOperation(3); 
          122f.setVisible(true); 
          123}
           
          124}
           

          posted on 2005-12-20 11:22 船夫 閱讀(3058) 評論(5)  編輯  收藏 所屬分類: java技術

          評論

          # re: java實現文件傳輸  回復  更多評論   

          謝謝!
          2006-10-28 19:46 | hello

          # re: java實現文件傳輸  回復  更多評論   

          這個功能不用Jbuider能實現嗎 ?
          能實現的話具體怎么實現 ?
          2007-05-21 10:47 |

          # re: java實現文件傳輸  回復  更多評論   

          這個跟你使用什么開發工具無關的,都可以實現的
          2007-05-21 11:59 | 船夫

          # re: java實現文件傳輸  回復  更多評論   

          很高興見到你回話,
          我是個初學者,現在在編一個基于SOCKET的聊天程序,主要的功能有3:一是基本的短信息收發,2是文件傳輸功能,3是語音視頻聊天功能,現在我想把傳輸功能部分做成象QQ那樣的 傳文件的時候彈出一個瀏覽對話框選擇文件進行傳輸,要Server與Client能進行互傳 想請你幫忙下,
          2007-05-21 18:06 |

          # re: java實現文件傳輸  回復  更多評論   

          是不是要考慮效率問題? 如果是校園網和外網傳輸呢????
          是不是要用到線程?
          樓主請指教?
          2009-05-20 21:29 | Blood
          主站蜘蛛池模板: 繁昌县| 定南县| 甘谷县| 通许县| 东乡族自治县| 甘德县| 藁城市| 阿拉善左旗| 会泽县| 弥渡县| 奉新县| 枣庄市| 赞皇县| 黔西县| 延吉市| 河西区| 沅江市| 依兰县| 嘉禾县| 巴马| 小金县| 阿克| 宁武县| 枝江市| 湖南省| 蒲城县| 库尔勒市| 新河县| 宝应县| 宜黄县| 万宁市| 德昌县| 贵州省| 泸溪县| 南岸区| 永嘉县| 互助| 江安县| 赣榆县| 图片| 东乡族自治县|