/images/avatar.jpg

雨临Lewis的博客

js、css外部文件的相对路径问题

如果js、css外部文件有使用到相对路径时,需要注意其相对路径的基准是不一样的。

比如说,在index.html中引用到了外部的js和css文件,这两个文件都通过相对路径引用了某一张图片;这些文件所在的目录如下:

1
2
3
4
5
6
7
8
.
├── js
|   └── index.js
├── css
|   └── index.css
├── images
|   └── bg.jpg
└── index.html

Hexo系列(1) - 简单搭建教程与远程部署

前言

搭建个人博客一般有两种选择,一个是使用WordPress,但是需要将博客搭建在服务器上,不过搭建好后写文章方便,适合没有程序基础的人使用。另一个是使用Hexo,相对简洁高效,不需要服务器,既可以部署在本地,也可以将博客部署到GitHub Pages上,支持Markdown语法,缺点是需要有Git基础,写文章比WordPress麻烦点。

初次使用Hexo来搭建个人博客,确实比较手忙脚乱,这里记录一下流程,希望对大家能有所帮助。

影评汇总

2018-03-31 《Ready Player One》

/images/posts/film/ReadyPlayerOne.jpg
ReadyPlayerOne

这部电影国内译名又叫头号玩家、玩家一号,讲述的是在未来人们为了躲避一团糟的现实而沉迷于一个名为绿洲的现实虚拟游戏。

特殊的空格-ASCII码值160

问题与分析

最近遇到个问题,在页面的搜索框输入带有空格的字符串时,总是无法从db中搜索出来对应的数据,于是将db里的空格复制出来,发现其ASCII码值为160,这才知道,原来ASCII码中除了32之外还有160这个特殊的空格。下边是查看字符对应ASCII值的逻辑:

1
2
3
4
final char c1 = ' '; //db里的空格
final char c2 = ' '; //手动输入的空格
System.out.println((int)c1); //160
System.out.println((int)c2); //32

PostgreSQL - 开窗函数汇总

和聚合函数的区别

开窗函数,也叫窗口函数,一般可以用开窗函数来做一些排行之类的统计操作。开窗函数必须搭配over()子句作为查询的条件,否则会报错。over()子句可以用partition by进行分组,以及用order by排序。

聚合函数是将多条记录聚合为一条;而开窗函数是每条记录都会执行,有几条记录执行完还是几条,聚合函数也可以用于开窗函数中。

JavaMail-发送一封简单邮件(附带附件)

代码实现

最近使用到JavaMail,写了个简单的工具类,记录一下。

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import java.util.Date;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import javax.mail.internet.MimeMultipart;

public class JavaMailUtils {
	//连接SMTP服务器的主机名,这里以qq邮箱为例
    private static final String SMTP_HOST = "smtp.qq.com";
    //连接的端口号,587为ssl端口,默认为25端口
    private static final String SMTP_PORT = "587";
    //发件人
    private static final String MAIL_FROM = "XXX@qq.com";
    //连接邮件服务器的用户名(邮箱地址去除了@qq.com的部分)
    private static final String USER = "XXX";
    /**
     * 授权码,就是你在邮件服务器上注册的密码,不是你的qq密码
     * 在邮箱里开启smtp/imap服务时需要发送短信,成功后会得到一个授权码
     */
    private static final String PASSWORD = "授权码";

    private JavaMailUtils() {
    }

    /**
     * send mail without attachments
     *
     * @param mailTo
     * @param mailCc
     * @param subject
     * @param content
     * @throws AddressException
     * @throws MessagingException
     */
    public static void sendMail(final String mailTo, final String mailCc, final String subject, final String content)
            throws Exception {
        // get the session
        final Session session = getSession();
        // create a mail
        final MimeMessage message = createMail(session, mailTo, mailCc, subject, content);
        // get the transport
        final Transport transport = session.getTransport();
        transport.connect(USER, PASSWORD);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }

    public static void sendMail(final String mailTo, final String mailCc, final String subject, final String content,
            final String[] attachments) throws Exception {
        // get the session
        final Session session = getSession();
        // create a mail
        final MimeMessage message = createMail(session, mailTo, mailCc, subject, content, attachments);
        // get the transport
        final Transport transport = session.getTransport();
        transport.connect(USER, PASSWORD);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }

    /**
     * create a mail without attachments
     *
     * @param session
     * @param mailTo
     * @param mailCc
     * @param subject
     * @param content
     * @return
     * @throws Exception
     */
    private static MimeMessage createMail(final Session session, final String mailTo, final String mailCc,
            final String subject, final String content) throws Exception {
        final MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(MAIL_FROM));
        message.setRecipients(RecipientType.TO, InternetAddress.parse(mailTo));
        message.setRecipients(RecipientType.CC, InternetAddress.parse(mailCc));
        message.setSubject(subject);
        message.setContent(content, "text/html;charset=UTF-8");
        message.setSentDate(new Date());
        message.saveChanges();

        return message;
    }

    /**
     * create a mail with attachments
     *
     * @param session
     * @param mailTo
     * @param mailCc
     * @param subject
     * @param content
     * @param attachments
     * @return
     */
    private static MimeMessage createMail(final Session session, final String mailTo, final String mailCc,
            final String subject, final String content, final String[] attachments) throws Exception {
        final MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(MAIL_FROM));
        message.setRecipients(RecipientType.TO, InternetAddress.parse(mailTo));
        message.setRecipients(RecipientType.CC, InternetAddress.parse(mailCc));
        message.setSubject(subject);
        // set the multipart of the mail
        final MimeMultipart multipart = new MimeMultipart();
        // set content part of the mail
        final MimeBodyPart contentPart = new MimeBodyPart();
        contentPart.setContent(content, "text/html;charset=UTF-8");
        multipart.addBodyPart(contentPart);
        // set attachment part of the mail
        final MimeBodyPart attach1 = new MimeBodyPart();
        final MimeBodyPart attach2 = new MimeBodyPart();
        if (attachments != null && attachments.length != 0) {
            final int length = attachments.length;
            if (length == 1) {
                attach1.attachFile(attachments[0]);
                multipart.addBodyPart(attach1);
            }
            if (length == 2) {
                attach1.attachFile(attachments[0]);
                attach2.attachFile(attachments[1]);
                multipart.addBodyPart(attach1);
                multipart.addBodyPart(attach2);
            }
        }
        message.setContent(multipart);
        message.setSentDate(new Date());
        message.saveChanges();

        return message;
    }

    /**
     * get the session to transport mails
     */
    private static Session getSession() {
        // set the properties for connecting the mail server
        final Properties props = new Properties();
        props.setProperty("mail.debug", "true");
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.host", SMTP_HOST);
        props.setProperty("mail.smtp.auth", "true");
        props.setProperty("mail.smtp.port", SMTP_PORT);
        // ssl
        props.setProperty("mail.imap.ssl.enable", "true");
        props.setProperty("mail.imap.ssl.socketFactory.class", "DummySSLSocketFactory");
        props.setProperty("mail.imap.ssl.socketFactory.fallback", "false");
        // create the session
        final Session session = Session.getDefaultInstance(props);

        return session;
    }

}