11_download

1 下载概念

一个超链接  客户端点击 就把服务器端提供的资源 下载到客户端的硬盘中

2 下载对jsp的要求

提供一个超链接 指定要下载的文件的名字即可
<a   href="<c:url value='/imgs/1.jpeg'/>">下载1.jpeg</a><br/>
<a   href="<c:url value='/imgs/1.png'/>">下载1.png</a><br/>
<a   href="<c:url value='/imgs/3.jpeg'/>">下载3.jpeg</a><br/>
<a   href="<c:url value='/imgs/4.jpeg'/>">下载4.jpeg</a><br/>
<a   href="<c:url value='/imgs/5.jpeg'/>">下载5.jpeg</a><br/>
<a   href="<c:url value='/imgs/6.jpeg'/>">下载6.jpeg</a><br/>
<a   href="<c:url value='/imgs/7.jpeg'/>">下载7.jpeg</a><br/>
<a   href="<c:url value='/imgs/8.jpeg'/>">下载8.jpeg</a><br/>
<a   href="<c:url value='/imgs/呵呵呵.jpeg'/>">下载呵呵呵.jpeg</a><br/>

3 对servlet的要求

提供response的输出流把服务器端目的资源的信息 传递给客户端::注意不是响应给客户端的页面  而是以附件的形式让客户端下载
//获取请求参数?fileName
req.setCharacterEncoding("UTF-8");
String fileName=req.getParameter("fileName");
//获取文件的大类型
String type=req.getServletContext().getMimeType(fileName);
//设置两个响应头之一Content-Type 指定响应的文件的类型:
resp.setHeader("Content-Type", type);
//设置两个响应头之一Content-Disposition 指定响应的文件以附件形式下载:
resp.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode("下载"+fileName, "utf-8"));

//获取imgs的真实路径
String path=req.getServletContext().getRealPath("/imgs/"+fileName);
//使用流读取源文件 通过response的输出流写出去
FileInputStream fin=new FileInputStream(path);
OutputStream out=resp.getOutputStream();
IOUtils.copy(fin, out);
fin.close();

4 java验证码

java画图:属于GUI:
String str="123456789一二三四区六七八九十男女老少上下多少春夏秋冬东西南北张王李赵";
//通过java生成一个验证码:步骤和画画完全相同
//1 准备画布
BufferedImage bin=new BufferedImage(200, 40, BufferedImage.TYPE_INT_RGB);//在内存中开辟空间画画
//2 准备画笔
Graphics2D g=bin.createGraphics();
//3 蘸墨水 画背景
g.setColor(Color.WHITE);
g.fillRect(1, 1, 198, 38);
//4 蘸墨水 写字符
String yzm="";
for (int i = 0; i < 4; i++) {
    yzm+=str.charAt((int)(Math.random()*str.length()));
}
//设置字体
g.setFont(new Font(null, Font.BOLD, 30));//第一个参数字体类型 第二个参数加粗/斜体 第三个参数字体大小
for (int i = 0; i < yzm.length(); i++) {
    //每个字符不同的颜色
    g.setColor(new Color(100+(int)(Math.random()*150), 50+(int)(Math.random()*200), 150+(int)(Math.random()*100)));
    //画字符
    g.drawString(yzm.charAt(i)+"", 10+i*45, 34);
}
//5 画布扯下来
FileOutputStream fout=new FileOutputStream("C:\\Users\\Administrator\\Desktop\\yzm\\"+System.currentTimeMillis()+".jpg");
ImageIO.write(bin, "JPEG", fout);

5 验证码的使用

5.1 提供一个表单 验证码组件的选项:day08_02login.jsp

<form method="post" action="/java43_01_web/day08/login.do">
    <table>
        <tr>
            <th>用户名字:</th>
            <td><input type="text" name="name" value="hmm"/></td>
        </tr>
        <tr>
            <th>用户密码:</th>
            <td><input type="text" name="pwd" value="123"/></td>
        </tr>
        <tr>
            <th>
                <img src="<c:url value='/day08/yzm.do'/>" id="img_yzm"/>
            </th>
            <td><input type="text" name="yzm"/></td>
        </tr>
        <tr>
            <th colspan="2">
                <input type="submit" value="登录"/>
            </th>
        </tr>
    </table>
</form>
<script type="text/javascript">
    window.οnlοad=function(){
        //给img_yzm添加点击时间
        document.getElementById("img_yzm").οnclick=function(){
            //添加提交参数n 是为了防止使用缓存
            this.src="<c:url value='/day08/yzm.do'/>?n="+new Date().getTime();
        }
    }
