springboot项目需要先做一个初始化操作,不然会抛异常
@SpringBootApplication
public class HttpApplication {
public static void main(String[] args) {
System.setProperty("java.awt.headless","false");
SpringApplication.run(HttpApplication.class, args);
}
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
代码实现
import org.springframework.core.io.FileSystemResource;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
@Service
public class Test{
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/screenshot")
public void screenImgTask() throws Exception {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRectangle);
String fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
String tempFilePath = System.getProperty("java.io.tmpdir") + fileName + ".png";
File file = new File(tempFilePath);
ImageIO.write(image, "png", file);
System.out.println("文件名称 -> " + file.getName());
FileSystemResource resource = new FileSystemResource(file);
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType(MediaType.MULTIPART_FORM_DATA_VALUE);
headers.setContentType(type);
MultiValueMap<String, Object> bodyMap = new LinkedMultiValueMap<>();
bodyMap.add("multipartFile", resource);
bodyMap.add("screenshotId", "123");
bodyMap.add("width", image.getWidth());
bodyMap.add("high", image.getHeight());
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(bodyMap, headers);
ResponseEntity<String> responseEntity = restTemplate.exchange("http://localhost:8080/screenshot/save", HttpMethod.PUT, httpEntity, String.class);
try {
file.delete();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(responseEntity.getBody());
}
}
服务端
Controller层
@RequestMapping("screenshot/save")
public ResultVo add(@RequestBody MultipartFile multipartFile, @RequestParam String screenshotId,
@RequestParam Integer width, @RequestParam Integer high) {
if (multipartFile == null) {
return ResultVo.error("图片不能为空");
}
return screenShotService.add(multipartFile, taskId, width, high);
}
Service层
@Override
public ResultVo add(MultipartFile multipartFile, String screenshotId, Integer width, Integer high) {
if (StringUtils.isNull(taskId)) {
return null;
}
ScreenshotEntity screenShotEntity = this.getById(screenshotId);
if (screenShotEntity == null) {
return ResultVo.error("该截屏记录不存在");
}
screenShotEntity.setScreenshotWidth(width);
screenShotEntity.setScreenshotHeight(high);
screenShotEntity.setScreenshotSize(multipartFile.getSize());
screenShotEntity.setCreatedAt(new Date());
screenShotEntity.setUpdatedAt(new Date());
String url = screenShotEntity.getScreenshotUrl() + multipartFile.getOriginalFilename();
File file = new File(url);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
try {
file.createNewFile();
} catch (IOException e) {
logger.error("文件 [{}] 创建失败", url);
e.printStackTrace();
}
}
try {
multipartFile.transferTo(file);
screenShotEntity.setScreenshotUrl(url);
boolean update = this.updateById(screenShotEntity);
if (!update) {
return ResultVo.error("修改截屏记录失败");
}
return ResultVo.ok();
} catch (IOException e) {
e.printStackTrace();
return ResultVo.error("保存截屏图片失败");
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通