当前位置:首页 » 《随便一记》 » 正文

org.csource.common.MyException: item “tracker_server“ in null not found异常已解决_Fall in your eyes的博客

17 人参与  2022年04月19日 09:27  分类 : 《随便一记》  评论

点击全文阅读


org.csource.common.MyException: item “tracker_server” in null not found异常已解决

背景

自己写的博客项目使用fastDfs 使用java工具类A初始化fdfs配置进行测试,代码如下

			ClassPathResource cpr = new ClassPathResource("fastdfs.properties");
			ClientGlobal.init(cpr.getClassLoader().getResource("fdfs_client.conf").toURI().getPath());

			Properties properties = new Properties();
			// 使用ClassLoader加载properties配置文件生成对应的输入流
			InputStream in = cpr.getInputStream();
			// 使用properties对象加载输入流
			properties.load(in);
			ClientGlobal.initByProperties(properties);

			TrackerGroup trackerGroup = ClientGlobal.g_tracker_group;
			trackerClient = new TrackerClient(trackerGroup);

工具类B初始化fdfs配置进行测试,代码如下

private static final String CONF_FILENAME = Thread.currentThread().getContextClassLoader()
			.getResource("fdfs_client.conf").getPath();
			
--------------------------------------------
			ClientGlobal.init(CONF_FILENAME);

使用这两种方式都是去读取配置文件fdfs_client.conf,但是本地都正常,服务器上(CentOS7.6)不行,报错内容如下

java.lang.NullPointerException
        at java.io.File.<init>(File.java:277)
        at org.csource.common.IniFileReader.loadFromOsFileSystemOrClasspathAsStream(IniFileReader.java:45)
        at org.csource.common.IniFileReader.loadFromFile(IniFileReader.java:150)
        at org.csource.common.IniFileReader.<init>(IniFileReader.java:30)
        at org.csource.fastdfs.ClientGlobal.init(ClientGlobal.java:91)
        at com.blog.utils.FastDFSClientUtils.<clinit>(FastDFSClientUtils.java:51)
        at com.blog.controller.FastDFSController.upload(FastDFSController.java:53)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197)
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:141)
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1064)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
        at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:681)
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
        at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
        at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
        at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
        at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
        at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:387)
        at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
        at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
        at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
        at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
        at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357)
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:382)
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1726)
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
        at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
        at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Thread.java:748)
09:13:42,711 [http-nio-8000-exec-1] ERROR com.blog.utils.FastDFSClientUtils  - 
org.csource.common.MyException: item "tracker_server" in null not found
2021-11-24 21:13:42.712  INFO 2498 --- [nio-8000-exec-1] com.blog.controller.FastDFSController    : ---文件类型--->jpg
09:13:42,714 [http-nio-8000-exec-1] ERROR com.blog.utils.FastDFSClientUtils  - java.lang.NullPointerException

主要报错内容 org.csource.common.MyException: item “tracker_server” in null not found

解决办法

不使用读取配置文件的方法,直接把fdfs的配置写入工具类进行加载
代码如下

Properties props = new Properties();
props.put(ClientGlobal.PROP_KEY_TRACKER_SERVERS,"82.157.172.63:22122");
props.put(ClientGlobal.PROP_KEY_CONNECT_TIMEOUT_IN_SECONDS,3);
props.put(ClientGlobal.PROP_KEY_NETWORK_TIMEOUT_IN_SECONDS,30);
ClientGlobal.initByProperties(props);
TrackerClient trackerClient=new TrackerClient();

完成工具类代码

public class FastDFSClientUtils {

	private static Logger logger = Logger.getLogger(FastDFSClientUtils.class);

	private static TrackerClient trackerClient;


