Skip to content

Commit

Permalink
Complete random image service.
Browse files Browse the repository at this point in the history
  • Loading branch information
unline2 committed Jan 5, 2020
1 parent 9deb632 commit 6a4d8a3
Show file tree
Hide file tree
Showing 8 changed files with 301 additions and 14 deletions.
1 change: 1 addition & 0 deletions list/domain.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test.com
11 changes: 11 additions & 0 deletions list/images.txt
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
29 changes: 15 additions & 14 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,55 @@
<groupId>me.ffis</groupId>
<artifactId>Random-Image-Api</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>

<properties>
<spring.boot.version>2.2.1.RELEASE</spring.boot.version>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<dependencies>
<!--spring-boot 起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<!--spring-boot test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>${spring.boot.version}</version>
</dependency>
<!--logging 日志-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>


<!-- 打包插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
<configuration>
<!-- 指定该Main Class为全局的唯一入口 -->
<mainClass>me.ffis.randomImage.RandomImageApplication</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<!--可以把依赖的包都打包到生成的Jar包中-->
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Expand Down
78 changes: 78 additions & 0 deletions src/main/java/me/ffis/randomImage/config/ReadConfig.java
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 src/main/java/me/ffis/randomImage/controller/ImageController.java
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 src/main/java/me/ffis/randomImage/pojo/reponse/ReponseCode.java
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 src/main/java/me/ffis/randomImage/pojo/reponse/Result.java
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 src/main/java/me/ffis/randomImage/pojo/reponse/ResultResponse.java
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;
}
}

0 comments on commit 6a4d8a3

Please sign in to comment.