将文件数据传递到函数中



Hi我一直在尝试将文件中的ipAdresses传递给将执行GET操作的SNMP函数。我正在逐行读取文件并传递数据。但我在setAddress上遇到了一个错误,如果我不从文件中传递,程序可以正常工作。请参阅代码中的第1行已注释ipAddress;

代码:

package com.snmp.discovery;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream.GetField;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Integer32;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
public class Discover1 {
    private static String  port    = "8001";
    // OID of MIB CISCO-MGMT-MDM; Scalar Object = .iso.org.dod.internet.mgmt.mib-2.system.sysDescr.0
    private static String  oidValue  = ".1.3.6.1.2.1.1.5.0";  // ends with 0 for scalar object
    private static int    snmpVersion  = SnmpConstants.version1;
    private static String  community  = "public";
    public static void main(String[] args) {
         try {
            // Open the file that is the first 
            // command line parameter
            FileInputStream fstream = new FileInputStream("textfile.txt");
            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            //Read File Line By Line
            while ((strLine = br.readLine()) != null)   {
              // Print the content on the console
                Discover1.getDevice(strLine);
            }
            //Close the input stream
            in.close();
        }catch (Exception e){//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
    }
    public static void getDevice(String a) throws Exception {
        System.out.println("SNMP GET Demo");
        System.out.println(a);
        // Create TransportMapping and Listen
        TransportMapping transport = new DefaultUdpTransportMapping();
        transport.listen();
        // Create Target Address object
        CommunityTarget comtarget = new CommunityTarget();
        comtarget.setCommunity(new OctetString(community));
        comtarget.setVersion(snmpVersion);
        comtarget.setAddress(new UdpAddress(a+"/"+port));
        System.out.println("------------");
        comtarget.setRetries(2);
        comtarget.setTimeout(1000);
        // Create the PDU object
        PDU pdu = new PDU();
        pdu.add(new VariableBinding(new OID(oidValue)));
        pdu.setType(PDU.GET);
        pdu.setRequestID(new Integer32(1));
        // Create Snmp object for sending data to Agent
        Snmp snmp = new Snmp(transport);
        System.out.println("Sending Request to Agent...");
        ResponseEvent response = snmp.get(pdu, comtarget);
        if (response != null){
            System.out.println("Got Response from Agent");
            PDU responsePDU = response.getResponse();
            if (responsePDU != null){
                int errorStatus = responsePDU.getErrorStatus();
                int errorIndex = responsePDU.getErrorIndex();
                String errorStatusText = responsePDU.getErrorStatusText();
                if (errorStatus == PDU.noError){
                    System.out.println("Snmp Get Response = " + responsePDU.getVariableBindings());
                } else {
                    System.out.println("Error: Request Failed");
                    System.out.println("Error Status = " + errorStatus);
                    System.out.println("Error Index = " + errorIndex);
                    System.out.println("Error Status Text = " + errorStatusText);
                }
            } else {
                System.out.println("Error: Response PDU is null");
            }
        } else {
            System.out.println("Error: Agent Timeout... ");
        }
        snmp.close();
    }
}

所以这可能不是一个确切的答案,但它不适合注释。

这很糟糕:

catch(Exception e) { .... }

只有在其他人的方法throws Exception时才执行此操作。此外,如果可以的话,打那个人。

你自己的方法不应该是methodName() throws Exception。它应该特别是methodName () throws IOException

你应该做catch(IOException e) { ... } catch(IllegalArgumentException e) {...}

正如您所看到的,如果您不知道抛出了哪种异常,则很难进行故障排除。

最后,当请求有关异常的帮助时,通常应该包括堆栈跟踪。例如:

Exception in thread "main" java.lang.NullPointerExceptionat com.example.myproject.Book.getTitle(Book.java:16)at com.example.myproject.Author.getBookTitles(Author.java:25)at com.example.myproject.Bootstrap.main(Bootstrap.java:14)

此信息有助于调试。

从错误消息来看,文件中似乎有空格。当您直接传递IP地址时,没有空格,因此它可以工作。在使用之前,您需要删除空格。

更改跟随线

Discover1.getDevice(strLine);

删除前导空格和尾部空格。类似:

选项1:

Discover1.getDevice(strLine.trim());

选项2:

Discover1.getDevice(StringUtils.trim(strLine));

参考:StringUtils

相关内容

  • 没有找到相关文章

最新更新