zoukankan      html  css  js  c++  java
  • Java常用基础代码

    1.加载properties文件
    Properties properties = new Properties(); 
    properties.load(Properties.class.getResourceAsStream("/config.properties"));
    properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"));
    2.获取绝对路径
    ${pageContext.request.contextPath} 
     
    3.获取路径
    HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
    String contextPath=request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+"/";
     
    4.运行Junit注解配置
    @RunWith(SpringJUnit4ClassRunner.class) // 整合
    @ContextConfiguration(locations = "classpath:spring-bean-*.xml") // 加载配置 
     
    5.加载spring文件,获取bean

    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    RegisterDAO registerDAO = (RegisterDAO)ac.getBean("RegisterDAO");

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring.xml");
    RedisTemplate redisTemplate = (RedisTemplate)context.getBean("redisTemplate");

    如果是两个以上:
    ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","dao.xml"});

    或者用通配符:

    ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/*.xml"); 

    6.计算总页码数

    (totalCount + pageInfo.getLength() - 1) / pageInfo.getLength()

    7.分页算法

    pageInfo.setStart((page-1)*pageSize);

    8.对URL进行编码

    URLDecoder.decode(str,"UTF-8");

    URLEncoder.encode(str,"UTF-8");

     
    9.将Set集合转为List,这样获得的list并不能有序排列
    List<Topic> topicList = new ArrayList<Topic>(user.getTopics());
     
    10.将list有序排列 
    Collections.sort(topicList, new Comparator<Topic>() {  
       public int compare(Topic arg0, Topic arg1) {  
           return arg0.getTopicId().compareTo(arg1.getTopicId()); // 按照id排列  
       }  
    });  
    11.CDATA
    AND <![CDATA[ (ac.length>= #{start,jdbcType=INTEGER})  ]]>
    12.DecimalFormat 

    DecimalFormat df = new DecimalFormat("####.00");
    Double joinRate=new Double(df.format(temp*100));

    13.Spring异常

    <!-- 异常处理 -->
    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="defaultErrorView" value="errorPage/systemError"/> <!-- 默认为500,系统错误(error.jsp) -->
    <property name="defaultStatusCode" value="500"/>
    <!-- 配置多个statusCode -->
    <property name="statusCodes">
    <props>
    <prop key="500">500</prop>
    <prop key="404">404</prop>
    </props>
    </property>
    <property name="exceptionMappings">
    <props>
    <!-- 这里你可以根据需要定义N多个错误异常转发 -->
    <prop key="com.Exception">errorPage/systemError</prop>
    <prop key="com.alibaba.dubbo.rpc.RpcException">errorPage/netError</prop>
    </props>
    </property>
    </bean>

     
    public static void main(String[] args)throws Exception{
    File file = new File("F:\a.png");
    String a=file.separator;
    System.out.println(a);
    String fileName=file.getName();
    String prefix=fileName.substring(fileName.lastIndexOf("."));
    String datePag = new SimpleDateFormat("yyyyMMdd").format(new Date());
    String ctime = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
    System.out.println(ctime);
    System.out.println(prefix);
    String path="D:\"+ctime;
    String pathPag="D:\"+datePag;
    File data = new File(path);
    File dataPag = new File(pathPag);
    if(!dataPag.exists())
    dataPag.mkdirs();
    FileInputStream fis = new FileInputStream(file);
    byte[] b = new byte[1024];
    int len = 0;
    FileOutputStream fos = new FileOutputStream(pathPag+"\"+ctime+prefix);
    while((len=fis.read(b))!=-1){
    fos.write(b,0,len);
    }
    fos.close();
    fis.close();
    }
        
  • 相关阅读:
    从零开始学Bootstrap
    CSS VS JS动画,哪个更快
    css定位(positon)
    json
    使用 CSS3 绘制 Hello Kitty
    后台找到repeater里面的div并添加客户端点击事件
    dropDownList之"请选择",同时设置默认选项
    asp.net 后台对话框,确认跳转
    后台生成textbox并设置多行属性,自动换行
    asp.net多图片上传同时保存对每张图片的描述
  • 原文地址:https://www.cnblogs.com/jimmy-muyuan/p/5595341.html
Copyright © 2011-2022 走看看