我是靠谱客的博主 积极花瓣,最近开发中收集的这篇文章主要介绍java登录注册原理_案例:登录注册实现文件上传,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

案例概述

案例知识点分析

9b65f3c1b1b8e87a393afd0a059a8f72.png

39b164021c73ee539cc8592c26c84ff4.png

开发框架

1fcde9050a0e7bdb896f082cd0b35547.png

b8da16772ea55928e33027b4bc59f8fc.png

注册功能的实现流程

监听器初始化数据

实现表单提交

Servlet接收

Servlet封装

Servlet处理数据(解耦)

Servlet转发,JSP显示

文件上传功能的实现

文件上传要点

c20de6197fd22546f7a20431377bd1dc.png

文件上传原理

9eb34e43c12282f9c37fd4134ce25c70.png

文件上传参考案例

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);

}

}

登陆功能和验证码

登陆功能的实现

d56849b746e321fd99dfda647dce8066.png

验证码

验证码生成的流程

a08a2e46114d23bdeb2ba7c9c5842fd2.png

验证码参考案例

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);

}

}

项目实现展示

77c1687a64ac9d9895516ef4f421eb9b.png

691f40c8b44cdca853b79dd68abc2029.png

最后

以上就是积极花瓣为你收集整理的java登录注册原理_案例:登录注册实现文件上传的全部内容,希望文章能够帮你解决java登录注册原理_案例:登录注册实现文件上传所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(43)

评论列表共有 0 条评论

立即
投稿
返回
顶部