TFTP实现文件的上传和下载 Linux上开启TFTP服务

Linux上开启TFTP服务
这里是在linux上开启Linux服务的引用链接

最近在做内核移植方面的工作,需要用uboot的tftp下载内核调试,之前一直都是在linux下编译,再将内核文件拷到windows下,通过Tftpd32这个软件开启tftp服务,在uboot下 tftp c008000 zimage.ram;

    虽然虚拟机和我的主机文件能通过复制、粘贴进行拷贝,但总感觉很别扭,不专业,于是就想在linux下实现tftp服务;

linux系统版本:fedora14;

1.下载安装tftp服务器、tftp客户端和守护进程xinetd三个包 yum install xinetd tftp tftp-server

目前最新版本应该是:

xinetd-2.3.14-33.fc14.i686

tftp-0.49-7.fc14.i686

tftp-server-0.49-7.fc14.i686

2.配置tftp:vim /etc/xinetd.d/tftp

service tftp { disable = no socket_type = dgram protocol = udp wait = yes user = root server = /usr/sbin/in.tftpd server_args = -s /lib/tftp -c pcer_source = 11 cps = 100 2 flags = IPv4 }

主要是修改红色部分:设置tftp服务器的文件路径,我是为了方便直接放在home目录下,若放其他目录,注意权限问题;
chomd 777 你设置的tftp存放文件的位置
chomd 777 /lib/tftp

3.关防火墙

我是通过 系统–》管理–》防火墙 就关了 ,也可通过命令方式。
或者将端口号开放也可以 firewall-cmd --add-port=69/tcp --permanent
或者将端口号开放也可以 firewall-cmd --add-port=69/udp --permanent

4.创建文件夹 在第2步设置的文件路径下创建自己的文件夹,同时注意修改权限;5.开启服务 先要开启守护进程:service xinetd start然后在客户端windows上开启 TFTPClient:在 系统–》管理–》服务 里开启就行了;6.测试

ok。。。。。。。。。。。。。

在客户端使用
tftp -i IP [get|put] 文件名

-------------下面是web中java实现的TFTP文件的上传和下载----------------------------------------------
关于TFTP协议Java的实现
先引入maven依赖

   <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>3.3</version>
        </dependency>

下面是工具代码

 
public class TFTPUtil {  
    private static TFTPClient tftp = new TFTPClient();  
  
      
    public static boolean downloadFile(String hostname, String localFilename,  
            String remoteFilename,int port) {  
          
        // 设置超时时间为60秒  
        tftp.setDefaultTimeout(60000);  
          
        // 打开本地socket  
        try {  
            tftp.open();  
        } catch (SocketException e) {  
            System.err.println("无法打开本地 UDP socket!");  
            System.err.println(e.getMessage());  
        }  
          
        boolean closed,success;  
        closed = false;  
        success = false;  
        FileOutputStream output = null;  
        File file;  
  
        file = new File(localFilename);  
        if (file.exists()) {  
            System.err.println("文件: " + localFilename + " 已经存在!");  
            return success;  
        }  
  
        try {  
            output = new FileOutputStream(file);  
        } catch (IOException e) {  
            tftp.close();  
            System.err.println("无法打开要写入的本地文件!");  
            System.err.println(e.getMessage());  
            return success;  
        }  
  
        try {  
            tftp.receiveFile(remoteFilename, TFTP.BINARY_MODE, output, hostname, port);  
            //tftp.receiveFile(remoteFilename, TFTP.BINARY_MODE, output, hostname);  
            success = true;  
        } catch (UnknownHostException e) {  
            System.err.println("无法解析主机!");  
            System.err.println(e.getMessage());  
            return success;  
        } catch (IOException e) {  
            System.err.println("接收文件时有I/O异常!");  
            System.err.println(e.getMessage());  
            return success;  
        } finally {  
            // 关闭本地 socket 和输出的文件  
            tftp.close();  
            try {  
                if (null != output) {  
                    output.close();  
                }  
                closed = true;  
            } catch (IOException e) {  
                closed = false;  
                System.err.println("关闭文件时出错!");  
                System.err.println(e.getMessage());  
            }  
        }  
        if (!closed)  
            return false;  
          
        return success;  
    }  
      
    public static boolean uploadFile(String hostname, String remoteFilename, InputStream input ,int port) {  
        // 设置超时时间为10秒  
        tftp.setDefaultTimeout(10000);  
          
        // 打开本地socket  
        try {  
            tftp.open();  
        } catch (SocketException e) {  
            System.err.println("无法打开本地 UDP socket!");  
            System.err.println(e.getMessage());  
        }  
          
        boolean success,closed;  
        closed = false;  
        success = false;  
         
        try {  
             tftp.sendFile(remoteFilename, TFTP.BINARY_MODE, input, hostname, port);  
             success = true;  
        } catch (UnknownHostException e) {  
            System.err.println("无法解析主机!");  
            System.err.println(e.getMessage());  
            return success;  
        } catch (IOException e) {  
            System.err.println("发送文件时有I/O异常!");  
            System.err.println(e.getMessage());  
            return success;  
            //System.exit(1);  
        } finally {  
            // 关闭本地 socket 和输出的文件  
            tftp.close();  
            try {  
                if (null != input) {  
                    input.close();  
                }  
                closed = true;  
            } catch (IOException e) {  
                closed = false;  
                System.err.println("关闭文件时出错!");  
                System.err.println(e.getMessage());  
            }  
        }  
          
        if (!closed)  
            return false;  
         
        return success;  
    }  
      
      
    public static boolean uploadFile(String hostname, String remoteFilename, String localFilePath,int port) {  
          
        FileInputStream fileInput=null;  
        try {  
            fileInput=new  FileInputStream(localFilePath);  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        }  
                     
        return uploadFile( hostname,  remoteFilename,  fileInput , port);  
    }  
      
      
    public static void main(String[] args) {  
        /*String hostname="192.168.20.200"; 
        String remoteFilename="/tftpboot/4.txt"; 
        String localFilePath="D:/1.txt"; 
        int port=69; 
        TFTPUtil.uploadFile(hostname, remoteFilename, localFilePath, port);*/  
        
        TFTPUtil.downloadFile("192.168.20.200", "D:/44.txt", "/tftpboot/4.txt", 69);  
     
    }  
}  
```[官方代码链接](http://commons.apache.org/proper/commons-net/examples/ftp/TFTPExample.java)


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值