<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    愚人碼頭

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

    java實(shí)現(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ù)器接受客戶請(qǐng)求 
     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// 通過(guò)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 船夫 閱讀(3055) 評(píng)論(5)  編輯  收藏 所屬分類: java技術(shù)

    評(píng)論

    # re: java實(shí)現(xiàn)文件傳輸  回復(fù)  更多評(píng)論   

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

    # re: java實(shí)現(xiàn)文件傳輸  回復(fù)  更多評(píng)論   

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

    # re: java實(shí)現(xiàn)文件傳輸  回復(fù)  更多評(píng)論   

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

    # re: java實(shí)現(xiàn)文件傳輸  回復(fù)  更多評(píng)論   

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

    # re: java實(shí)現(xiàn)文件傳輸  回復(fù)  更多評(píng)論   

    是不是要考慮效率問(wèn)題? 如果是校園網(wǎng)和外網(wǎng)傳輸呢????
    是不是要用到線程?
    樓主請(qǐng)指教?
    2009-05-20 21:29 | Blood
    主站蜘蛛池模板: 日韩午夜理论免费TV影院| AAA日本高清在线播放免费观看| 亚洲免费电影网站| 久久久久久久久亚洲| 三级网站在线免费观看| 激情97综合亚洲色婷婷五| 一级中文字幕乱码免费| 国产成人精品日本亚洲专区| 九九免费精品视频在这里| 亚洲一区二区视频在线观看| 两个人www免费高清视频| 亚洲精品乱码久久久久久| 免费91最新地址永久入口| 亚洲AV日韩AV高潮无码专区| 1000部禁片黄的免费看| 亚洲欧洲精品成人久久曰| 一本色道久久综合亚洲精品| caoporn国产精品免费| 国产亚洲精品免费视频播放| a色毛片免费视频| 18亚洲男同志videos网站| 无码高潮少妇毛多水多水免费| 成a人片亚洲日本久久| 国产成人精品久久亚洲高清不卡 | 中国极品美軳免费观看| 亚洲视频国产精品| 女人18毛片免费观看| 成人国产网站v片免费观看| aa级女人大片喷水视频免费| 亚洲AV永久青草无码精品| 69式国产真人免费视频| 美女啪啪网站又黄又免费| 成人一a毛片免费视频| 特级毛片爽www免费版| 亚洲精品一品区二品区三品区| 亚洲成人免费网站| 青青免费在线视频| 亚洲系列中文字幕| 免费人成激情视频| 精品成在人线AV无码免费看| 免费的黄色的网站|