• Java 笔记


    过滤器的执行过程:
    // 目标资源执行前执行
    chain.doFilter(request, response);
    // 目标资源执行后执行

    文件上传的前提:
    1.表单的method方法必须是post
    2.表单的enctype类型必须是:multipart/form-data
    3.表单中input的上传输入域为:<input type="file"/>
    <form method="post" enctype="multipart/form-data>
    name:<input type="text" name="name"/>
    file:<input type="file" name="upLoad"/>
    <input type="submit" value="up"/>
    </form>
    form表单的enctype属性,该属性的作用是用来告知服务器,请求正文内容是MIME类型,相当于Content-type
    利用第三方组件实施文件上传,apache:commons-fileupload.jar,依赖:commons-io.jar
    代码:
    // 检查form的enctype是否是multipart类型
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if(!isMultipart)
    throw new RuntimeException("文件类型不符")
    // 解析请求内容,磁盘文件条目工厂
    DiskFileItemFactory factory = new DiskFileFactory();// 产生FileItem的工厂
    ServletFileUpload sfu = new ServletFileUpload(factory);
    // 建立一个数据结构,用于存储FileItem的数据
    List<FileItem> items = new ArrayList<FileItem>;
    try{
    // 对请求进行解析,吧解析的内容放入到items里面
    items = sfu.parseRequest(request);
    }catch(FileUploadException e){
    throw new RuntimeException("failed");
    }
    // 对items的数据进行遍历取出
    for(FileItem item:items){
    // 普通字段
    if(item.isFormField()){
    processFormField(item);
    }else{
    processUploadField(item);
    }
    }
    上传中考虑的几个问题
    1.将文件放到用户访问不到的地方(WEB-INF目录下)
    2.重复名字文件被覆盖的问题,把文件名做成唯一,UUID:通用唯一标识码。a.txt--->UUID-a.txt
    3.避免一个文件夹中的文件过多
    1)按照日期分目录存储
    2)按照文件名的hashCode随机生成目录
    int hashCode = fileName.hashCode();
    int dir1 = hashCode&0xf;
    4.文件大小的限制
    web文件上传的内容不宜过大,就是要对工厂的容量进行限制
    1)限制单个文件的大小
    sfu.setFileSizeMax(3*1024*1024);
    2)限制整个文件的大小
    sfu.setFileMax(5*1024*1024);
    try{
    items = sfu.parseRequest(request);
    }catch(FileUploadBase.FileSizeLimitExceededException e){
    response.getWriter().write("must small 3M");
    }catch(FileUploadBase.SizeLimitExceededException e){
    response.getWriter().write("sum must small 5M");
    }
    ServketContextListener
    ServletContextListener
    HttpSessionListener
    HttpSessionListener
    ServletRequestListener
    ServletRequestListener
    ServletContextAttributeListener
    ServletContextAttributeListener
    HttpSessionAttributeListener
    HttpSessionAttributeListener
    ServletRequestAttributeListener
    HttpSessionBindingListener
    HttpSessionActivationListener
    AJAX引擎:XMLHttpRequest
    JSON:js对象标记,js Object Notation

    List<String> list = new List<String>;
    list.add("a");
    list.add("b");
    list.add("c");
    JSONArray json = JSONArray.fromObject(list);

    public class CreateDeng{
    // 泛型使用之前必须声明
    // <T>就是声明泛型类型,放在返回值的前面
    public <T> T v1(){
    return null;
    }
    public <T> void m2(T t){
    }
    public <T> void m3(class<T> t){
    }
    }
    // 类上声明泛型,实例方法中就可以使用了
    public class CreateDeng<T>{
    public T m1(){
    return null;
    }
    public void m2(T t){
    }
    public void m3(class<T> t){
    }
    // 对于静态方法必须都是先声明后使用
    public static <T> void m4(class<T> t){
    }
    public static <K,V> K m5(V v){}
    }
    hibernate.cf.xml
    <hibernate-configuration>
    <session-factory>
    <!--JDBC基本链接-->
    <properties name="hibernate.connection.driver_class>com.mysql.jdbc.Driver</properties>
    <properties name="connection.username">root</properties>
    <properties name="connection.password">sorry</properties>
    <properties name="connection.url">jdbc:mysql:localhost:3306/day22</properties>
    <!--配置数据库方言-->
    <properties name="dialect">com.hibernate.dialect.MySQLDialect</properties>
    <properties name="hibernate.hbm2ddl.auto">update</properties>
    <!--显示sql的语句及格式>
    <properties name="hibernate.show_sql">true</properties>
    <properties name="hibernate.format_sql">true</properties>
    <!--告知映射文件>
    <mapping resource="../Student.hbm.xml"/>

    </session-factory>
    </hibernate-configuration>

    Student.hbm.xml
    <hibernate-mapping>
    <class name="Student" table="STUDENTS">
    <id name="id" column="ID">
    <!--根据数据库的能力管理主健-->
    <generate class="native"></generate>
    </id>
    <properties name="name" column="NAME"></properties>
    <properties name="birthday" column="BIRTHDAY"></properties>
    </class>
    </hibernate-mapping>
    Dao中的文件总结
    Dao.java
    public Dao<T>{
    void add(T t);
    void update(T t);
    // 根据主健查找对象
    T findOne();
    // 根据主健删除对象
    void delete(Serializable id)
    }
    CustomerDao.java
    public interface CustomerDao extends Dao<Customer>{
    List<Customer> findPageCustomer(int startIndex, int size);
    }
    页面展现,数据传输,数据库,MVC框架模型,数据库要使用数据库连接池DBCP
  • 相关阅读:
    CSS 之 @media
    How to fix “Duplicate sources.list entry …” issue
    shell脚本加不加export的区别
    过滤部分错误信息,不输出到stderr
    /dev/null 2>&1 解释(转)
    crontab与环境变量
    PHP实现斐波那契数列非递归方法
    有反斜杠时候,CakePHP往pgsql插入数据异常
    PHP输出图片文件,实现浏览器缓存机制
    sudo: unable to resolve host XXX 解决方法
  • 原文地址:https://www.cnblogs.com/demo-deng/p/8145621.html
Copyright © 2020-2023  润新知