forked from unline2/RandomImage
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
301 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
http://img.test.com/images/bg-0.jpg | ||
http://img.test.com/images/bg-1.jpg | ||
http://img.test.com/images/bg-2.jpg | ||
http://img.test.com/images/bg-3.jpg | ||
http://img.test.com/images/bg-4.jpg | ||
http://img.test.com/images/bg-5.jpg | ||
http://img.test.com/images/bg-6.jpg | ||
http://img.test.com/images/bg-7.jpg | ||
http://img.test.com/images/bg-8.jpg | ||
http://img.test.com/images/bg-9.jpg | ||
http://img.test.com/images/bg-10.jpg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package me.ffis.randomImage.config; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.*; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* 用于读取图片列表和域名白名单数据 | ||
* Created by fanfan on 2020/01/05. | ||
*/ | ||
|
||
@Slf4j | ||
@Component | ||
public class ReadConfig { | ||
//创建静态成员变量用于保存图片列表 | ||
public static List<String> images = new ArrayList<>(); | ||
//创建静态成员变量用于域名列表 | ||
public static List<String> domain = new ArrayList<>(); | ||
|
||
static { | ||
//程序启动的时候先加载图片和域名列表到集合中 | ||
loadImages(); | ||
loadDomain(); | ||
} | ||
|
||
//加载图片列表到images中 | ||
public static Boolean loadImages() { | ||
images.clear(); | ||
BufferedReader bw = null; | ||
try { | ||
bw = new BufferedReader(new FileReader("list/images.txt")); | ||
String line; | ||
while ((line = bw.readLine())!= null) { | ||
images.add(line); | ||
} | ||
} catch (IOException e) { | ||
log.error("图片列表加载失败!文件不存在!", e); | ||
return false; | ||
} finally { | ||
try { | ||
if (bw != null) | ||
bw.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
log.info("图片加载成功!"); | ||
return true; | ||
} | ||
|
||
//加载域名列表到domain集合中 | ||
public static Boolean loadDomain() { | ||
domain.clear(); | ||
BufferedReader bw = null; | ||
try { | ||
bw = new BufferedReader(new FileReader("list/domain.txt")); | ||
String line; | ||
while ((line = bw.readLine())!= null) { | ||
domain.add(line); | ||
} | ||
} catch (IOException e) { | ||
log.error("域名列表加载失败!文件不存在!", e); | ||
return false; | ||
} finally { | ||
try { | ||
if (bw != null) | ||
bw.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
log.info("域名加载成功!"); | ||
return true; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/me/ffis/randomImage/controller/ImageController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package me.ffis.randomImage.controller; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import me.ffis.randomImage.config.ReadConfig; | ||
import me.ffis.randomImage.pojo.reponse.ReponseCode; | ||
import me.ffis.randomImage.pojo.reponse.Result; | ||
import me.ffis.randomImage.pojo.reponse.ResultResponse; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
|
||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by fanfan on 2020/01/05. | ||
*/ | ||
|
||
@Slf4j | ||
@Controller | ||
public class ImageController { | ||
|
||
//获取随机图片 | ||
@ResponseBody | ||
@GetMapping("images") | ||
public void getRandomImages(HttpServletResponse response) { | ||
//givenList.get((int) (Math.random() * givenList.size())); | ||
//根据images集合大小生成随机数 | ||
int index = (int) (Math.random() * ReadConfig.images.size()); | ||
//获取随机的图片地址 | ||
String imgUrl = ReadConfig.images.get(index); | ||
try { | ||
//重定向到图片地址 | ||
response.sendRedirect(imgUrl); | ||
} catch (IOException e) { | ||
log.error("重定向到图片地址失败!", e); | ||
} | ||
} | ||
|
||
/** | ||
* 刷新图片缓存 | ||
* @return 刷新结果 | ||
*/ | ||
@ResponseBody | ||
@GetMapping("flush") | ||
public Result flushCache() { | ||
log.info("刷新图片缓存"); | ||
Boolean imagesflag = ReadConfig.loadImages(); | ||
Boolean domainflag = ReadConfig.loadDomain(); | ||
|
||
if (!imagesflag && !domainflag) { | ||
return new ResultResponse(ReponseCode.FLUSH_FAIL); | ||
} | ||
if (!imagesflag) { | ||
return new ResultResponse(ReponseCode.FLUSH_IMAGES_FAIL); | ||
} | ||
if (!domainflag) { | ||
return new ResultResponse(ReponseCode.FLUSH_DOMAIN_FAIL); | ||
} | ||
|
||
List<String> domain = ReadConfig.domain; | ||
List<String> images = ReadConfig.images; | ||
return new ResultResponse(ReponseCode.FLUSH_SUCCESS); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/me/ffis/randomImage/pojo/reponse/ReponseCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package me.ffis.randomImage.pojo.reponse; | ||
|
||
import lombok.ToString; | ||
|
||
/** | ||
* 枚举类封装返回常量 | ||
*/ | ||
@ToString | ||
public enum ReponseCode implements Result { | ||
FLUSH_SUCCESS(true, 100, "刷新缓存成功"), | ||
FLUSH_FAIL(false, 101, "刷新缓存失败"), | ||
FLUSH_IMAGES_FAIL(false, 102, "刷新图片缓存失败"), | ||
FLUSH_DOMAIN_FAIL(false, 103, "刷新域名缓存失败"); | ||
|
||
//操作是否成功 | ||
private boolean flag; | ||
//操作代码 | ||
private Integer code; | ||
//提示信息 | ||
private String message; | ||
//返回信息 | ||
private Object data; | ||
|
||
ReponseCode(boolean flag, Integer code, String message) { | ||
this.flag = flag; | ||
this.code = code; | ||
this.message = message; | ||
} | ||
|
||
@Override | ||
public boolean flag() { | ||
return flag; | ||
} | ||
|
||
@Override | ||
public Integer code() { | ||
return code; | ||
} | ||
|
||
@Override | ||
public String message() { | ||
return message; | ||
} | ||
|
||
@Override | ||
public Object data() { | ||
return data; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/me/ffis/randomImage/pojo/reponse/Result.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package me.ffis.randomImage.pojo.reponse; | ||
|
||
/** | ||
* Created by fanfan on 2019/12/05. | ||
*/ | ||
public interface Result { | ||
//操作是否成功 | ||
boolean flag(); | ||
|
||
//操作代码 | ||
Integer code(); | ||
|
||
//提示信息 | ||
String message(); | ||
|
||
//返回信息 | ||
Object data(); | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/me/ffis/randomImage/pojo/reponse/ResultResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package me.ffis.randomImage.pojo.reponse; | ||
|
||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import lombok.ToString; | ||
|
||
/** | ||
* Created by fanfan on 2019/12/05. | ||
*/ | ||
|
||
@Data | ||
@ToString | ||
@NoArgsConstructor | ||
public class ResultResponse implements Result { | ||
|
||
//操作是否成功 | ||
private boolean flag; | ||
//操作代码 | ||
private Integer code; | ||
//提示信息 | ||
private String message; | ||
//返回数据 | ||
private Object data; | ||
|
||
public ResultResponse(boolean flag, Integer code, String message) { | ||
this.flag = flag; | ||
this.code = code; | ||
this.message = message; | ||
} | ||
|
||
public ResultResponse(Result result) { | ||
this.flag = result.flag(); | ||
this.code = result.code(); | ||
this.message = result.message(); | ||
} | ||
|
||
public ResultResponse(Result result, Object data) { | ||
this.flag = result.flag(); | ||
this.code = result.code(); | ||
this.message = result.message(); | ||
this.data = data; | ||
} | ||
|
||
@Override | ||
public boolean flag() { | ||
return flag; | ||
} | ||
|
||
@Override | ||
public Integer code() { | ||
return code; | ||
} | ||
|
||
@Override | ||
public String message() { | ||
return message; | ||
} | ||
|
||
@Override | ||
public Object data() { | ||
return data; | ||
} | ||
} |