Request 内容实体body的简单例子 PART3

本篇博客通过一个简单的例子,展示了如何使用HTTP请求访问特定路径,并详细讲解了在JSP和Servlet中处理请求内容实体body的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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-urlencoded
		String 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);
	}

}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值