JavaWeb 文件下载案例 (response)

学习资历:黑马JAVA2019版

自制简易验证码逻辑:

package cn.web.servlet;

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 java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

@WebServlet("/checkCodeServlet")
public class CheckCodeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int width = 100;
        int height = 50;

        //1创建一个对象,在内存中是图片(验证码图片对象)
        BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

        //2.美化图片
        //2.1填充背景色
        Graphics g = image.getGraphics(); //画笔对象
        g.setColor(Color.PINK);
        g.fillRect(0,0,width,height);

        //2.2画边框
        g.setColor(Color.blue);
        g.drawRect(0,0,width-1,height-1);

        //2.3 写验证码
        String str = "QWERTYUIOPASDFGHJKLZXCVBNM0123456789";

        //生成随机脚标
        Random ran = new Random();
        for(int i = 1;i <= 4;i++)
        {
            int index = ran.nextInt(str.length());
            //获取字符
            char ch = str.charAt(index);
            g.drawString(ch+"",width / 5 *i,height/2);
        }

        //2.4 划干扰线
        g.setColor(Color.green);

        for(int i = 0;i < 7;i++)
        {
            int x1 = ran.nextInt(width);
            int y1 = ran.nextInt(height);
            int x2 = ran.nextInt(width);
            int y2 = ran.nextInt(height);

            g.drawLine(x1,y1,x2,y2);
        }

        //3.将图片输出到页面上
        ImageIO.write(image,"jpg",response.getOutputStream());
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}

下载servlet:
注意两点:
1.html中的超链填写servlet的虚拟路径
2.html中超链参数不能有空格

package cn.web.download;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
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.FileInputStream;
import java.io.IOException;

@WebServlet("/downLoadServlet")
public class DownLoadServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取请求参数,文件名称
        String filename = request.getParameter("filename");


        //2.使用字节输入流加载文件进内存
        //2.1找到文件服务器路径
        ServletContext servletContext = this.getServletContext();
        String realpath = servletContext.getRealPath("/img/"+filename); //初始路径在web处
        //2.2用字节输入流关联(生成输入流)
        FileInputStream fis = new FileInputStream(realpath);


        //3.设置response响应头
        //3.1设置响应头类型,context-type 告诉浏览器文件类型
        String mimeType = servletContext.getMimeType(filename);
        response.setHeader("content-type",mimeType);

        //3.2设置响应头打开方式:content-disposition 告诉浏览器打开方式
        response.setHeader("content-disposition","attachment;filename=" + filename); //以附件形式打开

        //4.将输入流的数据写出到输出流中
        ServletOutputStream sos = response.getOutputStream();
        byte [] buff = new byte[1024 * 8]; //缓存区
        int len = 0;
        while((len = fis.read(buff)) != -1) //读到buff缓存区中,若不等于-1则没读完
        {
            sos.write(buff,0,len);
        }
        fis.close();//关闭输入流

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
	//这样的超链点开来是直接打开的,并且路径固定,一旦要改非常麻烦
    <a href="/download/488.png">图片1</a>

    <hr>
    //里面servlet地址填虚拟路径
    //注意问号后面filname=xxx不能有空格,不然完全读不到参数
    <a href="/day15/downLoadServlet?filename=640.png">图片1</a>
    <a href="/day15/downLoadServlet?filename=ex1.png">图片2</a>

</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值