最近在解析ip数据库时,遇到一个问题。代码在本地可以运行,但是放在集群上时,却无法运行,问题可能出现在java 文件流无法加载hdfs 文件?之后转换下思路,试着先把ip解析出来。 主要代码来源于网上的一个博客,红色部分是本人修改部分
package ip; import java.io.*; import java.nio.ByteOrder; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import ip.LogFactory; import org.apache.log4j.Level; /** * Created by XNLEJ on 2017/10/25 0025. */ public class IpSeeker { //����IP���ݿ��� private String IP_FILE="QQWry.Dat"; //������ļ��� private String INSTALL_DIR="f:/qqwry"; // һЩ�̶������������¼���ȵȵ� private static final int IP_RECORD_LENGTH = 7; private static final byte REDIRECT_MODE_1 = 0x01; private static final byte REDIRECT_MODE_2 = 0x02; // ������Ϊcache����ѯһ��ipʱ���Ȳ鿴cache���Լ��ٲ���Ҫ���ظ����� private Map<String, IPLocation> ipCache; // ����ļ������� private RandomAccessFile ipFile; // �ڴ�ӳ���ļ� private MappedByteBuffer mbb; // ��ʼ�����Ŀ�ʼ�ͽ����ľ���ƫ�� private long ipBegin, ipEnd; // Ϊ���Ч�ʶ����õ���ʱ���� private IPLocation loc; private byte[] buf; private byte[] b4; private byte[] b3; public IpSeeker(String fileName,String dir) { this.INSTALL_DIR=dir; this.IP_FILE=fileName; ipCache = new HashMap<String, IPLocation>(); loc = new IPLocation(); buf = new byte[1024]; b4 = new byte[4]; b3 = new byte[3]; try { ipFile = new RandomAccessFile(IP_FILE, "r"); } catch (FileNotFoundException e) { // ����Ҳ�������ļ����ٳ����ٵ�ǰĿ¼�����������ȫ������Сд�ļ��� // ��Ϊ��Щϵͳ�������ִ�Сд�����Ҳ���ip��ַ��Ϣ�ļ� String filename = new File(IP_FILE).getName().toLowerCase(); File[] files = new File(INSTALL_DIR).listFiles(); for(int i = 0; i < files.length; i++) { if(files[i].isFile()) { if(files[i].getName().toLowerCase().equals(filename)) { try { ipFile = new RandomAccessFile(files[i], "r"); } catch (FileNotFoundException e1) { LogFactory.log("IP��ַ��Ϣ�ļ�û���ҵ���IP��ʾ���ܽ��