/** */ /** * 文件传送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(); } }}