1. The syntax of the GET request
< a href ="download?filename=1366768.jpg" > Download image </ a > <!-- get request writing: address?request attribute name=request attribute value -->
Separate multiple attributes with &
2. Implement the download function by setting the response header
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <a href="download?filename=1366768.jpg">下载图片</a> <a href="download?filename=1366.jpg">下载图片</a> <a href="download?filename=你好.txt"> Download Chinese name document </a> < / body > < / html >
public class DownLoadServlet extends HttpServlet { @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String filename = request.getParameter("filename"); File f = new File( this .getServletContext().getRealPath("download/"+ filename));//Get server resources if (f.exists()) {//Solve the problem of Chinese file name String agent = request.getHeader("user-agent"); if (agent.contains("MSIE")) { // IE浏览器 filename = URLEncoder.encode(filename, "utf-8"); filename = filename.replace("+", " "); } else if (agent.contains("Firefox" )) { // Firefox BASE64Encoder base64Encoder = new BASE64Encoder(); filename = "=?utf-8?B?" + base64Encoder.encode(filename.getBytes("utf-8")) + "?="; } else { // Other browser filename = URLEncoder.encode(filename, "utf-8" ); } response.setContentType( this .getServletContext().getMimeType(f.getPath()));//Set the page file type response.setHeader( "Content-Disposition", "attachment;filename="+filename); // Open the file as an attachment ServletOutputStream sos = response.getOutputStream(); FileInputStream fis = new FileInputStream(f); int len = 0; byte[] b = new byte[1024]; while((len=fis.read(b))!=-1) { sos.write(b,0,len); } fis.close(); }else { response.setContentType( "text/html;charset=utf-8" );//Solve the problem of responding Chinese response.getWriter().println( "<h2 style='color:red;'>The file does not exist!</h2>" ); } } @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
3. Verification code generation
public class CheckImgServlet extends HttpServlet { // Save all idioms in the collection private List<String> words = new ArrayList<String> (); @Override public void init() throws ServletException { // Initialization stage, read new_words.txt // To read files in web project, absolute disk path must be used String path = getServletContext().getRealPath("/WEB-INF/new_words.txt " ); try { BufferedReader reader = new BufferedReader(new FileReader(path)); String line; while ((line = reader.readLine()) != null) { words.add(line); } reader.close(); } catch (IOException e) { e.printStackTrace (); } } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 禁止缓存 // response.setHeader("Cache-Control", "no-cache"); // response.setHeader("Pragma", "no-cache"); // response.setDateHeader("Expires", -1); int width = 120; int height = 30; // Step 1 draw a picture in memory BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // Step 2 picture drawing background color---through the drawing object Graphics graphics = bufferedImage.getGraphics(); // Get the drawing object---brush // A color must be specified before drawing any graphics graphics.setColor(getRandColor(200 , 250 )); graphics.fillRect(0, 0, width, height); // Step 3 draw the border graphics.setColor(Color.WHITE); graphics.drawRect(0, 0, width - 1, height - 1); // Step four and four random numbers Graphics2D graphics2d = (Graphics2D) graphics; // Set the output font graphics2d.setFont( new Font("宋体", Font.BOLD, 18 )); Random random = new Random(); // Generate random numbers int index = random.nextInt(words.size()); String word = words.get(index); // Get the idiom // define x coordinate int x = 10 ; for ( int i = 0; i < word.length(); i++ ) { // random color graphics2d.setColor( new Color(20 + random.nextInt(110), 20 + random .nextInt( 110), 20 + random.nextInt(110 ))); // rotate -30 --- 30 degrees int jiaodu = random.nextInt(60) - 30 ; // convert radians double theta = jiaodu * Math. PI/180 ; // get alphanumeric char c = word.charAt(i); // Output c to the picture graphics2d.rotate(theta, x, 20 ); graphics2d.drawString(String.valueOf(c), x, 20); graphics2d.rotate(-theta, x, 20); x += 30; } // Save the verification code content session request.getSession().setAttribute("checkcode_session" , word); // Step 5 draw the interference line graphics.setColor(getRandColor(160, 200 )); int x1; int x2; int y1; int y2; for ( int i = 0; i < 30; i++ ) { x1 = random.nextInt(width); x2 = random.nextInt(12); y1 = random.nextInt(height); y2 = random.nextInt(12); graphics.drawLine(x1, y1, x1 + x2, x2 + y2); } // Output the above image to the browser ImageIO graphics.dispose(); // Release resources // Write the image to response.getOutputStream() ImageIO.write(bufferedImage, "jpg" , response.getOutputStream()); // Verify ServletContext context = this .getServletContext(); context.setAttribute("word", word); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } /** * Take the color of a certain range * * @param fc * int range parameter 1 * @param bc * int range parameter 2 * @return Color */ private Color getRandColor( int fc, int bc) { // take its random color Random random = new Random(); if (fc > 255 ) { fc = 255; } if (bc > 255) { bc = 255; } int r = fc + random.nextInt(bc - fc); int g = fc + random.nextInt(bc - fc); int b = fc + random.nextInt(bc - fc); return new Color(r, g, b); } }