	//加载文件
	static {
		try {

			//加载fasetdfs配置项
			Properties props = new Properties();
			props.put(ClientGlobal.PROP_KEY_TRACKER_SERVERS,"82.157.172.63:22122");
			props.put(ClientGlobal.PROP_KEY_CONNECT_TIMEOUT_IN_SECONDS,3);
			props.put(ClientGlobal.PROP_KEY_NETWORK_TIMEOUT_IN_SECONDS,30);
			
			ClientGlobal.initByProperties(props);
			trackerClient=new TrackerClient();
//			/*第二种init方法  该方法我测试本地可以,服务器上不行。各位如使用自行测试
//			ClassPathResource cpr = new ClassPathResource("fastdfs.properties");
//			ClientGlobal.init(cpr.getClassLoader().getResource("fdfs_client.conf").toURI().getPath());
//
//			Properties properties = new Properties();
//			// 使用ClassLoader加载properties配置文件生成对应的输入流
//			InputStream in = cpr.getInputStream();
//			// 使用properties对象加载输入流
//			properties.load(in);
//			ClientGlobal.initByProperties(properties);
//
//			TrackerGroup trackerGroup = ClientGlobal.g_tracker_group;
//			trackerClient = new TrackerClient(trackerGroup);
//
//			System.out.println("------>"+trackerClient.getTrackerServer().getInetSocketAddress());
		} catch (Exception e) {
			logger.error(e);
		}
	}

