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

📄 sendwork.java

📁 JAVA编的聊天和文件发送程序
💻 JAVA
字号:
/*
 * SendWork.java
 *
 * Created on 2007年10月16日, 上午10:38
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package chat;
import common.FileInfo;
import java.awt.*;
import java.io.*;
import java.net.*;
import javax.swing.*;

/**
 *
 * @author olry
 */
public class SendWork implements Runnable
{
    
    private File file;
    private String ip;
    private int port;
    
    /** Creates a new instance of SendWork */
    public SendWork(File file,String ip,int port)
    {
        this.file=file;
        this.ip=ip;
        this.port=port;
        
    }
    
    public void run()
    {
        System.out.println("begin send ");
        FileInfo info=new FileInfo();
        info.setFileName(file.getName());
        info.setFileLength(file.length());
        
        FileInputStream fis=null;
        Socket socket=null;
        try
        {
            System.out.println("set socket");
            socket=new Socket(this.ip,this.port);
            OutputStream os=socket.getOutputStream();
            ObjectOutputStream oos=new ObjectOutputStream(os);
            oos.writeObject(info);
            System.out.println("end");
            InputStream is=socket.getInputStream();
            byte[] response=new byte[1024];
            int i=is.read(response);
            String answer=new String(response,0,i);
            System.out.println("hello");
            if(answer.equalsIgnoreCase("no"))
            {
                String msg=ip+" 拒绝接收["+this.file.getName()+"]文件";
                JOptionPane.showMessageDialog(null,msg,"文件发送",JOptionPane.INFORMATION_MESSAGE);
                
            }
            else
            {
                fis=new FileInputStream(this.file);
                BufferedOutputStream bos=new BufferedOutputStream(os,1024*100);
                byte[] data=new byte[1024*20];
                int count=fis.read(data);
                while(count!=-1)
                {
                    bos.write(data,0,count);
                    count=fis.read(data);
                }
                bos.flush();
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            if(socket!=null)
            {
                try
                {
                    socket.close();
                }
                catch(IOException ex)
                {
                }
            }
            if(fis!=null)
            {
                try
                {
                    fis.close();
                }
                catch(IOException ex)
                {
                    
                }
            }
            
            
        }
        
    }
    
}

⌨️ 快捷键说明

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