怎样在JavaWeb项目中自定义一个配置文件。

怎样在JavaWeb项目中自定义一个配置文件。,第1张

你直接写个过滤类就可以了(使用 filter  的3个方法  你懂得)

package com.enation.eopimport java.io.BufferedReaderimport java.io.IOExceptionimport java.io.InputStreamimport java.io.InputStreamReaderimport java.io.PrintWriterimport java.util.Collectionimport java.util.Dateimport java.util.Listimport java.util.Localeimport javax.servlet.Filterimport javax.servlet.FilterChainimport javax.servlet.FilterConfigimport javax.servlet.ServletExceptionimport javax.servlet.ServletOutputStreamimport javax.servlet.ServletRequestimport javax.servlet.ServletResponseimport javax.servlet.http.Cookieimport javax.servlet.http.HttpServletRequestimport javax.servlet.http.HttpServletResponseimport com.enation.eop.processor.back.BackendProcessorimport com.enation.eop.processor.core.freemarker.FreeMarkerPaserimport com.enation.eop.processor.facade.FacadeProcessorimport com.enation.eop.processor.facade.InstallProcessorimport com.enation.eop.processor.facade.ResourceProcessorimport com.enation.eop.resource.IAppManagerimport com.enation.eop.resource.model.EopAppimport com.enation.eop.sdk.context.EopContextimport com.enation.eop.sdk.context.EopContextIniterimport com.enation.eop.sdk.context.EopSettingimport com.enation.framework.context.spring.SpringContextHolderimport com.enation.framework.context.webcontext.ThreadContextHolderpublic class DispatcherDemoFilter implements Filter {    public void init(FilterConfig config) {

}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

//        HttpServletRequest  req= ServletActionContext.getRequest()//        String  flag= req.getHeader("X-Requested-With")//            HttpServletResponse   response  = ServletActionContext.getResponse()

String ip = ((HttpServletRequest) request).getHeader("x-forwarded-for")       if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){

ip = ((HttpServletRequest) request).getHeader("Proxy-Client-IP")

}        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){

ip = ((HttpServletRequest) request).getHeader("WL-Proxy-Client-IP")

}        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){

ip = request.getRemoteAddr()

}

String authtoken = request.getParameter("auth-token")

System.out.println(authtoken)

StringBuffer sb = new StringBuffer()

InputStream is = request.getInputStream()

InputStreamReader isr = new InputStreamReader(is)

BufferedReader br = new BufferedReader(isr)

String s = ""

while((s=br.readLine())!=null){

sb.append(s)

}

System.out.println(sb)

String str =sb.toString()

System.out.println(ip+"时间"+new Date().getTime())       //response.setCharacterEncoding("utf-8")

response.setContentType("application/xml")

String write = "<cart><items><item feed-id=\"449573\" offer-id=\"15\" price=\"825\" count=\"1\" delivery=\"true\"/>" +

"</items><delivery-options><delivery service-name=\"Курьер\" type=\"DELIVERY\" price=\"1490.00\">" +

"<dates from-date=\"24-10-2016\" to-date=\"26-10-2016\"/></delivery>" +

"</delivery-options><payment-methods>" +

"<payment-method>CASH_ON_DELIVERY</payment-method></payment-methods></cart>"

System.out.println(write)

response.getWriter().write(write)

}    public void destroy() {

}

private static boolean isExinclude(String uri) {

String[] exts = new String[] { "jpg", "gif", "js", "png", "css", "doc", "xls", "swf","ico" }

for (String ext : exts) {            if (uri.toUpperCase().endsWith(ext.toUpperCase())) {                return true

}

}        return false

}

}

要使用Java修改FreeSWITCH的配置文件,您可以使用Java IO类库中的FileWriter和BufferedWriter类来打开、读取和写入配置文件。以下是一个简单的示例代码:

java

Copy code

import java.io.*

public class ModifyConfigFile {

public static void main(String[] args) {

try {

// 打开配置文件

File file = new File("/usr/local/freeswitch/conf/sip_profiles/external.xml")

BufferedReader reader = new BufferedReader(new FileReader(file))

// 读取配置文件

String line

StringBuilder stringBuilder = new StringBuilder()

while ((line = reader.readLine()) != null) {

// 进行修改 *** 作

if (line.contains("<param name=\"ext-rtp-ip\"")) {

line = "<param name=\"ext-rtp-ip\" value=\"192.168.1.100\"/>"

}

stringBuilder.append(line).append("\n")

}

reader.close()

// 写入修改后的内容

FileWriter writer = new FileWriter(file)

BufferedWriter bufferedWriter = new BufferedWriter(writer)

bufferedWriter.write(stringBuilder.toString())

bufferedWriter.close()

System.out.println("配置文件修改成功!")

} catch (IOException e) {

e.printStackTrace()

}

}

}

以上代码将打开"/usr/local/freeswitch/conf/sip_profiles/external.xml"文件,并查找包含字符串"<param name="ext-rtp-ip""的行,并将其替换为"<param name="ext-rtp-ip" value="192.168.1.100"/>"。然后,它将修改后的内容写回到同一文件中。

请注意,在实际 *** 作中,您需要根据您的实际需求修改代码中的文件路径和替换字符串,以确保代码能够正确地修改您想要修改的配置文件。此外,您还需要确保您的应用程序具有足够的权限来修改配置文件。

java里可以再在resources里面新建一个XML file或者 file文件

XML file 会自动生成XML头,在下面加入内容就可以了,首先要有一个根节点,然后如果需要用到一些类,如:spring的一些类,就需要引入包,如:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<context:component-scan base-package="com.dist.*.controller" />

</beans >

其中<?xml ……就是头,<beans 是根节点,下面的<content:……是内容。

如果添加的事properties文件,格式如下:

# 连接池配置

pool.size = 2

pool.max = 50

然后jsp调用读取xml文件的方法去读取自重的内容就可以了。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/8006043.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-12
下一篇 2023-04-12

发表评论

登录后才能评论

评论列表(0条)

保存