	/**
	 * <B>方法名称:</B>上传方法<BR>
	 * <B>概要说明:</B><BR>
	 * @param file 文件
	 * @param path 路径
	 * @return 上传成功返回id,失败返回null
	 */
	public static String upload(File file, String path) {
		TrackerServer trackerServer = null;
		StorageServer storageServer = null;
		StorageClient1 storageClient1 = null;
		FileInputStream fis = null;
		try {
			NameValuePair[] meta_list = null; // new NameValuePair[0];
			fis = new FileInputStream(file);
			byte[] file_buff = null;
			if (fis != null) {
				int len = fis.available();
				file_buff = new byte[len];
				fis.read(file_buff);
			}

			trackerServer = trackerClient.getTrackerServer();
			if (trackerServer == null) {
				logger.error("getConnection return null");
			}
			storageServer = trackerClient.getStoreStorage(trackerServer);
			storageClient1 = new StorageClient1(trackerServer, storageServer);
			String fileid = storageClient1.upload_file1(file_buff, getFileExt(path), meta_list);

			return fileid;
		} catch (Exception ex) {
			logger.error(ex);
			return null;
		} finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					logger.error(e);
				}
			}
			storageClient1 = null;
		}
	}

	/**
	 * <B>方法名称:</B>上传方法<BR>
	 * <B>概要说明:</B><BR>
	 * @param data 数据
	 * @param extName 路径
	 * @return 上传成功返回id,失败返回null
	 */
	public static String upload(byte[] data, String extName) {
		TrackerServer trackerServer = null;
		StorageServer storageServer = null;
		StorageClient1 storageClient1 = null;
		try {
			NameValuePair[] meta_list = null; // new NameValuePair[0];

			trackerServer = trackerClient.getTrackerServer();
			if (trackerServer == null) {
				logger.error("getConnection return null");
			}
			storageServer = trackerClient.getStoreStorage(trackerServer);
			storageClient1 = new StorageClient1(trackerServer, storageServer);
			String fileid = storageClient1.upload_file1(data, extName, meta_list);
			return fileid;
		} catch (Exception ex) {
			logger.error(ex);
			return null;
		} finally {
			storageClient1 = null;
		}
	}

	/**
	 * <B>方法名称:</B>下载方法<BR>
	 * <B>概要说明:</B>通过文件id进行下载<BR>
	 * @param fileId 文件id
	 * @return 返回InputStream
	 */
	public static InputStream download(String groupName, String fileId) {
		TrackerServer trackerServer = null;
		StorageServer storageServer = null;
		StorageClient1 storageClient1 = null;
		try {
			trackerServer = trackerClient.getTrackerServer();
			if (trackerServer == null) {
				logger.error("getConnection return null");
			}
			storageServer = trackerClient.getStoreStorage(trackerServer, groupName);
			storageClient1 = new StorageClient1(trackerServer, storageServer);
			byte[] bytes = storageClient1.download_file1(fileId);
			InputStream inputStream = new ByteArrayInputStream(bytes);
			return inputStream;
		} catch (Exception ex) {
			logger.error(ex);
			return null;
		} finally {
			storageClient1 = null;
		}
	}

	/**
	 * <B>方法名称:</B>删除方法<BR>
	 * <B>概要说明:</B>根据id来删除一个文件<BR>
	 * @param fileId 文件id
	 * @return 删除成功返回0,非0则操作失败,返回错误代码
	 */
	public static int delete(String groupName, String fileId) {
		TrackerServer trackerServer = null;
		StorageServer storageServer = null;
		StorageClient1 storageClient1 = null;
		try {
			trackerServer = trackerClient.getTrackerServer();
			if (trackerServer == null) {
				logger.error("getConnection return null");
			}
			storageServer = trackerClient.getStoreStorage(trackerServer, groupName);
			storageClient1 = new StorageClient1(trackerServer, storageServer);
			int result = storageClient1.delete_file1(fileId);
			return result;
		} catch (Exception ex) {
			logger.error(ex);
			return 0;
		} finally {
			storageClient1 = null;
		}
	}
	/**
	 * <B>方法名称:</B><BR>
	 * <B>概要说明:</B><BR>
	 * @param oldFileId 旧文件id
	 * @param file 新文件
	 * @param path 新文件路径
	 * @return 上传成功返回id,失败返回null
	 *//*
	public static String modify(String oldGroupName, String oldFileId, File file, String path) {
		String fileid = null;
		try {
			// 先上传
			fileid = upload(file, path);
			if (fileid == null) {
				return null;
			}
			// 再删除
			int delResult = delete(oldGroupName, oldFileId);
			if (delResult != 0) {
				return null;
			}
		} catch (Exception ex) {
			logger.error(ex);
			return null;
		}
		return fileid;
	}

	*//**
	 * <B>方法名称:</B>获取文件后缀名<BR>
	 * <B>概要说明:</B>获取文件后缀名<BR>
	 * @param fileName
	 * @return 如:"jpg""txt""zip"*/
	private static String getFileExt(String fileName) {
		if (StringUtils.isBlank(fileName) || !fileName.contains(".")) {
			return "";
		} else {
			return fileName.substring(fileName.lastIndexOf(".") + 1);
		}
	}
	/**
	 * MultipartFile 转 File
	 *
	 * @param file
	 * @throws Exception
	 */
	public static File multipartFileToFile(MultipartFile file) throws Exception {

		File toFile = null;
		if ("".equals(file) || file.getSize() <= 0) {
			file = null;
		} else {
			InputStream ins = null;
			ins = file.getInputStream();
			toFile = new File(file.getOriginalFilename());
			inputStreamToFile(ins, toFile);
			ins.close();
		}
		return toFile;
	}

	//获取流文件
	public static void inputStreamToFile(InputStream ins, File file) {
		try {
			OutputStream os = new FileOutputStream(file);
			int bytesRead = 0;
			byte[] buffer = new byte[8192];
			while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
				os.write(buffer, 0, bytesRead);
			}
			os.close();
			ins.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 删除本地临时文件
	 * @param file
	 */
	public static void delteTempFile(File file) {
		if (file != null) {
			File del = new File(file.toURI());
			del.delete();
		}
	}

	public static void downloadPicture(String urlList, String imgUrl) {
		URL url = null;
		int imageNumber = 0;

		try {
			url = new URL(urlList);
			DataInputStream dataInputStream = new DataInputStream(url.openStream());

			String imageName = imgUrl;

			FileOutputStream fileOutputStream = new FileOutputStream(new File(imageName));
			ByteArrayOutputStream output = new ByteArrayOutputStream();

			byte[] buffer = new byte[1024];
			int length;

			while ((length = dataInputStream.read(buffer)) > 0) {
				output.write(buffer, 0, length);
			}
			byte[] context = output.toByteArray();
			fileOutputStream.write(output.toByteArray());
			dataInputStream.close();
			fileOutputStream.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

至此,两天没解决的问题告一段落!
记录一下备忘!!!


点击全文阅读


本文链接:http://zhangshiyu.com/post/38396.html

方法  文件  概要  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1