Request 内容实体body的简单例子 PART3
来源:互联网 发布:边际效应 知乎 编辑:程序博客网 时间:2024/06/04 23:26
1.访问路径http://localhost:8080/servlet-demo/submit.jsp
2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><form action="myRequestServlet3" method="post" enctype="multipart/form-data"><input name="userName" type="text" value="张三"/><input name="age" type="text" value="age"/><br/><input name="myfile1" type="file" value="上传"/><br/><input name="myfile2" type="file" value="上传"><br/><input name="sub" type="submit" value="提交"></form></body></html>
3.servlet
package com.zghw.servlet.demo;import java.io.IOException;import java.util.Collection;import javax.servlet.ServletException;import javax.servlet.annotation.MultipartConfig;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.Part;/** * 关于请求体body的处理 一般是通过post方式传递过来的,比如表单数据 或者是上传的资源 body体中的数据时二进制格式 上传文件需要设置@MultipartConfig * 或者在servlet设置可以使用Part * */// 设置上传目标文件夹地址@MultipartConfig(location = "/home/zghw/testdoc/")@WebServlet("/myRequestServlet3")public class MyRequestServlet3 extends HttpServlet {static void f(Object obj) {System.out.println(obj);}private static final long serialVersionUID = 1L;protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {// 设置编码格式request.setCharacterEncoding("UTF-8");// 得到body体的内容长度单位字节int contentLength = request.getContentLength();f(contentLength);// 3.1版本支持,现在用的3.0// long contentLengthLong = request.getContentLengthLong();// 内容体body的类型,比如表单的内容体类型为:application/x-www-form-urlencodedString contentType = request.getContentType();// 当类型是multipart/form-data (form表单中属性值enctype="multipart/form-data")时,// body体的内容就和默认的application/x-www-form-urlencoded 不一样了f(contentType);// getInputStream()和getReader()不能同时调用// 就像一个数据管道一样,当作为字节流打开后就不能同时使用字符流打开了/* * ServletInputStream sis = request.getInputStream(); //字节流输出body中的内容 * byte[] b=new byte[1024]; int off=0; int len=b.length; * while(sis.readLine(b, off, len)>-1){ //进行解码输出 * System.out.println(URLDecoder.decode(new String(b),"UTF-8")); } */// 直接使用字符流读取信息/* * BufferedReader reader = request.getReader(); String input=null; * while((input=reader.readLine())!=null){ System.out.println(input); } */// 文件上传的处理,也包含了文本框所以要和文本框分开的话就不再一个form中写// 取得上传的文件集合Collection<Part> partColl = request.getParts();if (partColl != null && !partColl.isEmpty()) {for (Part part : partColl) {// 得到上传的文件流// InputStream is=part.getInputStream();// 得到文件名f("name=" + part.getName());// 得到提交过来的文件名 servlet版本3.1才能使用// f("submittedFileName=" + part.getSubmittedFileName());// 文件大小f("size=" + part.getSize());// 文件类型f("content-type=" + part.getContentType());for (String head : part.getHeaderNames()) {f(head + "=" + part.getHeader(head));}part.write(part.getName());}}}protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}}
0 0
- Request 内容实体body的简单例子 PART3
- beego 获取 Request Body 里的内容
- request请求数据包组成:请求行(request line)消息头(header)实体内容(Body)
- 解决filter拦截request中body内容后,字符流关闭,无法传到controller的问题
- body下面的内容居中
- Request body二次取得的问题
- 【MapReduce】超大集群的简单数据处理 part3
- request.getQueryString的例子
- body里面简单的css
- 得到页面body区域内容的高度
- ACIS 创建只有一个点的实体(BODY)
- urllib2-addheaders(在 HTTP Request 中加入特定的 Header,Content-Type用 REST 接口时,服务器会检查该值,用来确定 HTTP Body 中的内容)
- Fiddler的实践心得(四):修改request的body
- C#显示(读取)XML元素内容的简单例子
- 一个htmlparser提取网页内容的简单例子
- Java爬取网页内容的简单例子
- CUDA的SDK里的N-body例子疑问
- AFNetWorking post body内容
- 南京理工大学第八届程序设计大赛题解
- spring framework源码下载并导入eclipse
- mybatis sqlsession
- 机器学习资料收集(更新中)
- k近邻算法
- Request 内容实体body的简单例子 PART3
- 第七届科技节微电影比赛须知
- Python的__slots__
- leetcode 27. Remove Element-数组去重|双指针
- 第七届科技节图像后期处理大赛须知
- VMvare版本10.0.7, Ubuntu 版本1404下安装vmtools 以后共享hgfs下为空的解决方案
- js中继承的几种用法总结(apply,call,prototype)
- CAEmitterLayers
- 进程—初印象