[Java] java在线聊天及文件传输系统8 →→→→→进入此内容的聊天室

来自 , 2021-03-02, 写在 Java, 查看 102 次.
URL http://www.code666.cn/view/571e0f7e
  1. package FileTran;
  2.  
  3. import java.io.EOFException;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.ObjectInputStream;
  9. import java.io.OutputStream;
  10. import java.net.Socket;
  11. import java.util.List;
  12.  
  13. import javax.swing.JFileChooser;
  14. import javax.swing.JOptionPane;
  15.  
  16. import MyFrame.Myframe;
  17.  
  18. public class FileMsgthread extends Thread {
  19.  
  20.         private Myframe mf;
  21.         private Socket sock = null;
  22.         private ObjectInputStream in = null;
  23.         private byte[] b = null;
  24.         private OutputStream out = null;
  25.         private String msg;
  26.         private JFileChooser jfc;
  27.         private File fi;
  28.         public FileMsgthread(Socket sock, Myframe mf) {
  29.                 this.sock = sock;
  30.                 this.mf = mf;
  31.                 msg = mf.getPane().getContendjt().getText();
  32.         }
  33.  
  34.         public void run() {
  35.                 try {
  36.                         in = new ObjectInputStream(this.sock.getInputStream());
  37.                         List li = null;
  38.                         List lb = null;
  39.  
  40.                         li = (List) this.in.readObject();
  41.  
  42.                         lb = li.subList(0, li.size() - 2);
  43.                         int len = (Integer) li.get(li.size() - 2);
  44.                         String file = (String) li.get(li.size() - 1);
  45.                        
  46.                         int judge=JOptionPane.showConfirmDialog(mf, "正在接受用户 "
  47.                                         + this.sock.getInetAddress().getHostAddress()
  48.                                         + " 发送文件:\n"+file);
  49.                         if(judge==0){
  50.                                 this.jfc=new JFileChooser();
  51.                                 this.fi=new File(file);
  52.                                 this.jfc.setSelectedFile(this.fi);
  53.                                 this.jfc.showSaveDialog(mf);
  54.                                 this.fi=this.jfc.getSelectedFile();
  55.                                 this.out = new FileOutputStream(this.fi);
  56.                         }else{
  57.                                 return;
  58.                         }
  59.                         this.mf.getPane().getContendjt().setText(msg + "接收中.......\n");
  60.                         int i = 0;
  61.                         while (i < lb.size()) {
  62.                                 b = (byte[]) lb.get(i);
  63.                                 if (i == (lb.size() - 1)) {
  64.                                         out.write(b, 0, len);
  65.                                         out.flush();
  66.                                         break;
  67.                                 }
  68.                                 out.write(b);
  69.                                 out.flush();
  70.                                 i++;
  71.                         }
  72.  
  73.                         this.mf.getPane().getContendjt().setText(msg + "文件接收完毕.....\n");
  74.  
  75.                 } catch (FileNotFoundException f) {
  76.                         JOptionPane.showMessageDialog(null, "文件存储有误...");
  77.                         return;
  78.                 } catch (EOFException eof) {
  79.                         JOptionPane.showMessageDialog(null, "无法接收文件...");
  80.                 } catch (IOException e) {
  81.                         // TODO Auto-generated catch block
  82.                         // e.printStackTrace();
  83.                         JOptionPane.showMessageDialog(null, "输入有误...");
  84.                         return;
  85.                 } catch (ClassNotFoundException e) {
  86.                         // TODO Auto-generated catch block
  87.                         // e.printStackTrace();
  88.                         JOptionPane.showMessageDialog(null, "无法找到文件");
  89.                         return;
  90.                 } finally {
  91.                         if (in != null) {
  92.                                 try {
  93.                                         in.close();
  94.                                 } catch (IOException e) {
  95.                                         // TODO Auto-generated catch block
  96.                                         e.printStackTrace();
  97.                                 }
  98.                         }
  99.  
  100.                         if (sock != null) {
  101.                                 try {
  102.                                         sock.close();
  103.                                 } catch (IOException e) {
  104.                                         // TODO Auto-generated catch block
  105.                                         e.printStackTrace();
  106.                                 }
  107.                         }
  108.                         if (this.out != null) {
  109.                                 try {
  110.                                         this.out.close();
  111.                                 } catch (IOException e) {
  112.                                         // TODO Auto-generated catch block
  113.                                         e.printStackTrace();
  114.                                 }
  115.                         }
  116.                 }
  117.  
  118.         }
  119.  
  120. }
  121. //源代码片段来自云代码http://yuncode.net
  122.                        

回复 "java在线聊天及文件传输系统8"

这儿你可以回复上面这条便签

captcha