我的聊天/文件传输软件代码(----TransServerSocket代码)

/**
 * 文件传送Socket,使用的是ServerScket和socket技术!
 * 处理接收文件
 
*/

package  transSocket;
import  java.io. * ;
import  java.awt. * ;
import  java.net. * ;
import  javax.swing. * ;
import  filetrans. * ;
public   class  TransServerSocket  extends  Thread {
    
private ServerSocket serverSocket;
    
private Socket socket;
    
private int port;
    
private InetAddress address;
    
private JFrame frame;    
    
/**
     * 构造函数
     
*/
    
    
public TransServerSocket(JFrame fra){
        
this.frame=fra;
        jbInit();
    }

    
/**
     * 函数初始化
     
*/

    
public void jbInit(){
        
try{
           port 
=8002;
           socket
=new Socket();
           serverSocket
=new ServerSocket(port);    
        }

        
catch(IOException e){
            e.printStackTrace();
        }

        
    }

    
/**
     * 处理接收的文件
     
*/

    
public void run(){
        
try{
           
while(true)
           
{
               
int i=0;
               BufferedReader buffr
=null;
               BufferedInputStream buff
=null;
               InputStream in
=null;
               InputStream in1
=null;
           socket
=serverSocket.accept();
           
if(socket!=null)    i= JOptionPane.showConfirmDialog(null,"朋友"+socket.getInetAddress().getHostName()+"/"+socket.getInetAddress().getHostAddress()+
                                                             
"给你传送了文件是否接收YES/NO","传送文件",JOptionPane.YES_NO_OPTION);    
           
if(i==JOptionPane.YES_OPTION) {
                                                                 
           address
=socket.getInetAddress();
           in
=socket.getInputStream();        
           
          ProgressMonitorInputStream pr
=new ProgressMonitorInputStream(null,"reading the file",in);
          ProgressMonitor prog
=pr.getProgressMonitor();
          prog.setMillisToPopup(
100);
          prog.setMillisToDecideToPopup(
1);
          buff
=new BufferedInputStream(pr);                                                         
           String fileName
="";
           String filePath
="D:/My Documents/";
         
          
byte[] b=new byte[50];
          
byte[] c=new byte[1];
          
int l=buff.read(c);
          
int q=0;                         
           
while(l!=0&&c[0]!=' '{
                   b[q
++]=c[0];
                   l
=buff.read(c);
           }

           
           fileName
=new String(b);
           
if(fileName!=null){                                                              
           FileChooser savefile
=new FileChooser(frame);    
           savefile.setSaveFileChooser(fileName);           
           
int j=savefile.getsaveButton();
                                 
            
if(j==JFileChooser.APPROVE_OPTION) {
                                           
                  File file1
=savefile.getSaveSelectFile();
                  
if(file1.exists()&&file1.isFile()){
                      String st
=file1.getAbsolutePath(); System.out.println("4444444444===== "+st); 
                      
int m=JOptionPane.showConfirmDialog(null,st+"已经存在,要替换它嗎","保存文件",JOptionPane.WARNING_MESSAGE);
                   
if(m==JOptionPane.YES_OPTION) filePath=st;     
                   
else  {
                       FileChooser savefile1
=new FileChooser(frame);
                       savefile1.setSaveFileChooser(st);
                       
int n=savefile.getsaveButton();
                       
if(n==JFileChooser.APPROVE_OPTION){
                           File file3
=savefile1.getSaveSelectFile();
                           String str
=file3.getAbsolutePath();
                           filePath
=str;
                       }
                       
                      }

                 }

                 
else {
                     File file2
=savefile.getSaveSelectFile();
                     filePath
= file2.getAbsolutePath();
                 }
    
               File file
=new File(filePath); System.out.println("filePath111===== "+filePath); 
               
               BufferedOutputStream out
=new BufferedOutputStream(new FileOutputStream(file));
               
int length=0;
            
while((length=buff.available())!=0){           
                  
byte[] w=new byte[length];
                  
int p=buff.read(w,0,w.length);
                  
if(p!=-1){
                      out.write(w);
                  }

              }

          
           out.close();
           buff.close();
          
           socket.close();
           
          }
 
          }

          }
 
          }

        }

        
        
catch(IOException e){
                JOptionPane.showMessageDialog(
null,"对方下线或者网络出现故障请稍等..........."
                                                                   ,
"连接问题",JOptionPane.WARNING_MESSAGE);
            e.printStackTrace();
        }

        
catch(Exception ee){
            ee.printStackTrace();
        }

    }

}
    
           
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值