读取properties文件的几种方式
来源:互联网 发布:淘宝螺旋是什么意思 编辑:程序博客网 时间:2024/05/28 06:07
先看一下文件在项目中的位置(这也是一般项目中的位置)
文件内的内容:
#图片服务器的地址IMAGE_SERVER_URL=http://192.168.25.133/
注:Java代码均在src/test中
方法一:
package test;import java.io.IOException;import java.io.InputStream;import java.util.Properties;import junit.framework.TestCase;/** *@作者:JackHisen(gwd) *@email:1154978352@qq.com *@时间:2017年8月23日 下午6:23:09 *@version 1.0 */public class testProperties extends TestCase{public void testPro() throws IOException{InputStream insss =this.getClass().getResourceAsStream("/conf/resources.properties");Properties pss = new Properties();pss.load(insss);System.out.println(pss.getProperty("IMAGE_SERVER_URL"));}}方法一有一个问题,我们都知道,this是不能在static(静态)方法或者static块中使用的,原因是static类型的方法或者代码块是属于类本身的,不属于某个对象,而this本身就代表当前对象,而静态方法或者块调用的时候是不用初始化对象的。
推荐写成如下形式:
public void testPro() throws IOException{InputStream insss =Object.class.getResourceAsStream("/conf/resources.properties");Properties pss = new Properties();pss.load(insss);System.out.println(pss.getProperty("IMAGE_SERVER_URL"));}
方法二:
package test;import java.io.IOException;import java.util.Properties;import org.springframework.core.io.ClassPathResource;import org.springframework.core.io.Resource;import org.springframework.core.io.support.PropertiesLoaderUtils;import junit.framework.TestCase;/** *@作者:JackHisen(gwd) *@email:1154978352@qq.com *@时间:2017年8月23日 下午6:23:09 *@version 1.0 */public class testProperties extends TestCase{public void testPro() throws IOException{Resource resource = new ClassPathResource("/conf/resources.properties");Properties props = PropertiesLoaderUtils.loadProperties(resource);System.out.println(props.getProperty("IMAGE_SERVER_URL"));}}
方法三:
1.spring_mvc.xml中加入如下代码读取该文件
<context:property-placeholder location="classpath:conf/resources.properties" />2.通过@value注解获取文件中的属性
package cn.e3mall.controller;import java.awt.PageAttributes.MediaType;import java.util.HashMap;import java.util.Map;import javax.print.attribute.standard.Media;import org.apache.log4j.Logger;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.multipart.MultipartFile;import cn.e3mall.common.utils.FastDFSClient;import cn.e3mall.common.utils.JsonUtils;/** * @作者:JackHisen(GWD) * @项目名:e3-manage-web * @时间:2017-8-22 下午5:35:34 * @version 1.0 */@Controllerpublic class PictureController {Logger logger=Logger.getLogger(PictureController.class);@Value("${IMAGE_SERVER_URL}")private String IMAGE_SERVER_URL;@RequestMapping(value="/pic/upload",produces="text/plain;charset=UTF-8")@ResponseBodypublic String fileUpload(MultipartFile uploadFile) {try {//1、取文件的扩展名String originalFilename = uploadFile.getOriginalFilename();String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);//2、创建一个FastDFS的客户端FastDFSClient fastDFSClient = new FastDFSClient("classpath:conf/client.conf");//3、执行上传处理String path = fastDFSClient.uploadFile(uploadFile.getBytes(), extName);//4、拼接返回的url和ip地址,拼装成完整的urlString url = IMAGE_SERVER_URL + path;//5、返回mapMap result = new HashMap<>();result.put("error",0);result.put("url",url);String json = JsonUtils.objectToJson(result);return json;} catch (Exception e) {e.printStackTrace();//5、返回mapMap result = new HashMap<>();result.put("error", 1);result.put("message", "图片上传失败");String json = JsonUtils.objectToJson(result);return json;}}}
阅读全文
1 0
- properties文件读取的几种方式
- Properties 文件 读取 的几种方式
- 读取properties文件的几种方式
- java几种读取.properties文件的方式
- 读取properties文件的几种常用方式
- java几种读取.properties文件的方式
- 读取Properties配置文件的几种方式
- 读取Properties文件的几种方法
- 读取Properties文件的几种方法
- 读取properties文件的几种方法
- 加载.properties文件的几种方式
- 读取.properties文件的两种方式
- 网上文章常见的几种读取.properties文件的方式
- 网上文章常见的几种读取.properties文件的方式
- 网上文章常见的几种读取.properties文件的方式
- properties文件的读取方式
- 读取properties文件的方式
- java读取.properties配置文件 的几种方式
- 剑指Offer—25—复杂链表的复制
- 推荐两个好用的PPT模板网站
- hdu 5450 Traversal(状压dp)
- Redis 集群的分库和分片
- 从后端获取数据,data数据传递
- 读取properties文件的几种方式
- xc-join方法
- STL vector,list,deque删除迭代器失效问题
- css样式——文字设置
- [js高手之路] dom常用API【appendChild,insertBefore,removeChild,replaceChild,cloneNode】详解与应用
- Primer (十四)
- JAVA图形界面设计
- 学生管理系统--排序规则的认识
- LightOJ 1307【暴力 二分】