# thepicbed **Repository Path**: a--designer/thepicbed ## Basic Information - **Project Name**: thepicbed - **Description**: 自定义图床项目 项目的方案:Spingboot + picGo + 云服务器 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-09-21 - **Last Updated**: 2023-09-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 自定义图床项目 项目方案 PicGO + 华为云服务器 + Springboot 1. 什么是图床,它有什么作用? 图床就是将图片上传到服务器中,许多互联网公司比如华为云都有OBS的云存储服务。我们在使用md编写的笔记时候本地图片常常把本地文件夹搞得乱糟糟的,或在进行微信小程序开发时文件怎么打包都因文件过大而无法部署上线,这个时候我们就需要将大文件或者图片上传到服务器的文件夹中,通过直链的方式来访问该资源。 所以总结一下,图床做了两件事。 1. 文件上传 2. 生成文件的访问直链 - 项目配置 ```java #打开文件传输端口 server: port:9999 ``` - 图床项目 ```java @RequestMapping("/uploadImg") public JSONObject uploadImg(HttpServletRequest request, @RequestPart("file") MultipartFile file, @RequestParam("imgPath") String imgPath, @RequestParam("ipAddress") String ipAddress ) { JSONObject jsonObject = new JSONObject(); try { String ip = getIpAddress(request); // 请求值不能为空 if (Objects.nonNull(file) && StringUtils.isNoneBlank(imgPath) && StringUtils.isNoneBlank(ipAddress)) { Date date = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd"); String format = formatter.format(date); String fileName = file.getOriginalFilename(); log.info("{}--{}", fileName, fileName.substring(fileName.lastIndexOf(".") + 1)); fileName = imgPath + format + "." + fileName.substring(fileName.lastIndexOf(".") + 1); File uploadFile = new File(fileName); //保存文件,保存的文件夹必须存在且要求绝对路径 file.transferTo(uploadFile); jsonObject.put("path", ipAddress + fileName); log.info("来自ip:{}的请求,文件上传路径为:{}", ip, ipAddress + fileName); } } catch (Exception e) { // log 中的异常用 e.toString() 而不是 e.getMessage() log.error("uploading is error:", e); } return jsonObject; } ```