wg-basic/basic-util/src/main/java/ink/wgink/util/ResourceUtil.java

72 lines
1.9 KiB
Java

package ink.wgink.util;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import java.io.*;
/**
* @ClassName: ResourceUtil
* @Description: 资源工具
* @Author: WangGeng
* @Date: 2019-05-23 11:04
* @Version: 1.0
**/
public class ResourceUtil {
/**
* 资源输入流
*
* @param resourcePath
* @return
* @throws IOException
*/
public static InputStream getResourceInputStream(String resourcePath) throws IOException {
ResourceLoader resourceLoader = new DefaultResourceLoader();
return resourceLoader.getResource(resourcePath).getInputStream();
}
/**
* jar资源输入流
*
* @param resourcePath
* @return
* @throws IOException
*/
public static InputStream getJarResourceInputStream(String resourcePath) throws IOException {
ClassPathResource classPathResource = new ClassPathResource(resourcePath);
return classPathResource.getInputStream();
}
/**
* 资源文件
*
* @param resourcePath
* @return
* @throws IOException
*/
public static File getResourceFile(String resourcePath) throws IOException {
ResourceLoader resourceLoader = new DefaultResourceLoader();
return resourceLoader.getResource(resourcePath).getFile();
}
/**
* 输入流输出文本
*
* @param inputStream
* @return
* @throws IOException
*/
public static String inputStreamToText(InputStream inputStream) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
StringBuffer resultStr = new StringBuffer();
for (String line; (line = bufferedReader.readLine()) != null; ) {
resultStr.append(line);
}
bufferedReader.close();
return resultStr.toString();
}
}