add lombok
This commit is contained in:
@@ -1,19 +1,16 @@
|
||||
package com.imyeyu.spring;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.imyeyu.io.IO;
|
||||
import com.imyeyu.java.TimiJava;
|
||||
import com.imyeyu.java.bean.Language;
|
||||
import com.imyeyu.java.bean.timi.TimiCode;
|
||||
import com.imyeyu.java.bean.timi.TimiException;
|
||||
import com.imyeyu.java.bean.timi.TimiResponse;
|
||||
import com.imyeyu.java.ref.Ref;
|
||||
import com.imyeyu.spring.bean.RequestRange;
|
||||
import jakarta.servlet.http.Cookie;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.servlet.http.HttpSession;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.BeanWrapper;
|
||||
import org.springframework.beans.BeanWrapperImpl;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
@@ -21,8 +18,7 @@ import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
@@ -39,56 +35,12 @@ import java.util.Locale;
|
||||
*/
|
||||
public class TimiSpring {
|
||||
|
||||
/** 版本号 */
|
||||
public static final String VERSION = "0.0.1";
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(TimiSpring.class);
|
||||
private static final Gson GSON = new Gson();
|
||||
|
||||
/**
|
||||
* 工具类禁止实例化
|
||||
*/
|
||||
private TimiSpring() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 回调数据
|
||||
*
|
||||
* @param response 返回
|
||||
* @param resp 返回结果
|
||||
*/
|
||||
public static void render(HttpServletResponse response, TimiResponse<?> resp) {
|
||||
try {
|
||||
HttpSession session = getSession();
|
||||
response.setContentType("application/json;charset=UTF-8");
|
||||
OutputStream out = response.getOutputStream();
|
||||
out.write(GSON.toJson(resp).getBytes(StandardCharsets.UTF_8));
|
||||
out.flush();
|
||||
out.close();
|
||||
} catch (Exception e) {
|
||||
log.error("TimiSpring.render Error", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 回调错误
|
||||
*
|
||||
* @param response 返回
|
||||
* @param code 代码
|
||||
* @param msgKey 消息映射键
|
||||
*/
|
||||
public static void renderError(HttpServletResponse response, TimiCode code, String msgKey) {
|
||||
try {
|
||||
response.setContentType("application/json;charset=UTF-8");
|
||||
OutputStream out = response.getOutputStream();
|
||||
out.write(GSON.toJson(code.toResponse().msg(msgKey)).getBytes(StandardCharsets.UTF_8));
|
||||
out.flush();
|
||||
out.close();
|
||||
} catch (Exception e) {
|
||||
log.error("TimiSpring.renderError error", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 Servlet 请求属性
|
||||
*
|
||||
@@ -380,9 +332,9 @@ public class TimiSpring {
|
||||
if (cookies == null) {
|
||||
return null;
|
||||
}
|
||||
for (int i = 0; i < cookies.length; i++) {
|
||||
if (cookies[i].getName().equals(key)) {
|
||||
return cookies[i];
|
||||
for (Cookie cookie : cookies) {
|
||||
if (cookie.getName().equals(key)) {
|
||||
return cookie;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
@@ -469,11 +421,11 @@ public class TimiSpring {
|
||||
/**
|
||||
* 解析 Range 请求范围
|
||||
*
|
||||
* @param fileLength 文件长度
|
||||
* @param total 总数据量
|
||||
* @return 请求范围
|
||||
* @throws IOException IO 异常
|
||||
*/
|
||||
public static RequestRange requestRange(long fileLength) throws IOException {
|
||||
public static RequestRange getRequestRange(long total) throws IOException {
|
||||
HttpServletResponse resp = getResponse();
|
||||
|
||||
String range = getRequestAttrAsString("Range");
|
||||
@@ -485,14 +437,14 @@ public class TimiSpring {
|
||||
String[] ranges = rangeValue.split("-");
|
||||
TimiException.requiredTrue(2 == ranges.length, "Invalid Range format");
|
||||
long start = Long.parseLong(ranges[0]);
|
||||
long end = ranges[1].isEmpty() ? fileLength - 1 : Long.parseLong(ranges[1]);
|
||||
long end = ranges[1].isEmpty() ? total - 1 : Long.parseLong(ranges[1]);
|
||||
// 验证范围有效性
|
||||
if (start < 0 || fileLength <= end || end < start) {
|
||||
resp.setHeader("Content-Range", "bytes */" + fileLength);
|
||||
if (start < 0 || total <= end || end < start) {
|
||||
resp.setHeader("Content-Range", "bytes */" + total);
|
||||
resp.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
|
||||
return null;
|
||||
}
|
||||
return new RequestRange(start, end);
|
||||
return new RequestRange(start, end, total);
|
||||
}
|
||||
|
||||
public static void copyPropertiesNotNull(Object source, Object target) {
|
||||
@@ -507,4 +459,22 @@ public class TimiSpring {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void responseRangeStream(InputStream stream, long total) throws IOException {
|
||||
HttpServletResponse resp = getResponse();
|
||||
RequestRange range = getRequestRange(total);
|
||||
if (range == null) {
|
||||
// 完整文件
|
||||
resp.setContentLengthLong(total);
|
||||
resp.setStatus(HttpServletResponse.SC_OK);
|
||||
IO.toOutputStream(stream, resp.getOutputStream());
|
||||
} else {
|
||||
// 分片文件
|
||||
resp.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
|
||||
resp.setHeader("Content-Range", "bytes %s-%s/%s".formatted(range.getStart(), range.getEnd(), range.getTotal()));
|
||||
resp.setContentLengthLong(range.getLength());
|
||||
IO.toOutputStream(stream, resp.getOutputStream(), range.getStart(), range.getEnd());
|
||||
resp.flushBuffer();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user