Java online chat and file transfer system 7

From , 5 Years ago, written in Java, viewed 224 times.
URL https://pastebin.vip/view/fe709c65
  1. package FileTran;
  2.  
  3. import java.io.File;
  4. import java.io.FileFilter;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.ObjectOutputStream;
  10. import java.net.Socket;
  11. import java.net.UnknownHostException;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14.  
  15. import javax.swing.JFileChooser;
  16. import javax.swing.JOptionPane;
  17.  
  18. import MyFrame.Myframe;
  19.  
  20. public class FileClient extends Thread {
  21.  
  22.         private Socket client = null;
  23.         private ObjectOutputStream out = null;
  24.         private InputStream in = null;
  25.         private Myframe mf;
  26.         private String msg;
  27.         private String ip;
  28.         private String fileload ;
  29.         private JFileChooser jfc;
  30.         private File fi;
  31.         public FileClient(Myframe mf) {
  32.                 this.mf = mf;
  33.                 this.msg = mf.getPane().getContendjt().getText();
  34.                 this.ip = (String) mf.getPane().getIpjt().getSelectedItem();
  35.         }
  36.  
  37.         public void run() {
  38.                 this.jfc=new JFileChooser();
  39.                 this.jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
  40.                 int reval=this.jfc.showOpenDialog(mf);
  41.                 if(reval==JFileChooser.APPROVE_OPTION){
  42.                         fi=this.jfc.getSelectedFile();
  43.                 }else if(reval==1){
  44.                         return;
  45.                        
  46.                 }else{
  47.                         JOptionPane.showMessageDialog(mf, "操作有误请稍后再试!!");
  48.                         return;
  49.                 }
  50.                 this.mf.getPane().getContendjt().setText(msg + "发送中.......\n");
  51.                 String[] file = fi.toString().split("\\\\", -1);
  52.                 byte[] b = new byte[10240];
  53.                 try {
  54.                         List li = new ArrayList();
  55.                         client = new Socket(this.ip, 9500);
  56.                         in = new FileInputStream(this.fi);
  57.                         out = new ObjectOutputStream(client.getOutputStream());
  58.                         int l = 0;
  59.                         int len = 0;
  60.                         byte[] a = null;
  61.                         while ((l = in.read(b)) != -1) {
  62.                                 a = new byte[1024];
  63.                                 a = b.clone();
  64.                                 len = l;
  65.                                 li.add(a);
  66.  
  67.                         }
  68.                                
  69.                         li.add(len);
  70.                         li.add(file[file.length - 1]);
  71.                        
  72.                         out.writeObject(li);
  73.                         out.flush();
  74.                         this.mf.getPane().getContendjt().setText(msg + "文件发送完毕.....\n");
  75.                 } catch (UnknownHostException e) {
  76.                         // TODO Auto-generated catch block
  77.                         JOptionPane.showMessageDialog(null, "没有连接到服务器...");
  78.                         return;
  79.                 } catch (FileNotFoundException f) {
  80.                         JOptionPane.showMessageDialog(null, "文件没有找到,请确认...");
  81.  
  82.                 } catch (IOException e) {
  83.                         // TODO Auto-generated catch block
  84.                         // e.printStackTrace();
  85.                         JOptionPane.showMessageDialog(null, "输入有误!!");
  86.                         return;
  87.                 } finally {
  88.                         if (out != null) {
  89.                                 try {
  90.                                         out.close();
  91.                                 } catch (IOException e) {
  92.                                         // TODO Auto-generated catch block
  93.                                         e.printStackTrace();
  94.                                 }
  95.                         }
  96.                         if (client != null) {
  97.                                 try {
  98.                                         client.close();
  99.                                 } catch (IOException e) {
  100.                                         // TODO Auto-generated catch block
  101.                                         e.printStackTrace();
  102.                                 }
  103.                         }
  104.                         if (in != null) {
  105.                                 try {
  106.                                         in.close();
  107.                                 } catch (IOException e) {
  108.                                         // TODO Auto-generated catch block
  109.                                         e.printStackTrace();
  110.                                 }
  111.                         }
  112.                 }
  113.         }
  114.  
  115. }
  116. //源代码片段来自云代码http://yuncode.net
  117.                        

Reply to "Java online chat and file transfer system 7"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website