懒得整理了,直接复制我工程里的代码:
第一个读取文件的类可以直接复制,
不复制的话,在下面取值和赋值部分的代码new EmailProperRead(systemType).properties根据自己情况修改。
一、读取文件的类EmailProperRead(一个完整文件,可以直接复制使用):
- package com.uniquedu.util;
-
- import java.io.File;
- import java.io.FileInputStream;
- import java.util.Properties;
-
- import javax.servlet.http.HttpServletRequest;
-
- import org.springframework.web.context.request.RequestContextHolder;
- import org.springframework.web.context.request.ServletRequestAttributes;
-
-
- public class EmailProperRead {
- public String iniPath = "/WEB-INF/systemConfig.Properties"; //文件路径
- public Properties properties = new Properties();
- @SuppressWarnings("deprecation")
- public EmailProperRead() throws Exception{
- HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
-
- iniPath = request.getRealPath("/")+iniPath;
-
- File file = new File(iniPath);
- if(!file.exists()){
- file.createNewFile();
- }
- properties.load(new FileInputStream(iniPath));
- }
-
- }
二、取值:
- Properties props = new Properties(); // 获取系统环境
- //获取邮箱配置信息
- final Properties properties = new EmailProperRead().properties;
- //发送邮件时显示的发件人名称
- String personalName = properties.getProperty("emailName");
- // 发件人的邮箱用户名
- final String usernames = properties.getProperty("emailAddress");
- // 发件人的邮箱密码
- final String passwords = properties.getProperty("emailPaswd");
- // 邮箱服务器
- String host = properties.getProperty("smtpAddress");
- //邮件主题
- String mail_subject = properties.getProperty("emailTheme");
三、赋值并保存文件:
- Properties properties = new EmailProperRead().properties;
- //发送邮件时显示的发件人名称
- properties.setProperty("emailName", request.getParameter("emailName"));
- //发件人的邮箱地址
- properties.setProperty("emailAddress", request.getParameter("emailAddress"));
- //发件人的邮箱密码
- properties.setProperty("emailPaswd", request.getParameter("emailPaswd"));
- //邮箱服务器
- properties.setProperty("smtpAddress", request.getParameter("smtpAddress"));
- //邮件主题
- properties.setProperty("emailTheme", request.getParameter("emailTheme"));
- properties.store(new FileOutputStream(new EmailProperRead(systemType).iniPath),"");