111
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
package com.xinda.http.controller;
|
||||
|
||||
import com.xinda.http.client.Amap;
|
||||
import com.xinda.http.client.Cn12306;
|
||||
import com.xinda.http.client.Gitee;
|
||||
import com.xinda.http.model.*;
|
||||
import com.dtflys.forest.http.ForestResponse;
|
||||
import javax.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
public class ForestExampleController {
|
||||
|
||||
@Resource
|
||||
private Amap amap;
|
||||
|
||||
@Resource
|
||||
private Gitee gitee;
|
||||
|
||||
@Resource
|
||||
private Cn12306 cn12306;
|
||||
|
||||
|
||||
|
||||
@GetMapping("/amap/location")
|
||||
public Result<Location> amapLocation(@RequestParam BigDecimal longitude, @RequestParam BigDecimal latitude) {
|
||||
Result<Location> result = amap.getLocation(longitude.toEngineeringString(), latitude.toEngineeringString());
|
||||
return result;
|
||||
}
|
||||
|
||||
@GetMapping("/amap/location2")
|
||||
public Map amapLocation2(@RequestParam BigDecimal longitude, @RequestParam BigDecimal latitude) {
|
||||
Coordinate coordinate = new Coordinate(
|
||||
longitude.toEngineeringString(),
|
||||
latitude.toEngineeringString());
|
||||
Map result = amap.getLocation(coordinate);
|
||||
return result;
|
||||
}
|
||||
|
||||
@GetMapping("/amap/location3")
|
||||
public Map amapLocation3(@RequestParam BigDecimal longitude, @RequestParam BigDecimal latitude) {
|
||||
Coordinate coordinate = new Coordinate(
|
||||
longitude.toEngineeringString(),
|
||||
latitude.toEngineeringString());
|
||||
Map result = amap.getLocationByCoordinate(coordinate);
|
||||
return result;
|
||||
}
|
||||
|
||||
@GetMapping("/gitee")
|
||||
public String gitee() {
|
||||
String result = gitee.index();
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/gitee/async")
|
||||
public String aysncGitee() throws ExecutionException, InterruptedException {
|
||||
Future<String> future = gitee.asyncIndex();
|
||||
return future.get();
|
||||
}
|
||||
|
||||
@GetMapping("/gitee/async2")
|
||||
public String aysncGitee2() throws ExecutionException, InterruptedException {
|
||||
AtomicReference<String> ref = new AtomicReference<>("");
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
gitee.asyncIndex2((result, request, response) -> {
|
||||
ref.set(result);
|
||||
latch.countDown();
|
||||
}, (ex, request, response) -> {
|
||||
ref.set(ex.getMessage());
|
||||
latch.countDown();
|
||||
});
|
||||
latch.await();
|
||||
return ref.get();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@GetMapping("/12306")
|
||||
public String cn12306() {
|
||||
ForestResponse<String> response = cn12306.index();
|
||||
return response.getResult();
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/gitee/branches")
|
||||
public List<GiteeBranch> giteeBranches(@RequestParam String accessToken,
|
||||
@RequestParam String owner,
|
||||
@RequestParam String repo) {
|
||||
List<GiteeBranch> branches = gitee.branches(accessToken, owner, repo);
|
||||
return branches;
|
||||
}
|
||||
|
||||
@GetMapping("/gitee/readme")
|
||||
public GiteeReadme giteeReadme(@RequestParam String accessToken,
|
||||
@RequestParam String owner,
|
||||
@RequestParam String repo,
|
||||
@RequestParam String ref) {
|
||||
GiteeReadme readme = gitee.readme(accessToken, owner, repo, ref);
|
||||
return readme;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.xinda.http.controller;
|
||||
|
||||
import com.xinda.http.client.TestInterceptorClient;
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
|
||||
@RestController
|
||||
public class InterceptorController {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(InterceptorController.class);
|
||||
|
||||
@Resource
|
||||
private TestInterceptorClient testInterceptorClient;
|
||||
|
||||
@PostMapping("/receive-interceptor")
|
||||
public String receiveInterceptor(HttpServletRequest request, @RequestParam String username) {
|
||||
String token = request.getHeader("accessToken");
|
||||
logger.info("accessToken: {}", token);
|
||||
return "ok";
|
||||
}
|
||||
|
||||
@GetMapping("/test-interceptor")
|
||||
public String testInterceptor(@RequestParam String username) {
|
||||
String result = testInterceptorClient.testInterceptor(username);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package com.xinda.http.controller;
|
||||
|
||||
import com.dtflys.forest.Forest;
|
||||
import com.dtflys.forest.logging.LogConfiguration;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/async")
|
||||
public class TestAsyncController {
|
||||
|
||||
@GetMapping("/data")
|
||||
public Map<String, Object> getData() {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("value", "foo");
|
||||
return map;
|
||||
}
|
||||
|
||||
@GetMapping("/test")
|
||||
public Map<String, Object> testAsync() throws InterruptedException {
|
||||
int batch = 20000;
|
||||
int total = 100;
|
||||
final LogConfiguration logConfiguration = new LogConfiguration();
|
||||
logConfiguration.setLogEnabled(false);
|
||||
for (int i = 0; i < batch; i++) {
|
||||
System.out.println("执行批次: " + i);
|
||||
final CountDownLatch latch = new CountDownLatch(total);
|
||||
final AtomicInteger count = new AtomicInteger(0);
|
||||
final AtomicInteger errorCount = new AtomicInteger(0);
|
||||
for (int j = 0; j < total; j++) {
|
||||
try {
|
||||
Forest.get("/async/data")
|
||||
.backend("httpclient")
|
||||
.host("localhost")
|
||||
.port(8080)
|
||||
.setLogConfiguration(logConfiguration)
|
||||
.async()
|
||||
.onSuccess((data, req, res) -> {
|
||||
latch.countDown();
|
||||
int c = count.incrementAndGet();
|
||||
// System.out.println("已成功 " + c);
|
||||
})
|
||||
.onError((ex, req, res) -> {
|
||||
latch.countDown();
|
||||
int c = count.incrementAndGet();
|
||||
errorCount.incrementAndGet();
|
||||
System.out.println("已失败 第一阶段: " + ex);
|
||||
})
|
||||
.execute();
|
||||
} catch (Throwable th) {
|
||||
}
|
||||
}
|
||||
try {
|
||||
latch.await();
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
|
||||
final CountDownLatch latch2 = new CountDownLatch(total);
|
||||
final AtomicInteger count2 = new AtomicInteger(0);
|
||||
final AtomicInteger errorCount2 = new AtomicInteger(0);
|
||||
for (int j = 0; j < total; j++) {
|
||||
Forest.get("/async/data")
|
||||
.backend("httpclient")
|
||||
.host("localhost")
|
||||
.port(8080)
|
||||
.async()
|
||||
.setLogConfiguration(logConfiguration)
|
||||
.onSuccess((data, req, res) -> {
|
||||
latch2.countDown();
|
||||
int c = count2.incrementAndGet();
|
||||
})
|
||||
.onError((ex, req, res) -> {
|
||||
latch2.countDown();
|
||||
int c = count2.incrementAndGet();
|
||||
if (ex != null) {
|
||||
errorCount2.incrementAndGet();
|
||||
}
|
||||
if (c == total) {
|
||||
} else {
|
||||
System.out.println("已失败 第二阶段: " + c);
|
||||
}
|
||||
})
|
||||
.execute();
|
||||
}
|
||||
try {
|
||||
latch2.await();
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
|
||||
}
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("status", "ok");
|
||||
return map;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package com.xinda.http.controller;
|
||||
|
||||
import com.xinda.http.client.DownloadClient;
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author gongjun[dt_flys@hotmail.com]
|
||||
* @since 2020-08-04 22:36
|
||||
*/
|
||||
@RestController
|
||||
public class TestDownloadController {
|
||||
|
||||
@Resource
|
||||
private DownloadClient downloadClient;
|
||||
|
||||
|
||||
@GetMapping("/download-image")
|
||||
public Map downloadImage() {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
File file = downloadClient.downloadFile("D:\\TestDownload", progress -> {
|
||||
System.out.println("-------------------------------------------------------");
|
||||
System.out.println("total bytes: " + progress.getTotalBytes());
|
||||
System.out.println("current bytes: " + progress.getCurrentBytes());
|
||||
System.out.println("percentage: " + (int) Math.floor(progress.getRate() * 100) + "%");
|
||||
});
|
||||
result.put("status", "ok");
|
||||
return result;
|
||||
}
|
||||
|
||||
@GetMapping("/download-file")
|
||||
public Map downloadFile() {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
File file = downloadClient.downloadFile("D:\\TestDownload", progress -> {
|
||||
System.out.println("-------------------------------------------------------");
|
||||
System.out.println("total bytes: " + progress.getTotalBytes());
|
||||
System.out.println("current bytes: " + progress.getCurrentBytes());
|
||||
System.out.println("percentage: " + (int) Math.floor(progress.getRate() * 100) + "%");
|
||||
});
|
||||
result.put("status", "ok");
|
||||
return result;
|
||||
}
|
||||
|
||||
@GetMapping("/download-image-to-byte-array")
|
||||
public Map downloadImageToByteArray() throws IOException {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
byte[] buffer = downloadClient.downloadImageToByteArray();
|
||||
File file = new File("D:\\TestDownload\\test-byte-array.jpg");
|
||||
FileUtils.writeByteArrayToFile(file, buffer);
|
||||
result.put("status", "ok");
|
||||
return result;
|
||||
}
|
||||
|
||||
@GetMapping("/download-image-to-byte-array-with-annotation")
|
||||
public Map downloadImageToByteArrayWithAnnotation() throws IOException {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
byte[] buffer = downloadClient.downloadImageToByteArrayWithAnnotation();
|
||||
File file = new File("D:\\TestDownload\\test-byte-array.jpg");
|
||||
FileUtils.writeByteArrayToFile(file, buffer);
|
||||
result.put("status", "ok");
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/download-image-to-stream")
|
||||
public Map downloadImageToStream() throws IOException {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
InputStream in = downloadClient.downloadImageToInputStream();
|
||||
File file = new File("D:\\TestDownload\\test-input-stream.jpg");
|
||||
FileUtils.copyInputStreamToFile(in, file);
|
||||
result.put("status", "ok");
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
package com.xinda.http.controller;
|
||||
|
||||
import com.xinda.http.client.UploadClient;
|
||||
import com.xinda.http.service.FileService;
|
||||
import com.xinda.http.utils.PathUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.*;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
public class TestUploadController {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(TestUploadController.class);
|
||||
|
||||
@javax.annotation.Resource
|
||||
private FileService fileService;
|
||||
|
||||
@Value("${xinda.profile}")
|
||||
private String uploadPath;
|
||||
|
||||
@javax.annotation.Resource
|
||||
private UploadClient uploadClient;
|
||||
|
||||
|
||||
@GetMapping("/hello")
|
||||
public String hello() {
|
||||
return "hello";
|
||||
}
|
||||
|
||||
//处理文件上传的方法
|
||||
@PostMapping("/upload")
|
||||
public Map upload(MultipartFile file, HttpServletRequest request) throws IOException {
|
||||
String webPath = "upload";
|
||||
System.out.println("webPath=" + webPath);
|
||||
String webFilePath = PathUtil.appendWebPath(webPath, file.getOriginalFilename());
|
||||
System.out.println("webFilePath=" + webFilePath);
|
||||
String filePath = PathUtil.appendWebPath(uploadPath, webFilePath);
|
||||
System.out.println("filePath=" + filePath);
|
||||
Map<String, String> result = fileService.uploadReal(filePath, file);
|
||||
result.put("webUrl", webFilePath);
|
||||
return result;
|
||||
}
|
||||
|
||||
//处理文件上传的方法
|
||||
@PostMapping("/upload2")
|
||||
public Map upload2(MultipartFile file, @RequestParam("username") String username, HttpServletRequest request) throws IOException {
|
||||
String webPath = "upload";
|
||||
System.out.println("username=" + username);
|
||||
System.out.println("webPath=" + webPath);
|
||||
String webFilePath = PathUtil.appendWebPath(webPath, file.getOriginalFilename());
|
||||
System.out.println("webFilePath=" + webFilePath);
|
||||
String filePath = PathUtil.appendWebPath(uploadPath, webFilePath);
|
||||
System.out.println("filePath=" + filePath);
|
||||
Map<String, String> result = fileService.uploadReal(filePath, file);
|
||||
result.put("webUrl", webFilePath);
|
||||
return result;
|
||||
}
|
||||
|
||||
//处理文件上传的方法
|
||||
@PostMapping("/upload-array")
|
||||
public Map uploadList(MultipartFile[] files, HttpServletRequest request) throws IOException {
|
||||
String webPath = "upload";
|
||||
System.out.println("webPath=" + webPath);
|
||||
Map<String, Map> resultMap = new LinkedHashMap<>();
|
||||
for (MultipartFile file : files) {
|
||||
String webFilePath = PathUtil.appendWebPath(webPath, file.getOriginalFilename());
|
||||
System.out.println("webFilePath=" + webFilePath);
|
||||
String filePath = PathUtil.appendWebPath(uploadPath, webFilePath);
|
||||
System.out.println("filePath=" + filePath);
|
||||
Map<String, String> result = fileService.uploadReal(filePath, file);
|
||||
result.put("webUrl", webFilePath);
|
||||
resultMap.put(file.getName(), result);
|
||||
}
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/do-upload-file-path")
|
||||
public Map doUploadFilePath() throws IOException {
|
||||
Resource resource = new ClassPathResource("test-img.jpg");
|
||||
String filePath = resource.getFile().getPath();
|
||||
Map result = uploadClient.upload(filePath, progress -> {
|
||||
logger.info("-------------------------------------------------------");
|
||||
logger.info("total bytes: " + progress.getTotalBytes());
|
||||
logger.info("current bytes: " + progress.getCurrentBytes());
|
||||
logger.info("percentage: " + (progress.getRate() * 100) + "%");
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@PostMapping("/do-upload-file")
|
||||
public Map doUploadFile() throws IOException {
|
||||
Resource resource = new ClassPathResource("test-img.jpg");
|
||||
File file = resource.getFile();
|
||||
Map result = uploadClient.upload(file, progress -> {
|
||||
logger.info("-------------------------------------------------------");
|
||||
logger.info("total bytes: " + progress.getTotalBytes());
|
||||
logger.info("current bytes: " + progress.getCurrentBytes());
|
||||
logger.info("percentage: " + (progress.getRate() * 100) + "%");
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
@PostMapping("/do-upload-bytes")
|
||||
public Map doUploadBytes() throws IOException {
|
||||
Resource resource = new ClassPathResource("test-img.jpg");
|
||||
File file = resource.getFile();
|
||||
byte[] buffer = null;
|
||||
try {
|
||||
FileInputStream fis = new FileInputStream(file);
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
byte[] b = new byte[1024];
|
||||
int n;
|
||||
while ((n = fis.read(b)) != -1) {
|
||||
bos.write(b, 0, n);
|
||||
}
|
||||
fis.close();
|
||||
bos.close();
|
||||
buffer = bos.toByteArray();
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Map result = uploadClient.upload(buffer, "test-bytes.jpg");
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/do-upload-input-stream")
|
||||
public Map doUploadInputStream() throws IOException {
|
||||
Resource resource = new ClassPathResource("test-img.jpg");
|
||||
File file = resource.getFile();
|
||||
FileInputStream fis = null;
|
||||
try {
|
||||
fis = new FileInputStream(file);
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Map result = uploadClient.upload(fis, "test-input-stream.jpg");
|
||||
return result;
|
||||
}
|
||||
|
||||
@PostMapping("/do-upload-resource")
|
||||
public Map doUploadResource() {
|
||||
Resource resource = new ClassPathResource("test-img.jpg");
|
||||
Map result = uploadClient.upload(resource);
|
||||
return result;
|
||||
}
|
||||
|
||||
@PostMapping("/do-upload-multipart-file")
|
||||
public Map doUploadMultipartFile(MultipartFile multipartFile) {
|
||||
Map result = uploadClient.upload(multipartFile, multipartFile.getOriginalFilename(), progress -> {
|
||||
logger.info("-------------------------------------------------------");
|
||||
logger.info("total bytes: " + progress.getTotalBytes());
|
||||
logger.info("current bytes: " + progress.getCurrentBytes());
|
||||
logger.info("percentage: " + (progress.getRate() * 100) + "%");
|
||||
logger.info("is done: " + progress.isDone());
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
@PostMapping("/do-upload-multipart-file-list")
|
||||
public Map doUploadMultipartFileList(MultipartFile multipartFile1, MultipartFile multipartFile2) {
|
||||
// Map result = uploadClient.uploadList(
|
||||
// Lists.newArrayList(multipartFile1, multipartFile2), progress -> {
|
||||
// logger.info("-------------------------------------------------------");
|
||||
// logger.info("total bytes: " + progress.getTotalBytes());
|
||||
// logger.info("current bytes: " + progress.getCurrentBytes());
|
||||
// logger.info("percentage: " + (progress.getRate() * 100) + "%");
|
||||
// logger.info("is done: " + progress.isDone());
|
||||
// });
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/do-upload-path-list")
|
||||
public Map doUploadPathList() throws IOException {
|
||||
Resource[] resources = new Resource[]{
|
||||
new ClassPathResource("static/images/test-img.jpg"),
|
||||
new ClassPathResource("static/images/test-img2.jpg"),
|
||||
new ClassPathResource("static/images/test-img3.jpg")
|
||||
};
|
||||
List<String> pathList = new LinkedList<>();
|
||||
for (int i = 0; i < resources.length; i++) {
|
||||
pathList.add(resources[i].getFile().getPath());
|
||||
}
|
||||
Map result = uploadClient.uploadPathList(pathList);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user