All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cpj.swagger.util.ResourceUtil Maven / Gradle / Ivy

Go to download

cpj-swagger通过与swagger ui一起快速为您的web项目产生接口文档,并且支持在线测试接口。cpj-swagger可以很方便的与struts2、spring mvc、servlet集成使用

The newest version!
/*
 * Copyright 2011-2017 CPJIT Group.
 * 
 * 
 * Licensed 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 com.cpj.swagger.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.log4j.Logger;

/**
 * 查找资源相关的工具方法。
 * 

Note:该类用于从classpath下查找资源。

* @author yonghuan * @since 1.2.2 */ public final class ResourceUtil { private final static Logger sLogger = Logger.getLogger(ResourceUtil.class); private final static Class sDefaultLoader = ResourceUtil.class; private ResourceUtil() { throw new AssertionError("不允许实例化 " + ResourceUtil.class.getName()); } /** * 根据资源文件名获取资源文件的URL。 * @param loader * @param name 资源文件名 * @return 资源文件的URL。 */ public static URL getResource(Class loader,String name) { URL url = null; Class l = loader; if(l == null) { l = sDefaultLoader; } url = l.getResource("/" + name); if(url == null) { url = ClassLoader.getSystemResource(name); } return url; } /** * 根据资源文件名获取资源文件的URL。 * @param name 资源文件名 * @return 资源文件的URL。 */ public static URL getResource(String name) { return getResource(sDefaultLoader, name); } /** * 根据资源文件名获取资源文件。 * @param loader * @param name 资源文件名 * @return 资源文件 * @since 1.0.4 */ public static File getResourceAsFile(Class loader,String name) { URL url = getResource(loader, name); try { Path path = Paths.get(url.toURI()); return path.toFile(); } catch (URISyntaxException e) { return null; } } /** * 根据资源文件名获取资源文件。 * @param name * @return 资源文件 * @since 1.0.4 */ public static File getResourceAsFile(String name) { return getResourceAsFile(sDefaultLoader, name); } public static InputStream getResourceAsStream(Class loader,String name) { URL url = getResource(loader, name); if(url == null) { // 未找到资源 return null; } InputStream is = null; try { is = Files.newInputStream(Paths.get(url.toURI())); } catch (IOException ioe) { sLogger.error("查找资源文件 " + name + " 发生错误", ioe); } catch (URISyntaxException use) { sLogger.error("查找资源文件 " + name + " 发生错误", use); } return is; } public static InputStream getResourceAsStream(String name) { return getResourceAsStream(sDefaultLoader, name); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy