文件下载&&动态生成验证码
来源:互联网 发布:linux 创建swap文件 编辑:程序博客网 时间:2024/05/18 09:15
文件下载
下载方式(两头一流):
(1)超链接下载
<a href="">下载</a>
(2)编码下载 通过servlet完成
<a href="/项目名称/servlet名称"?name=(文件名 )>下载</a>
a:设置文件的mime类型
String mimeType=context.getMimeType(文件名)
response.setContentType(mimeType);
b:设置下载头信息content-disposition
response.setHeader("content-disposition","attachment;filename="+文件名称
c:提供流
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取下载文件名称 String filename = request.getParameter("name"); ServletContext context = this.getServletContext(); // 文件下载 // 1:设置文件的mime类型 String mimeType = context.getMimeType(filename); response.setContentType(mimeType); // 2:设置下载的头信息 response.setHeader("content-disposition", " attchment;filename=" + filename); // 3:对拷流 // 获取输入流 InputStream is = context.getResourceAsStream("/download/" + filename); // 获取输出流 ServletOutputStream os = response.getOutputStream(); int len = -1; byte[] b = new byte[1024]; while ((len = is.read(b)) != -1) { os.write(b, 0, len); } os.close(); is.close(); }
用到的包:commons-io-1.4.jar,就可以不用流了
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取下载文件名称 String filename = request.getParameter("name"); ServletContext context = this.getServletContext(); // 文件下载 // 1:设置文件的mime类型 String mimeType = context.getMimeType(filename); response.setContentType(mimeType); // 2:设置下载的头信息 response.setHeader("content-disposition", " attchment;filename=" + filename); // 3:对拷流 // 获取输入流 InputStream is = context.getResourceAsStream("/download/" + filename); // 获取输出流 ServletOutputStream os = response.getOutputStream(); // int len = -1; /* * byte[] b = new byte[1024]; while ((len = is.read(b)) != -1) { os.write(b, 0, len); } */ IOUtils.copy(is, os); os.close(); is.close(); }
生成验证码
点击看不清可以换一张
/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 使用java图形界面技术绘制一张图片 int charNum = 4; int width = 30 * 4; int height = 30; // 1:创建一张内存图片 BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 2:获得绘制图像 Graphics graphics = bufferedImage.getGraphics(); // 3:绘制背颜色 graphics.setColor(Color.YELLOW); graphics.fillRect(0, 0, width, height); // 4:绘制图片边框 graphics.setColor(Color.BLUE); graphics.drawRect(0, 0, width - 1, height - 1); // 5:输出验证码内容 graphics.setColor(Color.RED); graphics.setFont(new Font("宋体", Font.BOLD, 20)); // 随机输出4个字符 Graphics2D graphics2d = (Graphics2D) graphics; String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ23456789"; Random random = new Random(); // session中要用到 String msg = ""; int x = 5; for (int i = 0; i < 4; i++) { int index = random.nextInt(32); String content = String.valueOf(s.charAt(index)); msg += content; double theta = random.nextInt(45) * Math.PI / 180; // 让字体扭曲 graphics2d.rotate(theta, x, 18); graphics2d.drawString(content, x, 18); graphics2d.rotate(-theta, x, 18); x += 30; } // 6.绘制干扰线 graphics.setColor(Color.GRAY); for (int i = 0; i < 5; i++) { int x1 = random.nextInt(width); int x2 = random.nextInt(width); int y1 = random.nextInt(height); int y2 = random.nextInt(width); graphics.drawLine(x1, y1, x2, y2); } // 释放资源 graphics.dispose(); // 图片输出ImageIO ImageIO.write(bufferedImage, "jpg", response.getOutputStream()); }
referer(盗链)
// 获取 浏览器内核 String agent = request.getHeader("user-agent"); System.out.println(agent); // 获取referer String referer = request.getHeader("referer"); if (referer == null) { System.out.println("直接在地址栏上输入的"); } else if (referer.contains("92.168.")) { System.out.println("哥们姐妹点的"); } else { System.out.println("盗链不好"); }
阅读全文
0 0
- 文件下载&&动态生成验证码
- 动态生成验证码
- 动态生成验证码
- 动态生成验证码
- 动态生成验证码
- 动态生成验证码
- 动态生成验证码
- 动态生成验证码图片
- 随机生成动态验证码
- Servlet生成动态验证码
- java动态生成验证码
- jsp生成动态验证码
- 【PHP】动态生成验证码
- 如果生成动态验证码
- PHP动态生成验证码
- java生成动态验证码
- 动态验证码生成实例
- 随机生成动态验证码
- 考研数学
- ubuntu卸载软件
- 商泰面试总结(嵌入式开发)
- Google File System
- MyBatis 示例之存储过程
- 文件下载&&动态生成验证码
- iOS关于支付宝回调走哪个的问题。。
- Spring注解区别
- wps表格的宏被禁用问题
- css如何做到 鼠标移动到超链接时,超链接会发生改变,例如改变颜色、字体大小、背景颜色等
- Android RelativeLayout 属性的含义
- ZLGGUI的几个BUG
- 2.1 基本内置类型(2)字面值常量,转义序列
- Google's BigTable