InetAddress.getLocalHost().getHostName()

InetAddress.getLocalHost().getHostName()  这个异常 java.net.UnknownHostException


需要 /etc/hosts 和/etc/sysconfig/network  一致配置  

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import java.io.*; import java.net.*; import java.util.*; public class Server { public static void main(String args[]){ try { ServerSocket server = new ServerSocket(8888); System.out.println("服务器在监听8888端口..."); while (true) { Socket socket = server.accept(); new ReceFile(socket).start(); } } catch (Exception e) { e.printStackTrace(); } } } //服务器接收文件线程 class ReceFile extends Thread { private Socket socket; public ReceFile(Socket socket){ this.socket = socket; } public void run(){ InputStream is = null; OutputStream os = null; DataInputStream dis = null; InetAddress address = null; String ip = ""; try { //获得输入流 is = socket.getInputStream(); //获取连接此输入流的地址 address = socket.getInetAddress(); //返回ip ip = address.getHostAddress(); dis = new DataInputStream(is); //读取上传过过来的文件名 String fileName = dis.readUTF(); System.out.println("服务器接收到自"+ip+"的"+fileName+"文件"); //将接收到的文件保存到指定的目录 os = new FileOutputStream("File/"+fileName); int len = 0; byte[] b = new byte[1024]; while (true) { len = is.read(b); if (len==-1) { break; } os.write(b,0,len); os.flush(); } } catch (Exception e) { e.printStackTrace(); } finally{ try { if (is!=null) { is.close(); } if (dis!=null) { dis.close(); } if (os!=null) { os.close(); } } catch (Exception ex) { ex.printStackTrace(); } } } } import java.io.*; import java.net.*; import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import org.roy.*; public class Client extends JFrame implements ActionListener { private JButton selbutton,upbutton; private JPanel jp; private String path; public Client(){ super("文件上传"); this.setSize(250,250); this.setLocation(300,400); init(); this.setVisible(true); this.setDefaultCloseOperation(EXIT_ON_CLOSE); } public void init(){ selbutton = new JButton("选择文件"); upbutton = new JButton("上 传"); jp = new JPanel(); selbutton.addActionListener(this); upbutton.addActionListener(this); jp.add(selbutton); jp.add(upbutton); this.add(jp); } public void actionPerformed(ActionEvent e){ if(e.getSource()==selbutt

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值