</script>

5.2 生成验证码的servlet

需要把生成的验证码存入session
需要把生成的图片通过response的输出流响应给客户端
package com.zhiyou100_08.download;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Day08_03YZM extends HttpServlet {
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String str="123456789一二三四区六七八九十男女老少上下多少春夏秋冬东西南北张王李赵";
		//通过java生成一个验证码:步骤和画画完全相同
		//1 准备画布
		BufferedImage bin=new BufferedImage(200, 40, BufferedImage.TYPE_INT_RGB);//在内存中开辟空间画画
		//2 准备画笔
		Graphics2D g=bin.createGraphics();
		//3 蘸墨水 画背景
		g.setColor(Color.WHITE);
		g.fillRect(1, 1, 198, 38);
		//4 蘸墨水 写字符
		String yzm="";
		for (int i = 0; i < 4; i++) {
			yzm+=str.charAt((int)(Math.random()*str.length()));
		}
		//把验证码放入session域中  登录时可以获取并判断验证码是否正确
		req.getSession().setAttribute("sessionYzm", yzm);
		//设置字体
		g.setFont(new Font(null, Font.BOLD, 30));//第一个参数字体类型 第二个参数加粗/斜体 第三个参数字体大小
		for (int i = 0; i < yzm.length(); i++) {
			//每个字符不同的颜色
			g.setColor(new Color(100+(int)(Math.random()*150), 50+(int)(Math.random()*200), 150+(int)(Math.random()*100)));
		    //画字符
			g.drawString(yzm.charAt(i)+"", 10+i*45, 34);
		}
		//添加一些干扰线
		for (int i = 0; i < (int)(Math.random()*10)+5; i++) {
			//每个字符不同的颜色
			g.setColor(new Color(200+(int)(Math.random()*50), 200+(int)(Math.random()*50), 150+(int)(Math.random()*100)));
		    //画线
			g.drawLine(1, (int)(Math.random()*40), 200, (int)(Math.random()*40));
		}
		//5把内存缓冲区中的图片的信息通过response的输出流 响应给客户端
		ImageIO.write(bin, "JPEG", resp.getOutputStream());
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}

5.3 登录的servlet

package com.zhiyou100_08.download;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Day08_04Login extends HttpServlet {
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//获取提交的yzm
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		//获取Day08_02YZM中随机的yzm
		String sessionYzm=(String)req.getSession().getAttribute("sessionYzm");
		//获取请求参数验证码
		req.setCharacterEncoding("utf-8");
		String yzm=req.getParameter("yzm");
		if(yzm.equals(sessionYzm)){
			  resp.getWriter().print("验证码正确!<a  href='/java43_01_web/jsp/day08_02login.jsp'>回到登录页面</a>");
		}else{
			 resp.getWriter().print("验证码错误!<a  href='/java43_01_web/jsp/day08_02login.jsp'>回到登录页面</a>");
		}
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}
Traceback (most recent call last): File "D:\pythonsthl\flaskProject\SeleniumTest\18Test11.py", line 11, in <module> driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install())) File "D:\pythonsthl\flaskProject\venv\lib\site-packages\webdriver_manager\chrome.py", line 39, in install driver_path = self._get_driver_path(self.driver) File "D:\pythonsthl\flaskProject\venv\lib\site-packages\webdriver_manager\core\manager.py", line 30, in _get_driver_path file = self._download_manager.download_file(driver.get_driver_download_url()) File "D:\pythonsthl\flaskProject\venv\lib\site-packages\webdriver_manager\drivers\chrome.py", line 40, in get_driver_download_url driver_version_to_download = self.get_driver_version_to_download() File "D:\pythonsthl\flaskProject\venv\lib\site-packages\webdriver_manager\core\driver.py", line 51, in get_driver_version_to_download self._driver_to_download_version = self._version if self._version not in (None, "latest") else self.get_latest_release_version() File "D:\pythonsthl\flaskProject\venv\lib\site-packages\webdriver_manager\drivers\chrome.py", line 62, in get_latest_release_version resp = self._http_client.get(url=latest_release_url) File "D:\pythonsthl\flaskProject\venv\lib\site-packages\webdriver_manager\core\http.py", line 37, in get self.validate_response(resp) File "D:\pythonsthl\flaskProject\venv\lib\site-packages\webdriver_manager\core\http.py", line 16, in validate_response raise ValueError(f"There is no such driver by url {resp.url}") ValueError: There is no such driver by url https://chromedriver.storage.googleapis.com/LATEST_RELEASE_115.0.5790 Process finished with exit code 1
07-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值