| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- package com.example.demo.Controller;
- import org.springframework.web.bind.annotation.*;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- @RestController
- public class ipGetController {
- public static String path = "D:\\demos\\src\\main\\resources\\static\\IP\\";
- @PostMapping("/saveIP")
- @ResponseBody
- static void saveIP(@RequestParam(value = "value",required = false) String IP){
- String id = IP.split("~~")[0];
- String ip = IP.split("~~")[1];
- File file = new File(path+id+".txt");
- if(file.exists()){
- file.delete();
- }
- try {
- file.createNewFile();
- }catch (Exception e){
- System.out.println(e);
- }
- try{
- FileWriter fw = new FileWriter(file);
- fw.write(ip);
- fw.flush();
- fw.close();
- }catch(Exception e){
- System.out.println(e);
- }
- }
- @GetMapping("/detectIP")
- @ResponseBody
- static int detectIP(@RequestParam(value = "ip",required = false) String IP){
- String id = IP.split("~~")[0];
- String ip = IP.split("~~")[1];
- File file = new File(path+id+".txt");
- if(!file.exists()){
- try {
- file.createNewFile();
- FileWriter fw = new FileWriter(file);
- fw.write(ip);
- fw.flush();
- fw.close();
- return 1;
- }catch (Exception e){
- System.out.println(e);
- return 0;
- }
- }
- else{
- try{
- FileReader fr = new FileReader(file);
- BufferedReader br = new BufferedReader(fr);
- String line = br.readLine();
- if(line.equals(ip)){
- return 1;
- }
- else{
- return 0;
- }
- }catch (Exception e){
- System.out.println(e);
- return 0;
- }
- }
- }
- }
|