4.读取资源文件:
4.读取资源文件: (1).在java目录下新建properties (2).在resource目录下新建properties 都被打包到了同一个路径下:classes,我们俗称这个路径为classpath (3).切记我们赋值路径,复制的是out目录下的路径.
2.在class文件夹下进行添加properties文件.资源导出可能会出现问题:
Maven由于他的约定大于配置,我们之后可能遇到我们写的配置文件,无法导出或则生效的问题,解决方案:
!--在build中配置resources--> <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </build>
合成成功
基本思路: (1).设置getServletContext() ServletContext servletContext = this.getServletContext(); (2).导入文件的相对目录 InputStream is = servletContext.getResourceAsStream("/WEB-INF/classes/Database.properties"); (3).进行流的加载 Properties properties = new Properties(); properties.load(is); (4).通过键值对进行获取值 String username = properties.getProperty("username"); String password = properties.getProperty("password"); (6).打印输出 resp.getWriter().println(username+" "+password); (7).配置web
package com.Jsxs.Servlet; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class ServletDemo05 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext servletContext = this.getServletContext(); InputStream is = servletContext.getResourceAsStream("/WEB-INF/classes/Database.properties"); Properties properties = new Properties(); properties.load(is); String username = properties.getProperty("username"); String password = properties.getProperty("password"); resp.getWriter().println(username+" "+password); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
(十)、HttpServletResponse下载文件
30.简单分类:
web服务器接受到客户端的http请求,针对这个请求,分别创建一个代表响应的 HttpServletResponse对象,和一个代表请求HttpServletRequest的对象。 (1).如果要获取客户端请求过来的参数: 找httpServletRequest (2).如果要给客户端相应一些信息: 找HttpServletResponse 1.简单分类: 负责向浏览器发送数据的方法: (1). Servletoutputstream getOutputStream() throw IOException; (2). Printwriter getwriter() throw IOException; 负责向浏览器发送响应头的方法: void setContentLength(int var1); void setContentLengthLong(long var1); void setContentType(String var1); void setBufferSize(int var1); void setDateHeader(String var1, long var2); void addDateHeader(String var1, long var2); void setHeader(String var1, String var2); void addHeader(String var1, String var2); void setIntHeader(String var1, int var2); void addIntHeader(String var1, int var2); 响应的状态码: int SC_CONTINUE = 100; int SC_SWITCHING_PROTOCOLS = 101; int SC_OK = 200; int SC_CREATED = 201; int SC_ACCEPTED = 202; int SC_NON_AUTHORITATIVE_INFORMATION = 203; int SC_NO_CONTENT = 204; int SC_RESET_CONTENT = 205; int SC_PARTIAL_CONTENT = 206; int SC_MULTIPLE_CHOICES = 300; int SC_MOVED_PERMANENTLY = 301; int SC_MOVED_TEMPORARILY = 302; int SC_FOUND = 302; int SC_SEE_OTHER = 303; int SC_NOT_MODIFIED = 304; int SC_USE_PROXY = 305; int SC_TEMPORARY_REDIRECT = 307; int SC_BAD_REQUEST = 400; int SC_UNAUTHORIZED = 401; int SC_PAYMENT_REQUIRED = 402; int SC_FORBIDDEN = 403; int SC_NOT_FOUND = 404; int SC_METHOD_NOT_ALLOWED = 405; int SC_NOT_ACCEPTABLE = 406; int SC_PROXY_AUTHENTICATION_REQUIRED = 407; int SC_REQUEST_TIMEOUT = 408; int SC_CONFLICT = 409; int SC_GONE = 410; int SC_LENGTH_REQUIRED = 411; int SC_PRECONDITION_FAILED = 412; int SC_REQUEST_ENTITY_TOO_LARGE = 413; int SC_REQUEST_URI_TOO_LONG = 414; int SC_UNSUPPORTED_MEDIA_TYPE = 415; int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416; int SC_EXPECTATION_FAILED = 417; int SC_INTERNAL_SERVER_ERROR = 500; int SC_NOT_IMPLEMENTED = 501; int SC_BAD_GATEWAY = 502; int SC_SERVICE_UNAVAILABLE = 503; int SC_GATEWAY_TIMEOUT = 504; int SC_HTTP_VERSION_NOT_SUPPORTED = 505;
1.下载文件
resp.setHeader("content-Disposition","attachment;filename="+ URLEncoder.encode(fileName,"utf-8")); 1.向浏览器输出消息 2.下载文件: (1).要知道下载文件的路径 (2).下载的文件名是啥? (3).设置想办法让浏览器支持下载我们需要的东西 (4).获取下载文件的输入流 (5).获取输出流 (6).使用输出流将缓存区的数据输出到客户端. (7).配置web的操作
package com.Jsxs.Servlet; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.URLEncoder; public class FileServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // (1).要知道下载文件的路径 String realPath = "E:\\Ideal源码\\FirstMaven\\Servelete03\\src\\main\\resources\\1.jpg"; // (2).下载的文件名是啥? String fileName = realPath.substring(realPath.lastIndexOf("\\") + 1); // (3).设置想办法让浏览器支持下载我们需要的东西 resp.setHeader("content-Disposition","attachment;filename="+ URLEncoder.encode(fileName,"utf-8")); // (4).获取下载文件的输入流 FileInputStream fileInputStream = new FileInputStream(realPath); // (5).设置缓冲区 int count=0; byte[] bytes = new byte[1024]; // (6).获取fillOutputStream输出流 ServletOutputStream outputStream = resp.getOutputStream(); // (7).将fillOutputStream流写入到缓存区,使用输出流将缓存区的数据输出到客户端. while ((count=(fileInputStream.read(bytes)))!=-1){ outputStream.write(bytes,0,count); } outputStream.close(); fileInputStream.close(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
2.验证码
2.验证码功能: (1).浏览器每秒刷新的操作: resp.setHeader("refresh","3");
package com.Jsxs.Servlet; import javax.imageio.ImageIO; import javax.servlet.ServletException; 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; public class ImageServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 让浏览器秒刷新一次 resp.setHeader("refresh","3"); // 在内存中国创建一个图片 BufferedImage bufferedImage = new BufferedImage(80, 20, BufferedImage.TYPE_INT_BGR); // 得到图片 Graphics2D graphics2D=(Graphics2D) bufferedImage.getGraphics(); //笔 // 设置图片的背景颜色 graphics2D.setColor(new Color(224, 222, 222)); graphics2D.fillRect(0,0,80,20); // 给图片写入数据 graphics2D.setColor(new Color(25, 132, 206)); graphics2D.setFont(new Font("黑体",Font.BOLD,25)); graphics2D.drawString(makeNum(),0,20); // 告诉浏览器,这个方式用图片的方式打开 resp.setContentType("image/jpg"); // 取消缓存 resp.setDateHeader("expires",-1); resp.setHeader("Cache-Control","no-cache"); resp.setHeader("pragma","no-cache"); // 把图片写给浏览器 ImageIO.write(bufferedImage,"jpg",resp.getOutputStream()); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } private String makeNum(){ Random random=new Random(); String s = random.nextInt(9999999) + ""; StringBuffer stringBuffer=new StringBuffer(); // 假如不足七位数就进行0填充 for (int i = 0; i <7-s.length() ; i++) { stringBuffer.append("0"); } s=stringBuffer.toString()+s; return s; } }