/images/avatar.jpg

雨临Lewis的博客

特殊的空格-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;
    }

}

cmd - bat文件如何关闭回显

echo命令

bat文件是dos下的批处理文件,可以一次性执行多条dos命令,其扩展名为.bat或.cmd,通过双击该文件或者在cmd窗口中输入该文件名可以在cmd窗口中按文件中的顺序执行多条dos命令。

echo是回显命令,会将echo后的内容输出到cmd窗口中,比如在一个Test.bat文件中输入如下命令:

MySQL - 8小时连接闲置超时

问题与分析

最近发现之前部署在阿里云的一个web项目,每过一段时间就会报错,但是刷新下页面就会显示正常;在过了比较长的一段时间后,又会报同样的错误,如下:

Java高并发秒杀API(四)之高并发优化

1. 高并发优化分析

关于并发

并发性上不去是因为当多个线程同时访问一行数据时,产生了事务,因此产生写锁,每当一个获取了事务的线程把锁释放,另一个排队线程才能拿到写锁,QPS(Query Per Second每秒查询率)和事务执行的时间有密切关系,事务执行时间越短,并发性越高,这也是要将费时的I/O操作移出事务的原因。