@[toc]
shareX是一個很方面的截圖工具,可一鍵實現截圖->上傳圖牀->生成連接 等工做流,堪稱截圖神器,惟一美中不足的是,發現使用一段時間後,軟件自帶的幾個圖牀總是掛掉,前期辛苦寫的文章圖片都404了,在頻繁更換可用圖牀的過程當中,偶然的發現原來shareX竟是支持自定義圖牀的,這就簡單了,本身寫一個圖牀後臺,掛到本身的服務器上就不再拍圖牀莫名其妙的掛掉了java
1.後臺提供上傳文件的http接口spring
2.shareX上傳圖片到後臺json
3.後臺返回上傳圖片外部可訪問路徑給shareX安全
好了 開始動手服務器
採用spring boot 來實現:mybatis
spring: mvc: static-path-pattern: /** resources: static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${serverInfo.uploadpath} servlet: multipart: max-file-size: 10MB #最大文件 10M max-request-size: 10MB jmx: default-domain: sharex serverInfo: # baseurl項目部署後的訪問地址 baseurl: http://192.168.1.188:8080/sharex/ #uploadpath爲上傳文件的保存路徑 uploadpath: c:\sharex\uploadFile\ server: port: 8080 servlet: context-path: /sharex
主要配置項:mvc
uploadpath:上傳圖片保存路徑,部署時要按需修改;app
baseurl:返回圖片RUL的根路徑;dom
max-file-size,max-request-size:設置上傳文件大小限制;工具
static-locations:設置要暴露的靜態資源路徑,不設置的話上傳的圖片訪問不到;
/** * @Author: wanggang.io * @Date: 2019/5/8 14:05 * @todo */ @RestController @RequestMapping("pic") public class UploadController { @Autowired private LocalConfig localConfig; @Autowired private FileUploadService fileUploadService; @PostMapping("upload") public ResultMsg uploadFile(String jessionids , MultipartFile file) { if(jessionids == null || jessionids.isEmpty()){ return ResultMsg.getFailedMsg("jessionids 不能爲空"); } //爲了安全,服務端要嚴驗證jessionids,jessionids是一串隨機數 if(!jessionids.equals("9be51d2f-bb1a-4c61-b989-c3d126211d661570588000594")) return ResultMsg.getFailedMsg("jessionids 錯誤"); long size = (long) file.getSize(); if (size > localConfig.getMaxFileSize()) { return ResultMsg.getMsg("上傳文件過大,請上傳小於10MB大小的文件"); } ResultMsg resultMsg = fileUploadService.uploadFile("shareX", file); if (resultMsg.getResult() != "SUCCESS") { return ResultMsg.getFailedMsg("保存文件失敗"); } FileInfo fileInfo = (FileInfo) resultMsg.getData(); resultMsg.setData(fileInfo); return resultMsg; } }
接口的請求路徑爲:http://192.168.1.188:8080/sharex/pic/upload
/** * @Author: wanggang.io * @Date: 2019/5/8 14:01 * @todo */ @Service public class FileUploadService { @Autowired private LocalConfig localConfig; public ResultMsg uploadFile(String sendId, MultipartFile file){ String sepa = File.separator; String fileName = file.getOriginalFilename(); String fileNameSuffix = fileName.substring(fileName.lastIndexOf(".") + 1); String fileNamePrefix = fileName.substring(0 , fileName.lastIndexOf(".")); String timeStr = ""+ DateUtils.getDayStr(new Date()); String newFileName = fileNamePrefix + "-"+ sendId +"-"+ DateUtils.getDateStr(new Date()) + "." + fileNameSuffix; String baseFilePath = localConfig.getUploadFilePath(); String path = baseFilePath + sendId+ sepa + timeStr; createDirectory(path); File targetFile = new File(path + sepa + newFileName); try { //將上傳文件寫到服務器上指定的文件夾 file.transferTo(targetFile); String saveUrl =localConfig.getBaseurl()+ sendId+ "/" + timeStr + "/" + newFileName; FileInfo fileInfo = new FileInfo(newFileName,fileNameSuffix,saveUrl,fileName); return ResultMsg.getMsg(fileInfo); } catch (IOException e) { e.printStackTrace(); return ResultMsg.getFailedMsg(e.getMessage()); } } /**建立目錄**/ public void createDirectory(String directoryPath){ File targetFile = new File(directoryPath); if (!targetFile.exists()) { targetFile.mkdirs(); } } }
FileInfo.java
@Data public class FileInfo { public FileInfo(){} public FileInfo(String fileName, String fileType, String fileUrl, String oldFileName){ this.fileName = fileName; this.fileType= fileType; this.fileUrl = fileUrl; this.originFileName = oldFileName; } public String fileName; public String fileType; public String fileUrl; public String originFileName; }
啓動程序測試了一下,接口返回信息以下:
{ "data": { "fileName": "Test-shareX-20191009133656.png", "fileType": "png", "fileUrl": "http://192.168.1.188:8080/sharex/shareX/20191009/Test-shareX-20191009133656.png", "originFileName": "Test.png" }, "result": "SUCCESS", "resultCode": 200, "resultMsg": "" }
這裏我shareX的版本是:13.0.1 其餘版本的配置相似
點擊上傳至->自定義上傳目標
配置請求參數以下:
響應參數配置以下:
響應消息的URL配置格式參見接口返回消息格式
點擊左側的測試,看下shareX可否正常解析返回的圖片RUL
能夠看到解析正常。
若是不正常,請根據事情狀況調整請求和響應參數配置
自定義圖牀配置好了之後,接着還要設置shareX當前上傳圖片的圖牀爲剛纔咱們自定義的圖牀,設置方法爲:
至此,shareX自定義圖牀配置完成!
歡迎你們交流!!!