愚人碼頭

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

          java實現(xiàn)文件傳輸

            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//創(chuàng)建網(wǎng)絡(luò)服務(wù)器接受客戶請求 
           42ServerSocket ss=new ServerSocket(3108); 
           43Socket client=ss.accept(); 
           44//創(chuàng)建網(wǎng)絡(luò)輸出流并提供數(shù)據(jù)包裝器 
           45OutputStream netOut=client.getOutputStream(); 
           46OutputStream doc=new DataOutputStream(new BufferedOutputStream(netOut)); 
           47//創(chuàng)建文件讀取緩沖區(qū) 
           48byte[] buf=new byte[2048]; 
           49int num=fos.read(buf); 
           50while(num!=(-1)) 
           51//是否讀完文件 
           52doc.write(buf,0,num);//把文件數(shù)據(jù)寫出網(wǎng)絡(luò)緩沖區(qū) 
           53doc.flush();//刷新緩沖區(qū)把數(shù)據(jù)寫往客戶端 
           54num=fos.read(buf);//繼續(xù)從文件中讀取數(shù)據(jù) 
           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連接文件服務(wù)器 
           87Socket server=new Socket(InetAddress.getLocalHost(),3108); 
           88//創(chuàng)建網(wǎng)絡(luò)接受流接受服務(wù)器文件數(shù)據(jù) 
           89InputStream netIn=server.getInputStream(); 
           90InputStream in=new DataInputStream(new BufferedInputStream(netIn)); 
           91//創(chuàng)建緩沖區(qū)緩沖網(wǎng)絡(luò)數(shù)據(jù) 
           92byte[] buf=new byte[2048]; 
           93int num=in.read(buf); 
           94System.out.println("in.read(buf)′length="+num); 
           95while(num!=(-1)) 
           96{//是否讀完所有數(shù)據(jù) 
           97raf.write(buf,0,num);//將數(shù)據(jù)寫往文件 
           98raf.skipBytes(num);//順序?qū)懳募止?jié) 
           99num=in.read(buf);//繼續(xù)從網(wǎng)絡(luò)中讀取文件 
          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技術(shù)

          評論

          # re: java實現(xiàn)文件傳輸  回復  更多評論   

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

          # re: java實現(xiàn)文件傳輸  回復  更多評論   

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

          # re: java實現(xiàn)文件傳輸  回復  更多評論   

          這個跟你使用什么開發(fā)工具無關(guān)的,都可以實現(xiàn)的
          2007-05-21 11:59 | 船夫

          # re: java實現(xiàn)文件傳輸  回復  更多評論   

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

          # re: java實現(xiàn)文件傳輸  回復  更多評論   

          是不是要考慮效率問題? 如果是校園網(wǎng)和外網(wǎng)傳輸呢????
          是不是要用到線程?
          樓主請指教?
          2009-05-20 21:29 | Blood
          主站蜘蛛池模板: 车致| 封丘县| 保定市| 祁阳县| 大田县| 浏阳市| 鸡西市| 翁牛特旗| 陵水| 拉孜县| 怀集县| 扶绥县| 洪洞县| 湟中县| 丹江口市| 香港| 黔东| 禹城市| 崇礼县| 成安县| 韩城市| 安福县| 兴安盟| 永安市| 冕宁县| 温泉县| 柳林县| 东港市| 巴东县| 麦盖提县| 依安县| 绥中县| 亳州市| 如东县| 岱山县| 杭锦旗| 章丘市| 怀安县| 泸水县| 武鸣县| 肥城市|