|  | 
				
					
	
		
		
		  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
    { 11
  private JButton openButton,saveButton; 12
  JFileChooser fc; 13
  String fileName; 14
  int result; 15
  Chooser() 16
    { 17
  setLayout(new GridLayout()); 18
  JButton openButton=new JButton("Open"); 19
  openButton.addActionListener(new openFile()); 20
  JButton saveButton=new JButton("Save"); 21
  saveButton.addActionListener(new saveFile()); 22
  add(openButton); 23
  add(saveButton); 24
  } 25
  26
  class openFile implements ActionListener 27
    { 28
  public void actionPerformed(ActionEvent e) 29
    { 30
  fc = new JFileChooser(); 31
  result = fc.showOpenDialog(Chooser.this); 32
  File file = fc.getSelectedFile(); 33
  if(file!=null && result==JFileChooser.APPROVE_OPTION) 34
    { 35
  fileName = file.getAbsolutePath(); 36
  System.out.println("You chose to open this file: " +fileName); 37
  try 38
    { 39
  File file1=new File(fileName); 40
  FileInputStream fos=new FileInputStream(file1); 41
  //创建网络服务器接受客户请求 42
  ServerSocket ss=new ServerSocket(3108); 43
  Socket client=ss.accept(); 44
  //创建网络输出流并提供数据包装器 45
  OutputStream netOut=client.getOutputStream(); 46
  OutputStream doc=new DataOutputStream(new BufferedOutputStream(netOut)); 47
  //创建文件读取缓冲区 48
  byte[] buf=new byte[2048]; 49
  int num=fos.read(buf); 50
  while(num!=(-1)) 51
    { //是否读完文件 52
  doc.write(buf,0,num);//把文件数据写出网络缓冲区 53
  doc.flush();//刷新缓冲区把数据写往客户端 54
  num=fos.read(buf);//继续从文件中读取数据 55
  } 56
  fos.close(); 57
  doc.close(); 58
  } 59
  catch(Exception ex) 60
    { 61
  System.out.println(ex); 62
  } 63
  } 64
  if(result == JFileChooser.CANCEL_OPTION) 65
    { 66
  } 67
  68
  } 69
  } 70
  class saveFile implements ActionListener 71
    { 72
  public void actionPerformed(ActionEvent e) 73
    { 74
  fc = new JFileChooser(); 75
  result = fc.showSaveDialog(Chooser.this); 76
  File file1 = fc.getSelectedFile(); 77
  fileName = file1.getAbsolutePath(); 78
  System.out.println("fileName:"+fileName); 79
  if (result == JFileChooser.APPROVE_OPTION) 80
    { 81
  try 82
    { 83
  File file2=new File(fileName); 84
  file2.createNewFile(); 85
  RandomAccessFile raf=new RandomAccessFile(file2,"rw"); 86
  // 通过Socket连接文件服务器 87
  Socket server=new Socket(InetAddress.getLocalHost(),3108); 88
  //创建网络接受流接受服务器文件数据 89
  InputStream netIn=server.getInputStream(); 90
  InputStream in=new DataInputStream(new BufferedInputStream(netIn)); 91
  //创建缓冲区缓冲网络数据 92
  byte[] buf=new byte[2048]; 93
  int num=in.read(buf); 94
  System.out.println("in.read(buf)´length="+num); 95
  while(num!=(-1)) 96
    {//是否读完所有数据 97
  raf.write(buf,0,num);//将数据写往文件 98
  raf.skipBytes(num);//顺序写文件字节 99
  num=in.read(buf);//继续从网络中读取文件 100
  } 101
  in.close(); 102
  raf.close(); 103
  } 104
  catch(Exception ex) 105
    { 106
  System.out.println(ex); 107
  } 108
  109
  } 110
  if(result == JFileChooser.CANCEL_OPTION) 111
    { 112
  } 113
  } 114
  } 115
  public static void main(String args[]) 116
    { 117
  JFrame f=new JFrame(); 118
  f.getContentPane().add(new Chooser()); 119
  f.setSize(250, 110); 120
  f.setResizable(false); 121
  f.setDefaultCloseOperation(3); 122
  f.setVisible(true); 123
  } 124
  }   |