更新网页接口

更新接口
This commit is contained in:
yutou
2020-12-04 14:09:12 +08:00
parent 3edfd2b55f
commit 7ec461ff29
4 changed files with 106 additions and 85 deletions

View File

@@ -9,7 +9,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ToolsApplication {
public static final String version="1.0.12";
public static final String version="1.0.13";
public static void main(String[] args) {
System.out.println("当前版本号:"+version);

View File

@@ -8,6 +8,7 @@ import com.yutou.tools.utils.ConfigTools;
import com.yutou.tools.utils.MusicTools;
import com.yutou.tools.utils.Tools;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
@@ -106,6 +107,9 @@ public class MusicController {
@ResponseBody
public String findFile(@RequestBody JSONObject body) {
String path = body.getString("path");
if(!path.startsWith(defaultMusicPath)){
path=Tools.base64ToString(path);
}
JSONObject json = new JSONObject();
if (StringUtils.isEmpty(path)) {
json.put("code", -1);
@@ -129,38 +133,29 @@ public class MusicController {
json.put("code", 0);
return json.toJSONString();
}
@RequestMapping("image.do")
@RequestMapping(value = "web/image.do",produces = MediaType.IMAGE_JPEG_VALUE)
@ResponseBody
public String getImage(@RequestBody JSONObject body) {
JSONObject json = new JSONObject();
public byte[] getImage(String fileName){
JSONObject json=new JSONObject();
json.put("fileName",fileName);
return getImage(json);
}
@RequestMapping(value = "image.do",produces = MediaType.IMAGE_JPEG_VALUE)
@ResponseBody
public byte[] getImage(@RequestBody JSONObject body) {
String fileName = body.getString("fileName");
json.put("code", 1);
if (StringUtils.isEmpty(fileName)) {
json.put("msg", "文件为空");
json.put("code", -1);
json.put("data", "");
return json.toJSONString();
if(!fileName.startsWith(defaultMusicPath)){
fileName=Tools.base64ToString(fileName);
}
File file = new File(fileName);
try {
if (file.exists()) {
json.put("msg", "ok");
json.put("code", 0);
json.put("data", musicTools.readImage(file.getAbsolutePath()));
} else {
json.put("msg", "文件不存在");
json.put("code", -1);
json.put("data", "");
File file=new File(fileName);
if(file.exists()){
try {
return musicTools.readImage(file.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
json.put("msg", "图片读取失败");
json.put("code", -1);
json.put("data", "");
e.printStackTrace();
}
return json.toJSONString();
return null;
}
@RequestMapping("random.do")
@@ -182,16 +177,19 @@ public class MusicController {
return new String(Base64.getEncoder().encode(str.getBytes()));
}
@RequestMapping("play.do")
public ResponseEntity<FileSystemResource> play(@RequestBody JSONObject body) {
String filePath = body.getString("filePath");
boolean random = body.containsKey("random") ? body.getBoolean("random") : StringUtils.isEmpty(filePath);
if (random) {
public ResponseEntity<FileSystemResource> play(String filePath,String random) {
String _filePath;
boolean _random;
_random= !StringUtils.isEmpty(random) && random.equals("true");
_filePath=Tools.base64ToString(filePath);
if (_random) {
List<MusicData> list = musicTools.getMusicList();
MusicData data = list.get(Tools.randomCommon(0, list.size() - 1, 1)[0]);
filePath = data.getFile();
_filePath = data.getFile();
}
File file = new File(filePath);
File file = new File(_filePath);
if (file.exists()) {
return Tools.getFile(file);
} else {

View File

@@ -17,6 +17,7 @@ import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.Base64;
@@ -374,7 +375,18 @@ public class Tools {
}
public static String base64ToString(String base){
base=base.replace(" ","+");
return new String(Base64.getDecoder().decode(base.replace("\r\n","").getBytes()));
try {
base=new String(Base64.getDecoder().decode(base.replace("\r\n","").getBytes()));
}catch (Exception e){
try {
base=URLDecoder.decode(base,"UTF-8");
base=base.replace(" ","+");
base=new String(Base64.getDecoder().decode(base.replace("\r\n","").getBytes()));
} catch (Exception e1) {
e1.printStackTrace();
}
}
return base;
}
/**
* 异常输出