|
|
|
@ -4,15 +4,13 @@ import cn.hutool.json.JSONObject;
|
|
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
|
|
import com.dsideal.gw.GwApplication;
|
|
|
|
|
import com.jfinal.handler.Handler;
|
|
|
|
|
import com.jfinal.upload.UploadFile;
|
|
|
|
|
import okhttp3.*;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import javax.servlet.http.Part;
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.util.Enumeration;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class RouterHandler extends Handler {
|
|
|
|
@ -39,11 +37,11 @@ public class RouterHandler extends Handler {
|
|
|
|
|
/**
|
|
|
|
|
* 测试用例:
|
|
|
|
|
* GET
|
|
|
|
|
* http://10.10.21.20:8000/ds-base/dm/getDmSchoolProperty
|
|
|
|
|
* http://10.10.21.20:8000/ds-base/global/getGlobalList?page=1&limit=10
|
|
|
|
|
* http://10.10.21.20:8000/dsBase/dm/getDmSchoolProperty
|
|
|
|
|
* http://10.10.21.20:8000/dsBase/global/getGlobalList?page=1&limit=10
|
|
|
|
|
* <p>
|
|
|
|
|
* POST
|
|
|
|
|
* http://10.10.21.20:8000/ds-base/global/testPost?a=100
|
|
|
|
|
* http://10.10.21.20:8000/dsBase/global/testPost?a=100
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
//OkHttp的实例,单例模式
|
|
|
|
@ -77,9 +75,20 @@ public class RouterHandler extends Handler {
|
|
|
|
|
return formBodyBuilder.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static byte[] convertPartToByteArray(Part part) throws IOException {
|
|
|
|
|
InputStream input = part.getInputStream();
|
|
|
|
|
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
|
|
|
|
byte[] buffer = new byte[1024];
|
|
|
|
|
int bytesRead;
|
|
|
|
|
while ((bytesRead = input.read(buffer)) != -1) {
|
|
|
|
|
output.write(buffer, 0, bytesRead);
|
|
|
|
|
}
|
|
|
|
|
output.flush();
|
|
|
|
|
return output.toByteArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void handle(String target, HttpServletRequest req, HttpServletResponse res, boolean[] isHandled) {
|
|
|
|
|
|
|
|
|
|
//可以正确获取到URL的完整路径
|
|
|
|
|
String servletPath = req.getServletPath();
|
|
|
|
|
String queryString = req.getQueryString();
|
|
|
|
@ -90,6 +99,7 @@ public class RouterHandler extends Handler {
|
|
|
|
|
}
|
|
|
|
|
//如果是白名单,不检查jwt,否则需要检查jwt
|
|
|
|
|
if (GwApplication.whiteSet.contains(servletPath)) {
|
|
|
|
|
// TODO
|
|
|
|
|
System.out.println("白名单内链接,不检查jwt!");
|
|
|
|
|
} else {
|
|
|
|
|
System.out.println("不包含在白名单内链接,检查jwt!");
|
|
|
|
@ -111,7 +121,7 @@ public class RouterHandler extends Handler {
|
|
|
|
|
renderJson(res, jo);
|
|
|
|
|
} else {
|
|
|
|
|
//路由到哪个微服务
|
|
|
|
|
String FORWARD_URL = GwApplication.routeList.get(prefix) + "/" + action;
|
|
|
|
|
String FORWARD_URL = GwApplication.routeList.get(prefix) + "/" + prefix + "/" + action;
|
|
|
|
|
//处理GET请求
|
|
|
|
|
if (req.getMethod().equals("GET")) {
|
|
|
|
|
//参数:queryString
|
|
|
|
@ -150,24 +160,36 @@ public class RouterHandler extends Handler {
|
|
|
|
|
if (req.getContentType().startsWith("multipart/form-data")) {
|
|
|
|
|
// 指定文件类型
|
|
|
|
|
MediaType mediaType = MediaType.parse("multipart/form-data");
|
|
|
|
|
// 创建RequestBody
|
|
|
|
|
// 从JFinal请求中获取上传的文件
|
|
|
|
|
List<UploadFile> uploadFiles = ctx.getFiles();
|
|
|
|
|
if (!uploadFiles.isEmpty()) {
|
|
|
|
|
UploadFile uploadFile = uploadFiles.get(0); // 假设只有一个文件被上传
|
|
|
|
|
File file = uploadFile.getFile(); // 获取上传的文件
|
|
|
|
|
RequestBody requestBody = RequestBody.create(mediaType, file);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 获取上传的文件
|
|
|
|
|
Part filePart = req.getPart("file");
|
|
|
|
|
if (filePart == null) {
|
|
|
|
|
res.setStatus(HttpServletResponse.SC_BAD_REQUEST);
|
|
|
|
|
res.getWriter().write("No file uploaded");
|
|
|
|
|
//停止filter
|
|
|
|
|
isHandled[0] = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 获取文件流
|
|
|
|
|
RequestBody requestBody = RequestBody.create(convertPartToByteArray(filePart), mediaType, (int) filePart.getSize());
|
|
|
|
|
// 构建MultipartBody
|
|
|
|
|
MultipartBody body = new MultipartBody.Builder()
|
|
|
|
|
.setType(MultipartBody.FORM)
|
|
|
|
|
.addFormDataPart("file", file.getName(), requestBody)
|
|
|
|
|
.addFormDataPart("file", filePart.getSubmittedFileName(), requestBody)
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
// 构建Request
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
|
.url("http://your.upload.url/post")
|
|
|
|
|
.url(FORWARD_URL)
|
|
|
|
|
.post(body)
|
|
|
|
|
.build();
|
|
|
|
|
executeRequest(request, res);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
JSONObject jo = new JSONObject();
|
|
|
|
|
jo.put("success", false);
|
|
|
|
|
jo.put("message", "请求失败!" + e);
|
|
|
|
|
renderJson(res, jo);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// MinIO - 服务端签名直传(前端 + 后端 + 效果演示)
|
|
|
|
|