概述
案例概述
案例知识点分析
开发框架
注册功能的实现流程
监听器初始化数据
实现表单提交
Servlet接收
Servlet封装
Servlet处理数据(解耦)
Servlet转发,JSP显示
文件上传功能的实现
文件上传要点
文件上传原理
文件上传参考案例
pageEncoding="UTF-8"%>
Insert title here文件上传
package com.jesse.servlet;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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;
@WebServlet("/uploadservlet")
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1.创建磁盘文件项工厂
DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
// 2.创建核心解析类
ServletFileUpload fileUpload = new ServletFileUpload(diskFileItemFactory);
// 3.解析请求对象,将请求分成几个部分(FileItem)
try {
List list = fileUpload.parseRequest(request);
// 4. 遍历集合获得每个部分的对象
for(FileItem fileItem:list) {
// 判断是普通项还是文件上传项
if(fileItem.isFormField()) {
// 普通项
//获得普通项的名称:
String name = fileItem.getFieldName();
//获得普通项的值:
String value = fileItem.getString("UTF-8");
System.out.println(name + "-" + value);
}else {
// 文件上传项
// 获得文件的名称:
String fileName = fileItem.getName();
// 获得文件的输入流:
InputStream is = fileItem.getInputStream();
// 需要将文件写入到服务器的某个路径即可:
String path = getServletContext().getRealPath("/upload");
System.out.println(path);
// 创建输出流与输入流进行对接
OutputStream os = new FileOutputStream(path+ "\" + fileName);
int len = 0;
byte[] b = new byte[1024];
while((len = is.read(b)) != -1) {
os.write(b, 0, len);
}
is.close();
os.close();
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
登陆功能和验证码
登陆功能的实现
验证码
验证码生成的流程
验证码参考案例
package com.jesse.web.servlet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.prism.Image;
@WebServlet("/check1")
public class CheckImgServlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int width = 120;
int height = 30;
// 步骤一:在内存中生成一张图片
BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
// 步骤二:操作该图片,设置背景色及绘制边框
Graphics graphics = bufferedImage.getGraphics();
// 设置背景色
graphics.setColor(Color.YELLOW);
graphics.fillRect(0, 0, width, height);
// 绘制边框
graphics.setColor(Color.BLUE);
graphics.drawRect(0, 0, width-1, height-1);
// 步骤三:生成随机的四个字母,写入到图片中
Graphics2D g2d = (Graphics2D)graphics;
g2d.setColor(Color.BLACK);
g2d.setFont(new Font("宋体", Font.BOLD, 18)); // a 4 3 r
String words = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
Random random = new Random();
int x = 10;
for(int i = 0; i < 4; i++) {
int idx = random.nextInt(words.length());
// 获得指定位置的字符
char ch = words.charAt(idx);
// 旋转的角度 -30到30之间
int angle = random.nextInt(60) - 30;
// 将角度转化为弧度:
double theta = angle * Math.PI / 180;
g2d.rotate(theta, x, 20);
g2d.drawString(String.valueOf(ch), x, 20);
g2d.rotate(-theta, x, 20);
x += 30;
}
// 步骤四:将内存中的图片,进行输出
ImageIO.write(bufferedImage, "jpg", response.getOutputStream());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
项目实现展示
最后
以上就是积极花瓣为你收集整理的java登录注册原理_案例:登录注册实现文件上传的全部内容,希望文章能够帮你解决java登录注册原理_案例:登录注册实现文件上传所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复