获取打包后项目static文件夹内文件
Resource resource = new PathMatchingResourcePatternResolver().getResource("classpath:static/uploadFiles/a1.rar");
String url = resource.getURL().getPath().replace("!/", "/");
InputStream inputStream = resource.getInputStream();
不能用/*只获取一级目录,不知道为啥
Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath:static/**");
for (Resource resource : resources) {
String url = resource.getURL().getPath().replace("!/", "/");
InputStream inputStream = resource.getInputStream();
}
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("static/uploadFiles/a1.rar");
获取打包后项目里static下的文件并下载
@RequestMapping(value = "downloadFile", produces = "text/html;charset=UTF-8")
public void downloadSalaryTemplateExcel(HttpServletRequest request, HttpServletResponse response) {
try {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("static/downloadFile/a1.rar");
//设置Http响应头告诉浏览器下载这个附件,下载的文件名也是在这里设置的
response.setHeader("Content-Disposition", "attachment;Filename=" + URLEncoder.encode("a1.rar", "UTF-8").replace("+", "%20"));
OutputStream outputStream = response.getOutputStream();
int len = 0;
while((len=inputStream.read()) != -1){
outputStream.write(len);
}
inputStream.close();
outputStream.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
Map<String,String> propertiesMap=new HashMap<>();
// 获得PathMatchingResourcePatternResolver对象
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
// 加载resource文件(也可以加载resources)
Resource resources = resolver.getResource("classpath:alipay.properties");
PropertiesFactoryBean config = new PropertiesFactoryBean();
config.setLocation(resources);
config.afterPropertiesSet();
Properties prop = config.getObject();
// 循环遍历所有得键值对并且存入集合
for (String key : prop.stringPropertyNames()) {
System.out.println(key);
System.out.println((String) prop.get(key));
propertiesMap.put(key, (String) prop.get(key));
}
} catch (Exception e) {
new Exception("配置文件加载失败");
}
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.util.Map;
public class YamlUtil {
private static StringBuilder stringBuilder=new StringBuilder()
.append("stat")
.append("ic/asse")
.append("ts/ke")
.append("y/PROJ")
.append("ECT_ST")
.append("ATUS.")
.append("ya")
.append("ml");
public static void updateTaml(Object object,String name, String value) throws FileNotFoundException {
URL url = object.getClass().getClassLoader().getResource(stringBuilder.toString());
if(url!=null){
DumperOptions dumperOptions = new DumperOptions();
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
dumperOptions.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);
dumperOptions.setPrettyFlow(false);
Yaml yaml = new Yaml(dumperOptions);
Map<String,String> map1 =(Map)yaml.load(new FileInputStream(url.getFile()));
map1.put(name,value);
yaml.dump(map1, new OutputStreamWriter(new FileOutputStream(url.getFile())));
}else {
}
}
public static String selectTaml(Object object,String name) throws FileNotFoundException {
URL url = object.getClass().getClassLoader().getResource(stringBuilder.toString());
if(url!=null){
DumperOptions dumperOptions = new DumperOptions();
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
dumperOptions.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);
dumperOptions.setPrettyFlow(false);
Yaml yaml = new Yaml(dumperOptions);
Map<String,String> map1 =(Map)yaml.load(new FileInputStream(url.getFile()));
return map1.get(name);
}else {
return null;
}
}
}