ipGetController.java 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package com.example.demo.Controller;
  2. import org.springframework.web.bind.annotation.*;
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. @RestController
  8. public class ipGetController {
  9. public static String path = "D:\\demos\\src\\main\\resources\\static\\IP\\";
  10. @PostMapping("/saveIP")
  11. @ResponseBody
  12. static void saveIP(@RequestParam(value = "value",required = false) String IP){
  13. String id = IP.split("~~")[0];
  14. String ip = IP.split("~~")[1];
  15. File file = new File(path+id+".txt");
  16. if(file.exists()){
  17. file.delete();
  18. }
  19. try {
  20. file.createNewFile();
  21. }catch (Exception e){
  22. System.out.println(e);
  23. }
  24. try{
  25. FileWriter fw = new FileWriter(file);
  26. fw.write(ip);
  27. fw.flush();
  28. fw.close();
  29. }catch(Exception e){
  30. System.out.println(e);
  31. }
  32. }
  33. @GetMapping("/detectIP")
  34. @ResponseBody
  35. static int detectIP(@RequestParam(value = "ip",required = false) String IP){
  36. String id = IP.split("~~")[0];
  37. String ip = IP.split("~~")[1];
  38. File file = new File(path+id+".txt");
  39. if(!file.exists()){
  40. try {
  41. file.createNewFile();
  42. FileWriter fw = new FileWriter(file);
  43. fw.write(ip);
  44. fw.flush();
  45. fw.close();
  46. return 1;
  47. }catch (Exception e){
  48. System.out.println(e);
  49. return 0;
  50. }
  51. }
  52. else{
  53. try{
  54. FileReader fr = new FileReader(file);
  55. BufferedReader br = new BufferedReader(fr);
  56. String line = br.readLine();
  57. if(line.equals(ip)){
  58. return 1;
  59. }
  60. else{
  61. return 0;
  62. }
  63. }catch (Exception e){
  64. System.out.println(e);
  65. return 0;
  66. }
  67. }
  68. }
  69. }