Java doGet, doPost方法和文件上传实例代码
作者:lqh 时间:2023-01-19 03:24:19
Java doGet, doPost方法和文件上传
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/test" method="post" enctype="multipart/form-data">
<table >
<tr>
<td>
用户名:
</td>
<td>
<input name="username" type="text">
</td> <tr>
<td>
密  码:
</td>
<td colspan="2">
<input name="password" type="password">
</td>
</tr>
<tr align="center">
<td><input name="login" type="submit" value="登陆"></td>
<td><input name="register" type="reset" value="重置"></td>
</tr>
<tr>
<td colspan="2">
<input type="radio" name="sex" value="male">男
</td> <td colspan="2">
<input type="radio" name="sex" value="female">女
</td>
</tr>
<tr>
<td colspan="2" >
<input name="upload" type="file">
</td>
</tr>
</table>
</form>
</body>
</html>
TestServlet.Java
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.List;
/**
* Project: Day15_Web
* Created: Lulu
* Date: 2016/8/16
*/
@WebServlet(name = "TestServlet")
public class TestServlet extends HttpServlet {
private File root_upload;
public TestServlet() {
root_upload = new File("C:/temp");
if (!root_upload.exists()) {
root_upload.mkdirs();
}
}
//两种链接方式 doPost doGet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
ServletOutputStream os = response.getOutputStream();
StringBuilder builder = new StringBuilder();
builder.append("<html>");
builder.append("<head>");
builder.append(" <meta charset=\"utf-8\">");
builder.append(" <title>测试Servlet</title>");
builder.append("</head>");
builder.append("<body>");
Enumeration<String> names = request.getHeaderNames();
while (names.hasMoreElements()) {
String name = names.nextElement();
builder.append(name)
.append(":")
.append(request.getHeader(name))
.append("<br>");
}
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(10);//设置未保存在临时路径的最大值
factory.setRepository(new File("C:/temp/temp"));//超过上述最大值后保存的临时路径
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List<FileItem> list = upload.parseRequest(request);//上传的文件列表
for (FileItem item : list) {
builder.append(item.getFieldName())
.append(":")
.append(item.isFormField() ? item.getString() : item.getName())//判断是不是属性, 如果是属性, 就
.append("<br>");
if (!item.isFormField()) {
File file = new File(root_upload, item.getName());
builder.append(file.getAbsolutePath()).append("<br>");
item.write(file);
}
}
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
//可以从输入流中读取数据
// ServletInputStream is = request.getInputStream();
// byte[] buffer = new byte[8192];
// int len ;
// ByteArrayOutputStream bos= new ByteArrayOutputStream();
// while ((len = is.read(buffer)) != -1) {
// bos.write(buffer, 0, len);
// }
// builder.append("InputStream: ")
// .append(bos.toString("UTF-8"))
// .append("<br>");
// builder.append("<h1>Post的请求参数</h1>").append("<br/>");
//
// Enumeration<String> parameterNames = request.getParameterNames();
// while (parameterNames.hasMoreElements()) {
// String name = parameterNames.nextElement();
// builder.append(name)
// .append(":")
// .append(request.getParameter(name))
// .append("<br/>");
// }
//接收来自浏览器的文件,
os.write(builder.toString().getBytes("UTF-8"));
//参数列表: 待转码的字符串, 字符编码
URLEncoder.encode("&", "UTF-8");
URLDecoder.decode("%26", "UTF-8");
builder.append("</body>");
builder.append("</html>");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>org.lulu.learn.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
</web-app>
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
标签:Java,doGet,doPost方法
0
投稿
猜你喜欢
java实现小i机器人api接口调用示例
2023-02-05 00:09:15
C#微信开发之启用开发者模式
2022-07-07 11:24:54
Java中的访问修饰符详细解析
2022-01-18 17:23:02
springboot之端口设置和contextpath的配置方式
2023-10-05 14:16:20
Monaco Editor实现sql和java代码提示实现示例
2023-01-31 16:59:48
java后台接受到图片后保存方法
2023-06-03 09:23:04
java 同步器SynchronousQueue详解及实例
2023-04-29 00:41:07
Android中文件的压缩和解压缩实例代码
2022-06-25 12:06:29
Android EasyPlayer声音自动停止、恢复,一键静音等功能
2023-01-12 21:16:48
Java调用groovy实现原理代码实例
2023-05-16 16:41:51
Spring中Bean扫描原理详情
2022-05-26 04:33:14
idea设置JVM运行参数的几种方式
2023-05-26 14:57:33
BeanUtils.copyProperties在拷贝属性时忽略空值的操作
2022-07-11 05:22:04
Java Hutool工具实现验证码生成及Excel文件的导入和导出
2023-02-04 22:49:32
java实现模拟进度计量器
2023-05-26 12:05:03
springmvc请求转发和重定向问题(携带参数和不携带参数)
2022-09-17 13:53:59
一看就懂:图解C#中的值类型、引用类型、栈、堆、ref、out
2022-06-08 16:36:41
android使用Socket通信实现多人聊天应用
2023-07-08 02:43:37
浅谈Java自定义类加载器及JVM自带的类加载器之间的交互关系
2021-09-12 23:37:24
Unity实现简单虚拟摇杆
2023-08-04 17:33:09