`
king520
  • 浏览: 167399 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
文章分类
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
javamail 使用Spring JavaMail发送邮件总结
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    <!-- 注意:这里的参数(如用户名、密码)都是针对邮件发送者的 -->
    <bean id="mailSender"
  class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host">
            <value>stmp.163.com</value>
        </property>
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.smtp.timeout">25000</prop>
            </props>
        </property>
        <property name="username">
            <value>xxxx@163.com</value>
        </property>
        <property name="password">
            <value>xxxxxx</value>
        </property>
    </bean>
</beans>
文件下载(只需要简单的四步),Java中都通用 文件下载(只需要简单的四步),java中都通用 文件下载(只需要简单的四步),Java中都通用
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@Controller
public class FileController implements ServletContextAware{
    //Spring这里是通过实现ServletContextAware接口来注入ServletContext对象
    private ServletContext servletContext;


    @RequestMapping("file/download")
    public void fileDownload(HttpServletResponse response){
        //获取网站部署路径(通过ServletContext对象),用于确定下载文件位置,从而实现下载
        String path = servletContext.getRealPath("/");

        //1.设置文件ContentType类型,这样设置,会自动判断下载文件类型
        response.setContentType("multipart/form-data");
        //2.设置文件头:最后一个参数是设置下载文件名(假如我们叫a.pdf)
        response.setHeader("Content-Disposition", "attachment;fileName="+"a.pdf");
        ServletOutputStream out;
        //通过文件路径获得File对象(假如此路径中有一个download.pdf文件)
        File file = new File(path + "upload/" + "download.pdf");

        try {
            FileInputStream inputStream = new FileInputStream(file);

            //3.通过response获取ServletOutputStream对象(out)
            out = response.getOutputStream();

            int b = 0;
            byte[] buffer = new byte[512];
            while (b != -1){
                b = inputStream.read(buffer);
                //4.写到输出流(out)中
                out.write(buffer,0,b);
            }
            inputStream.close();
            out.close();
            out.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }
}
sqlite 中判断某个表是否存在的方法 Android sqlite中判断某个表是否存在方法
/**
     * 判断某张表是否存在
     * @param tabName 表名
     * @return
     */
    public boolean tabbleIsExist(String tableName){
            boolean result = false;
            if(tableName == null){
                    return false;
            }
            SQLiteDatabase db = null;
            Cursor cursor = null;
            try {
                    db = this.getReadableDatabase();
                    String sql = "select count(*) as c from Sqlite_master  where type ='table' and name ='"+tableName.trim()+"' ";
                    cursor = db.rawQuery(sql, null);
                    if(cursor.moveToNext()){
                            int count = cursor.getInt(0);
                            if(count>0){
                                    result = true;
                            }
                    }
                    
            } catch (Exception e) {
                    // TODO: handle exception
            }                
            return result;
    }
Global site tag (gtag.js) - Google Analytics