zoukankan      html  css  js  c++  java
  • 文件上传分析

    文件上传功能使用的很普遍,比如项目中附件的上传、微博或者招聘网站头像的上传...

    文件上传的表单要求:

    1.enctype="multipart/form-data";

    2.表单中需要有file类型的input;

    3.表单的请求方式一定是POST;

    Servlet要求:

    1.不能使用request.getParameter,使用request.getInputStream。

    案例如下

      form表单:

    1 <form action="register" method="post" enctype="multipart/form-data">
    2           用户:<input type="text" name="username" /><br/>
    3           密码:<input type="password" name="password" /><br/>
    4           照片:<input type="file" name="pic" /><br/>
    5           <input type="submit" value="提 交" />
    6 </form>

      servlet:

     1         //如果是复杂类型的表单,getParameter全部失效,取而代之的是getInputStream
     2         String username = request.getParameter("username");
     3         String password = request.getParameter("password");
     4         System.out.println(username);
     5         System.out.println(password);
     6         //获得复杂类型的表单
     7         InputStream in = request.getInputStream();
     8         //或的输入流的内容
     9         String result = IOUtils.toString(in);
    10         System.out.println(result);

      

  • 相关阅读:
    mybatis和spring整合
    Freemarker教程1(基本使用)
    mybatis教程6(逆向工程)
    mybatis教程4(动态SQL)
    mybatis教程5(延迟加载和缓存)
    mybatis教程2(配置文件)
    python作用域
    软件测试基础面试题
    http协议
    selenium自动化测试
  • 原文地址:https://www.cnblogs.com/ShawnYang/p/7644532.html
Copyright © 2011-2022 走看看