1 .TFTP 环境的配置,不同的类型的服务器,配置还是有些区别的
linux:百度
unix:百度
AIX: /etc/tftpaccess.ctl
2.工具类
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); } }
3.官方例子
http://commons.apache.org/proper/commons-net/examples/ftp/TFTPExample.java
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package examples.ftp; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.SocketException; import java.net.UnknownHostException; import org.apache.commons.net.tftp.TFTP; import org.apache.commons.net.tftp.TFTPClient; /*** * This is an example of a simple Java tftp client. * Notice how all of the code is really just argument processing and * error handling. * <p> * Usage: tftp [options] hostname localfile remotefile * hostname - The name of the remote host * localfile - The name of the local file to send or the name to use for * the received file * remotefile - The name of the remote file to receive or the name for * the remote server to use to name the local file being sent. * options: (The default is to assume -r -b) * -s Send a local file * -r Receive a remote file * -a Use ASCII transfer mode * -b Use binary transfer mode * <p> ***/ public final class TFTPExample { static final String USAGE = "Usage: tftp [options] hostname localfile remotefile\n\n" + "hostname - The name of the remote host\n" + "localfile - The name of the local file to send or the name to use for\n" + "\tthe received file\n" + "remotefile - The name of the remote file to receive or the name for\n" + "\tthe remote server to use to name the local file being sent.\n\n" + "options: (The default is to assume -r -b)\n" + "\t-s Send a local file\n" + "\t-r Receive a remote file\n" + "\t-a Use ASCII transfer mode\n" + "\t-b Use binary transfer mode\n"; public static void main(String[] args) { boolean receiveFile = true, closed; int transferMode = TFTP.BINARY_MODE, argc; String arg, hostname, localFilename, remoteFilename; TFTPClient tftp; // Parse options for (argc = 0; argc < args.length; argc++) { arg = args[argc]; if (arg.startsWith("-")) { if (arg.equals("-r")) { receiveFile = true; } else if (arg.equals("-s")) { receiveFile = false; } else if (arg.equals("-a")) { transferMode = TFTP.ASCII_MODE; } else if (arg.equals("-b")) { transferMode = TFTP.BINARY_MODE; } else { System.err.println("Error: unrecognized option."); System.err.print(USAGE); System.exit(1); } } else { break; } } // Make sure there are enough arguments if (args.length - argc != 3) { System.err.println("Error: invalid number of arguments."); System.err.print(USAGE); System.exit(1); } // Get host and file arguments hostname = args[argc]; localFilename = args[argc + 1]; remoteFilename = args[argc + 2]; // Create our TFTP instance to handle the file transfer. tftp = new TFTPClient(); // We want to timeout if a response takes longer than 60 seconds tftp.setDefaultTimeout(60000); // Open local socket try { tftp.open(); } catch (SocketException e) { System.err.println("Error: could not open local UDP socket."); System.err.println(e.getMessage()); System.exit(1); } // We haven't closed the local file yet. closed = false; // If we're receiving a file, receive, otherwise send. if (receiveFile) { FileOutputStream output = null; File file; file = new File(localFilename); // If file exists, don't overwrite it. if (file.exists()) { System.err.println("Error: " + localFilename + " already exists."); System.exit(1); } // Try to open local file for writing try { output = new FileOutputStream(file); } catch (IOException e) { tftp.close(); System.err.println("Error: could not open local file for writing."); System.err.println(e.getMessage()); System.exit(1); } // Try to receive remote file via TFTP try { tftp.receiveFile(remoteFilename, transferMode, output, hostname); } catch (UnknownHostException e) { System.err.println("Error: could not resolve hostname."); System.err.println(e.getMessage()); System.exit(1); } catch (IOException e) { System.err.println( "Error: I/O exception occurred while receiving file."); System.err.println(e.getMessage()); System.exit(1); } finally { // Close local socket and output file tftp.close(); try { if (output != null) { output.close(); } closed = true; } catch (IOException e) { closed = false; System.err.println("Error: error closing file."); System.err.println(e.getMessage()); } } if (!closed) { System.exit(1); } } else { // We're sending a file FileInputStream input = null; // Try to open local file for reading try { input = new FileInputStream(localFilename); } catch (IOException e) { tftp.close(); System.err.println("Error: could not open local file for reading."); System.err.println(e.getMessage()); System.exit(1); } // Try to send local file via TFTP try { tftp.sendFile(remoteFilename, transferMode, input, hostname); } catch (UnknownHostException e) { System.err.println("Error: could not resolve hostname."); System.err.println(e.getMessage()); System.exit(1); } catch (IOException e) { System.err.println( "Error: I/O exception occurred while sending file."); System.err.println(e.getMessage()); System.exit(1); } finally { // Close local socket and input file tftp.close(); try { if (input != null) { input.close(); } closed = true; } catch (IOException e) { closed = false; System.err.println("Error: error closing file."); System.err.println(e.getMessage()); } } if (!closed) { System.exit(1); } } } }
相关推荐
在本例中,Java被选为实现TFTP协议的编程语言,这表明该实现可能适用于多种操作系统环境,并且可以与其他Java应用轻松集成。 4. **tftp.java**: 这是压缩包中的核心文件,很可能包含了TFTP协议的主要功能,如读写...
4. 在Java中使用`TFTPClient`时,需正确处理异常,确保文件传输的可靠性和安全性。 通过理解和实践Apache Commons Net提供的TFTP示例,开发者可以有效地集成TFTP功能到Java项目中,满足特定场景下的文件传输需求。...
本篇将深入探讨基于Java实现的TFTP客户端与服务器端源码,以及如何在Java工程中集成和编译这些代码。 TFTP协议主要由两个角色构成:TFTP客户端和TFTP服务器。客户端用于发起文件传输请求,而服务器则响应这些请求并...
【标题】"tftp4java 服务器、客户端" 涉及的是在Java环境中实现的TFTP(Trivial File Transfer Protocol)服务端和客户端程序。TFTP是一种简单、轻量级的文件传输协议,常用于网络设备配置、操作系统更新等场景,...
标题中的“基于JavaUDP开发的TFTp”指的是使用Java编程语言实现的用户数据报协议(UDP)上的简单文件传输协议(TFTP)。TFTP是一种轻量级的文件传输协议,常用于网络设备配置或者在有限带宽和简单环境下的文件交换。...
Java TFTP库是一种轻量级的文件传输协议(Trivial File Transfer Protocol)实现,主要用在需要简单、快速传输小文件的场景,如网络设备配置更新或操作系统引导加载。TFTP协议比FTP(File Transfer Protocol)更为...
TFTP(Trivial File Transfer Protocol)是一种简单且广泛使用的文件传输协议,尤其适用于初始化网络设备配置或进行固件更新。 描述中提到,“TFTP server,主要用于数据的tftp交互,使用方式为:tftp -l -g /tftp ...
《TFTP4Java:深入理解与应用》 TFTP4Java是针对TFTP(Trivial File Transfer Protocol)协议的一个开源Java实现,版本为0.8.1.8。TFTP是一种简单、轻量级的文件传输协议,常用于网络设备配置、软件更新等场景,因...
Java_TFTP: 使用Java开发TFTP协议 TFTP(Trivial File Transfer Protocol)是一种简单文件传输协议,常用于网络设备配置、操作系统更新等场景,因其轻量级和易于实现而被广泛采用。在Java中开发TFTP协议,可以帮助...
本文将详细介绍如何在Linux系统中配置TFTP服务器。 **一、TFTP服务器组件** 在Linux中,常用的TFTP服务器软件有`xinetd`(eXtended Internet Services Daemon)和`tftpd-hpa`。这里我们以`tftpd-hpa`为例进行讲解...
TFTP32J,全称为TFTP 32位Java版,是一款轻量级但功能强大的网络服务工具,集成了TFTP服务器、DHCP服务器以及Syslog服务器的功能,是网络管理与配置中不可或缺的实用软件。在本文中,我们将深入探讨这款工具的各个...
Java非对称加密源程序代码实例,本例中使用RSA加密技术,定义加密算法可用 DES,DESede,Blowfish等。 设定字符串为“张三,你好,我是李四” 产生张三的密钥对(keyPairZhang) 张三生成公钥(publicKeyZhang...
- **客户端代码**:这通常是用编程语言(如C、Python、Java等)编写的,负责从TFTP服务器获取文件。客户端会发出读请求,服务器响应并发送固件文件的数据包。 - **服务端代码**:这部分代码部署在TFTP服务器上,负责...
传统上,TFTP基于UDP(用户数据报协议)进行通信,但本项目中,我们探索了一种基于TCP的TFTP原型,这在某些网络环境下可能会提供更稳定的服务。 TCP(Transmission Control Protocol,传输控制协议)是一种面向连接...
### TFTP服务器源码解析与理解 ...- 分析其他编程语言中实现的TFTP服务器代码,比如Python或Java版本的实现。 通过这些额外的学习,可以更加深入地理解TFTP协议及其实际应用,为进一步的技术开发打下坚实的基础。
TFTP是一个基于UDP(User Datagram Protocol)的简单文件传输协议,不支持复杂的交互和认证机制,适合在网络配置和小型数据传输中使用。它主要由两个基本操作组成:GET(下载)和PUT(上传)。在这个项目中,我们...
Java非对称加密源程序代码实例,本例中使用RSA加密技术,定义加密算法可用 DES,DESede,Blowfish等。 设定字符串为“张三,你好,我是李四” 产生张三的密钥对(keyPairZhang) 张三生成公钥(publicKeyZhang...
Java非对称加密源程序代码实例,本例中使用RSA加密技术,定义加密算法可用 DES,DESede,Blowfish等。 设定字符串为“张三,你好,我是李四” 产生张三的密钥对(keyPairZhang) 张三生成公钥(publicKeyZhang...
Java非对称加密源码实例 1个目标文件 摘要:Java源码,算法相关,非对称加密 Java非对称加密源程序代码实例,本例中使用RSA加密技术,定义加密算法可用 DES,DESede,Blowfish等。 设定字符串为“张三,你好,我是李四”...
在Java中,telnet是一个非常有用的工具,允许开发者通过telnet协议与远程服务器进行交互,进行调试、测试或者管理远程服务。telnet库在Java中的实现使得开发者可以方便地创建telnet客户端或服务器应用程序。 Apache...