java 一些Java的的小片断,想不起来的时候到这里找

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 一些Java的的小片断,想不起来的时候到这里找相关的知识,希望对你有一定的参考价值。

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;

import javax.servlet.ServletContext;

import org.springframework.web.context.ServletContextAware;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import freemarker.template.Configuration;
import freemarker.template.Template;
/**
 * 配置式开发静态化
 * @author lx
 *
 */
													//通过实现ServletContextAware接口获取上下文对象
public class StaticPageServiceImpl implements StaticPageService,ServletContextAware{
	//注入:通过set方法注入,在配置文件中配置
	private Configuration conf;
	public void setFreeMarkerConfigurer(FreeMarkerConfigurer freeMarkerConfigurer) {
		this.conf = freeMarkerConfigurer.getConfiguration();
	}
	//静态化程序,参数1:页面上要显示的数据,参数2:商品id
	public void index(Map<String,Object> root,String id){
		//路径
		String path = "/html/product/" + id + ".html";
		//C:\workspace\.metadata\.plugins
		//\org.eclipse.wst.server.core\tmp6\wtpwebapps\babasport-service-cms/html/product/442.html
		String url = getAllPath(path);
		
		File f = new File(url);
		File parentFile = f.getParentFile();
		if(!parentFile.exists()){
			parentFile.mkdirs();
		}
		//加载模板  加载到内存中形成对象
		Writer out = null;
		try {
			//读
			Template template = conf.getTemplate("product.html");
			//输出流到指定文件
			out = new OutputStreamWriter(new FileOutputStream(f),"UTF-8");
			//处理
			template.process(root, out);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if(null != out){
					out.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	//获取全路径
	public String getAllPath(String path){
		return servletContext.getRealPath(path);
	}
	
	//声明一个上下文件
	private ServletContext servletContext;
	@Override//实现ServletContextAware接口重写setServletContext方法
	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}
}
ClassPathResource resource = new ClassPathResource("fdfs_client.conf");
//提交 Form表单
$("#jvForm").attr("action","/product/isShow.do");
$("#jvForm").attr("method","post");
$("#jvForm").submit();

//点击表单中的按钮提交表单
$(function(){
	$("#loginsubmit").click(function(){
		$("#loginForm").submit();
	});
});
<script type="text/javascript">
	//全选
	function checkBox(name,checked){
		$("input[name='"+name+"']").attr("checked",checked);
	}
</script>
<!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    -->
    <!-- 修改tomcat的Servers中的server.xml文件 -->
    <Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8084" protocol="HTTP/1.1" redirectPort="8443"/>
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" />
    -->
package aa;
import java.util.ArrayList;
import java.util.Collection;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOCase;
public class FilenameUtilstest {
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  String basePath = "D:\\aa\\bb\\cc\"; 
  String fileName = "file.txt";    
  String fullFileName = basePath+fileName;
  System.out.println("1、显示linux路径:"+FilenameUtils.normalizeNoEndSeparator(fullFileName));
  System.out.println("2、合并目录和文件名为文件全路径:"+FilenameUtils.concat(basePath, fileName));
  System.out.println("3、文件路径去除目录和后缀后的文件名:"+FilenameUtils.getBaseName(fullFileName));
  System.out.println("4、获取文件的后缀:"+FilenameUtils.getExtension(fullFileName));
  System.out.println("5、获取文件的目录:"+FilenameUtils.getFullPath(fullFileName));
  System.out.println("6、获取文件的目录不包含结束符:"+FilenameUtils.getFullPathNoEndSeparator(fullFileName));
  System.out.println("7、获取文件名称,包含后最:"+FilenameUtils.getName(fullFileName));
  System.out.println("8、去除前缀的路径:"+FilenameUtils.getPath(fullFileName)); 
  System.out.println("9、去除前缀并结尾去除分隔符:"+FilenameUtils.getPathNoEndSeparator(fullFileName));
  System.out.println("10、获取前缀:"+FilenameUtils.getPrefix(fullFileName));
  System.out.println("11、获取前缀长度:"+FilenameUtils.getPrefixLength(fullFileName));
  System.out.println("12、获取最后一个.的位置:"+FilenameUtils.indexOfExtension(fullFileName));
  System.out.println("13、获取最后一个/的位置:"+FilenameUtils.indexOfLastSeparator(fullFileName));
  System.out.println("14、获取当前系统格式化路径:"+FilenameUtils.normalize(fullFileName));
  System.out.println("15、获取linux系统格式化路径:"+FilenameUtils.normalize(fullFileName));
  System.out.println("16、获取当前系统无结尾分隔符的路径:"+FilenameUtils.normalizeNoEndSeparator(basePath));
  System.out.println("17、获取linux系统无结尾分隔符的路径:"+FilenameUtils.normalizeNoEndSeparator(basePath));
  System.out.println("18、移除文件的扩展名:"+FilenameUtils.removeExtension(fullFileName));
  System.out.println("19、转换分隔符为当前系统分隔符:"+FilenameUtils.separatorsToSystem(fullFileName));  
  System.out.println("20、转换分隔符为linux系统分隔符:"+FilenameUtils.separatorsToUnix(fullFileName));
  System.out.println("20、转换分隔符为linux系统分隔符:"+FilenameUtils.separatorsToUnix(fullFileName));
  System.out.println("21、转换分隔符为windows系统分隔符:"+FilenameUtils.separatorsToWindows(fullFileName));
  //System.out.println("22、判断目录下是否包含指定文件或目录:"+FilenameUtils.directoryContains(basePath, fullFileName));
  String linuxFileName = FilenameUtils.normalize(fullFileName);
  System.out.println("23、判断文件路径是否相同,非格式化:"+FilenameUtils.equals(fullFileName, linuxFileName));
  System.out.println("24、判断文件路径是否相同,格式化并大小写不敏感:"+FilenameUtils.equals(fullFileName, FilenameUtils.normalize(fullFileName),true,IOCase.INSENSITIVE));  
  System.out.println("25、判断文件路径是否相同,格式化并大小写敏感:"+FilenameUtils.equalsNormalized(fullFileName, linuxFileName));
  System.out.println("26、判断文件路径是否相同,不格式化,大小写敏感根据系统规则:windows:敏感;linux:不敏感:"+FilenameUtils.equalsOnSystem(fullFileName, linuxFileName)); 
  Collection extensions = new ArrayList();   extensions.add("txt");   extensions.add("java");   System.out.println("27、判断文件扩展名是否包含在指定集合中:"+FilenameUtils.isExtension(fullFileName, extensions)); 
  System.out.println("28、判断文件扩展名是否等于指定扩展名:"+FilenameUtils.isExtension(fullFileName, "txt"));
  System.out.println("29、判断文件扩展名是否包含在指定字符串数组中:"+FilenameUtils.isExtension(fullFileName, new String[]{"txt","java"})); 
  System.out.println("30、判断文件扩展名是否和指定规则匹配,大小写敏感:"+FilenameUtils.wildcardMatch(fileName, "*.???")); 
  System.out.println("31、判断文件扩展名是否和指定规则匹配,大小写不敏感:"+FilenameUtils.wildcardMatch(fileName, "*.???",IOCase.INSENSITIVE));
  System.out.println("32、判断文件扩展名是否和指定规则匹配,根据系统判断敏感型:windows:不敏感;linux:敏感:"+FilenameUtils.wildcardMatchOnSystem(fileName, "*.???"));
 }
}

以上是关于java 一些Java的的小片断,想不起来的时候到这里找的主要内容,如果未能解决你的问题,请参考以下文章

SUSCTF2022的两道Java复现

SUSCTF2022的两道Java复现

SUSCTF2022的两道Java复现

前端经验之谈(在项目中经常使用,但是有时候想不起来的一些东西,待完善,想起一个加一个)

java for循环中的sleep

Java策略模式之总有你想不到的知识