栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

org.csource.common.MyException: item “tracker

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

org.csource.common.MyException: item “tracker

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.(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.(IniFileReader.java:30)
        at org.csource.fastdfs.ClientGlobal.init(ClientGlobal.java:91)
        at com.blog.utils.FastDFSClientUtils.(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();
//			
	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;
		}
	}

	
	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;
		}
	}

	
	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;
		}
	}

	
	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;
		}
	}
	/*
	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;
	}

	*/
	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();
		}
	}

	
	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();
		}
	}
}

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/591259.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号