⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 receive.java

📁 可以支持很多人在线聊天
💻 JAVA
字号:
package talk3;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2006</p> * <p>Company: </p> * @author not attributable * @version 1.0 */import java.io.*;import java.net.Socket;import java.awt.Event.*;import java.awt.*;import java.awt.event.*;import java.awt.*;import javax.swing.*;import java.util.Date;public class Receive extends JFrame  implements Runnable, ActionListener{  int M=1048576;  Thread runner;  String a="";  long length=0;   boolean  flag=true;  static Socket client ;   PrintStream out = null;   BufferedReader in = null;     JProgressBar bar= new JProgressBar(0,100);     JPanel labelP=new JPanel();     JLabel label1=new JLabel();     JLabel label2=new JLabel();     JPanel buttonP = new JPanel();     JButton accept;     JButton refuse;    String message;public  Receive (String ip,int port) throws IOException{      client = new Socket(ip,port);      out = new PrintStream(client.getOutputStream());      in = new BufferedReader(new InputStreamReader(                                client.getInputStream()));     if((a=in.readLine())!=null)      System.out.println(a);     length=Integer.parseInt(in.readLine());     if(length/M>1)       flag=false;     System.out.println("LENGTH"+length);    createTalkWin();  }     public void createTalkWin()    {       this.setSize(300,400);       Container  pane = getContentPane();       pane.setLayout(new BorderLayout());       bar.setStringPainted(true);       pane.add(bar,BorderLayout.NORTH);       pane.add(labelP,BorderLayout.CENTER);       labelP.setLayout(new GridLayout(2,2));       labelP.add(label1);       labelP.add(label2);       pane.add(buttonP,BorderLayout.SOUTH);       buttonP.setLayout(new FlowLayout());       accept=new JButton("另存为");       accept.addActionListener(this);       buttonP.add(accept);       refuse=new JButton("拒绝");       refuse.addActionListener(this);       buttonP.add(refuse);    addWindowListener(new WindowDestroyer());    try {UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");}  catch (UnsupportedLookAndFeelException ex) {}    catch (IllegalAccessException ex) {}     catch (InstantiationException ex) {} catch (ClassNotFoundException ex) {}  }  public void actionPerformed(ActionEvent e)  {    String actionCommand=e.getActionCommand();    if(actionCommand.equals("另存为"))    {      out.println("yes");      runner = new Thread(this);      try      {        runner.start();        runner = null;      }      catch (Exception ex1) {}    }    if(actionCommand.equals("拒绝"))    {      out.println("no");      System.exit(0);    }}  public void run()  {    try         {         InputStream stream = client.getInputStream();         JFileChooser  file = new JFileChooser();         file.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);         file.showOpenDialog(this);         String path= file.getSelectedFile().getPath()+"\\"+getFileName(a);         System.out.println("运行到复制成旭"+path+"     ");         File f = new File(path);         f.createNewFile();         accept.setEnabled(false);         refuse.setEnabled(false);         int BUFFER = 1024;         int count;          System.out.println("jfkajflkajlkf==-+_++_+_+_+++++++++++_+_++_+_"+length);         byte data[] = new byte[BUFFER];         FileOutputStream fos = new FileOutputStream(f);         BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);         int currentSize=0;         Date time=new Date();         System.out.println() ;         int number=0;         long t1 = time.getSeconds();         while ( (count = stream.read(data, 0, BUFFER)) != -1)           {             dest.write(data, 0, count);             currentSize+=count;             int baiFen=(int)((currentSize)/(length/100.0));             bar.setValue(baiFen);             if(flag)             {               label1.setText("已传: "+length/1024*baiFen/100+"/"+length/1024);               System.out.println(length/1024*baiFen/100);             }             else               label1.setText("已传: "+length/M*baiFen/100+"M/"+length/M+"M");             Date time2=new Date();             long t2=time2.getSeconds();             if(t2-t1>=1)             {               long v = (t2 - t1);             //  System.out.println(t1 + "在这里谈话是干什么风口假大空风景点 " + t2);               label2.setText( "传送速度: " +(currentSize-number)/v/1024 + " K//S" );               t1 = t2;               number=currentSize;             }           //  System.out.println("jfkajflkajlkf==         "+currentSize);             //System.out.println("百分比:::: "+(int)(currentSize*100));             try             {         runner.sleep( (long) 1);         }         catch (InterruptedException ex2)           {            }           }            bar.setValue(100);            label1.setText("");            label2.setText("");           dest.close();           stream.close();       }         catch(Exception ex)         {                 System.out.println(ex);         }  }private String getFileName(String path) { int length=path.length(); System.out.println("length "+length); String fileName=""; boolean file=true; int fileBorder=0; for(int i=length-1;i>=0&&file;i--) { if(path.charAt(i)=='\\') { file = false; fileBorder=i+1; } } for(int i=fileBorder;i<length;i++) { fileName+=path.charAt(i); } System.out.println("正文风纪扣分解阿 "+fileName+fileBorder); return fileName; }  /*public static void main(String[] args)  {    try    {     // client = new Socket("211.87.234.144", 4700);      Riceive c = new Riceive("211.87.234.144", 4700);      c.show();    }    catch(Exception e)    {}  }*/  private class WindowDestroyer extends WindowAdapter  {  public void windowClosing(WindowEvent e)  {    out.close();  System.exit(0) ;  }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -