zoukankan      html  css  js  c++  java
  • Http协议相关内容

    1纯文本的表单:

    1:get请求,通过url明文传递参数,不安全而且参数大小有限制。在servlet中可以使用如下代码获取参数字符串:

        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            String queryString = request.getQueryString();
            System.out.println(queryString);
        }

    打印内容:

    name=GET_NAME&password=GET_PASSWORD //表单只有两个属性

    2:post请求,对于上传数据必须使用post请求,理论上讲,POST是没有大小限制的。HTTP协议规范也没有进行大小限制,起限制作用的是服务器的处理程序的处理能力。post请求如下方法获取请求参数:

        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
    
            // 1. 获取表单数据流
            InputStream in = request.getInputStream();
            // 2. 转换流
            InputStreamReader inStream = new InputStreamReader(in, "UTF-8");
            // 3. 缓冲流
            BufferedReader reader = new BufferedReader(inStream);
            // 输出数据
            String str = null;
            while ((str = reader.readLine()) != null) {
                System.out.println(str);
            }
            // 关闭
            reader.close();
            inStream.close();
            in.close();
    
        }

    同一个表单打印数据如下:

    name=POST_NAME&password=POST_PASSWORD

    2上传数据的表单:

    必须使用post方法,而且form必须设置enctype="multipart/form-data"属性,例如:

        <div align="center">
            <form action="fileLoad" method="post" enctype="multipart/form-data">
                Name:<input name="name" type="text" /> <br>
                Password:<input type="file" name="uploadfile" /><br>
                <button type="submit">发送</button>
            </form>
        </div>

    表单中enctype="multipart/form-data"的意思,是设置表单的MIME编码。默认情况,这个编码格式是application/x-www-form-urlencoded,不能用于文件上传;只有使用了multipart/form-data,才能完整的传递文件数据。

    此时如果后台还是按照前面post获取数据的方式直接使用流获取数据的话,你会发现上传的文件错误,错误示例代码:

        /**
         * 上传文件损坏
         * @param request
         * @param response
         * @throws Exception
         */
        public static void uploadError(HttpServletRequest request, HttpServletResponse response) throws Exception {
            // 上传错误:没能过滤掉一些控制信息
            InputStream in = request.getInputStream(); //这个流是所有的表单字段数据和文件数据的总和
            byte[] b = new byte[1024];
            FileOutputStream out = new FileOutputStream(
                    new File("servlet-fileupload.avi"));
            int bytesRead = 0;
            while ((bytesRead = in.read(b)) > 0) { //此处我们将所有数据都认为是上传数据,所以导致上传文件损坏
                out.write(b, 0, bytesRead);
                out.flush();
            }
            out.flush();
            out.close();
            in.close();
            System.out.println("完毕!");
    
        }

    切记:以上是错误示例!!!

    下面是一个正确的示例:

        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            //判断是一个多媒体内容
            if (ServletFileUpload.isMultipartContent(request)) {
                try {
                    DiskFileItemFactory factory = new DiskFileItemFactory();
                    factory.setSizeThreshold(MEMORY_THRESHOLD);
                    ServletFileUpload upload = new ServletFileUpload(factory);
                    // 设置最大文件上传值
                    upload.setFileSizeMax(MAX_FILE_SIZE);
                    List<FileItem> items = upload.parseRequest(request);
                    
                    for (int i = 0, size = items.size(); i < size; i++) {
                        FileItem item = items.get(i);
                        if (!item.isFormField()) {// isFormField: true表示是表单的一个属性,false表示一个上传文件
                            File file = new File("upload-daxin.flv");//文件名可以使用上传文件的实际名称,示例就略过了
                            item.write(file);
                        }
                    }
    
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    
        }

    写的相对简单,总要分享的是思路。多多包涵!

  • 相关阅读:
    2016.01.04接触spring一年开始读spring源码
    hibernate 各历史版本下载 spring各历史版本下载
    mongodb 安装使用遇到的问题记录
    EmEditor处理大文本文件
    linux的常用易忘命令
    签名的html
    添加用户-查看用户列表-禁止默认root登陆
    今天领导分享了一个探测端口的命令-linux下提示bash:command not found
    【原创】java 获取十个工作日之前或之后的日期(算当天)-完美解决-费元星
    Oracle 完全理解connect by-详细脚本-可实战
  • 原文地址:https://www.cnblogs.com/leodaxin/p/7509889.html
Copyright © 2011-2022 走看看