java实现文件传输

java实现文件传输

  1 import  java.awt. *
  2 import  java.awt.event. *
  3 import  javax.swing. *
  4 import  javax.swing.event. *
  5 import  java.io. *
  6 import  java.net. *
  7 import  javax.swing.filechooser.FileFilter; 
  8
  9 public   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}
 

你可能感兴趣的:(java实现文件传输)