当前位置:网站首页>Practice of online problem feedback module (12): realize image deletion function
Practice of online problem feedback module (12): realize image deletion function
2022-07-24 06:55:00 【Bug bacteria ¹】
author :bug bacteria
official account : Ape circle wonderful house
important clause : Originality is not easy. , For reprint, please attach the original source link and this statement , Thank you for your cooperation .
Copyright notice : Some words or pictures in the article may come from the Internet or Baidu Encyclopedia , If there is infringement, please contact bug Bacterial treatment .
One 、 summary
The next few issues ,bug I want to share with you that I just received a temporary demand yesterday , It's hot , I would like to share how I faced the temporary requirements and made the whole development cycle , It includes sorting out businesses, creating business tables, realizing business logic to form a closed loop, and connecting with the front end , Some business development and functional development will be interspersed , This one-stop process is witnessed online with you , Share with the novice , I hope it will help you .
Environmental statement :idea2019.3 + springboot2.3.1.REALSE + mybati-plus3.2.0 + mysql5.6 + jdk1.8
If the partners feel that the article is helpful to you in the process of reviewing the article , Please don't be stingy with your praise , Boldly put the article Lighten up Well , Your likes are three in a row ( Collection ️+ Focus on + Leaving a message. ) That's right bug The best encouragement and support on my creative path . Time does not give up *️, Create constantly , come on. ️
Two 、 Text
This issue , I then focus on the picture , Since we want to teach , It is necessary to complete the addition, deletion, modification and check of the pictures . So for you in the future , If there is any business related to pictures , Then you have no pressure , Follow my tutorial directly , I'll teach you hand in hand , This must be the fastest and best zero foundation introductory teaching , Maybe they will be appreciated by the leaders , Work , It's not just about code safety and efficiency , Get off work early , Try to finish the workload of the day within working hours , This is the goal of everyone , It's yours and mine , Let's cheer together ! Ok or not ?
Let's not talk more nonsense , Start today's content directly .
3、 ... and 、 How to code image deletion
Speaking of the contents of these issues , Addition, deletion, modification and check of pictures , The only thing left is to delete the picture , We talked about the other three function points in the previous issues , For example, picture upload 、 Images are downloaded 、 Picture preview online , And this issue , Let's finish it , Put the rest of the specified deletion function of the picture to the implementation . This piece of picture business is closed-loop , You can upload to the server and delete the image resources of the server , Right .
Talk about these small function points , Although it is not directly related to the business , But when it comes to business scenarios such as picture resources , The addition, deletion and modification of this picture are indispensable , So , I will tell you about these small function points word by word , I hope you will meet with this business in the future , If you can help .
I don't say much nonsense , Let's begin today's lesson .
1️⃣ Definition Controller request
We're still the same old way , Define the request interface first , The function node is image deletion , Those parameters are required ? According to the business analysis, a wave can be obtained , I will tell you about my actual business scenario . For example, my business scenario is , Users can edit their own feedback , At the same time, you can manually delete the attached pictures , In other words, you can edit the contents of common fields , You can also remove attached pictures , Then I will separate this point , The picture needs to remove the direct call interface 1, Feedback problem editing call interface 2, Right , In this way, there is no need to organize the business into a complex business logic that is saved together with the problem feedback at that time .
so, Let's go on , Let's go straight to the code !
@GetMapping("/delete-img-by-path")
@ApiOperation(value = " Picture deletion ", notes = " Delete the picture according to the picture address ")
public ResultResponse<Boolean> deleteImgByPath(@ApiParam(" Picture path ") @RequestParam("imgPath") String imgPath,
@ApiParam(" Domain account number of the feedback person id") @RequestParam("accountId") String accountId,
@ApiParam(" The primary key of this data id") @RequestParam("id") String id) {
return userQuestionsService.deleteImgByPath(imgPath, accountId, id);
}2️⃣ Defining interfaces deleteImgByPath()
The next step is to define the deleteImgByPath() The interface . The specific implementation code is as follows :
ResultResponse<Boolean> deleteImgByPath(String imgPath, String accountId,String id);
3️⃣ Realization deleteImgByPath() Method
Here is the key point of image deletion , You should know the logic of batch saving my pictures , In the data filePaths Fields are saved with commas separated by multiple picture paths , So for the need to remove filePaths A path in , Then we need to delete the path, specify the path to be removed and update the record , That is, write the removed path back to the record .
therefore , We write this method , First of all : Update the removed path address , second : Remove server files . The premise of running the second step is that the first step must be successful , Because the first step is more error prone , For the second step, even if the removal fails , You can also conduct a regular scan to clean up useless resources in the future , Even if deleting the file fails , That is, it takes up a little server memory resources , It doesn't affect anything , Because the user wants the removal to be successful , Therefore, the success rate of submission and removal , We just update the path first , Remove the entity file after .
Then I will take you all to implement this method .
@Override
public ResultResponse<Boolean> deleteImgByPath(String imgPath, String accountId, String id) {
// First query this data
UserQuestionsEntity entity = this.getById(id);
if (Objects.isNull(entity)) {
return new ResultResponse<>(ErrorCodeEnum.DATA_NOT_EXIST);
}
// Remove the deleted path
String filePaths = entity.getFilePaths();
if (StringUtils.isBlank(filePaths)) {
return new ResultResponse<>(ErrorCodeEnum.FILE_NOT_FIND);
}
// Turn into str Array
String[] paths = filePaths.split(ConstantUtils.COMMA);
// Remove the file path to delete
// First, determine whether the target path is in the specified set
boolean isExist = ReviewStringUtils.isExist(paths, imgPath);
if (!isExist) {
return new ResultResponse<>(ErrorCodeEnum.FILE_NOT_FIND);
}
String pathStr = ReviewStringUtils.removeStr(paths, imgPath);
// Update the image path after removing the image
entity.setFilePaths(pathStr);
// Update data
boolean success = this.updateById(entity);
// If the update is successful, the file will be removed
if (success) {
// Specify the delete target full path
String targetPath = this.buildImgPath(imgPath, accountId);
uploader.removeFile(new File(targetPath));
return new ResultResponse<>(success);
}
return new ResultResponse<>(ErrorCodeEnum.DELETE_IMAGE_ERROR);
}Explain it. , about filePaths Field , We can directly use split Split with commas into an array of paths , Then match whether there is a target path from the array , Then select to return some business judgments about , For example, whether the file exists .
4️⃣ Realize the picture deleteImgByPath() Delete method
For this removeFile() Method , This is a little bit easier , You can directly use file Provided by the delete() Method , The picture resource can be effectively deleted . This is very convenient and advantageous .
public Boolean removeFile(File file) {
// Delete directly
return file.delete();
}5️⃣ The interface test
As for the above interfaces, they are all defined and implemented , So the remaining thing to do now is , unit testing + The interface test . We are more concerned about whether the function node can really meet the business needs , If the business needs cannot be reached , Then we have to modify or rewrite , It's possible , So when we finish writing an interface , Be sure to do a good test , Don't let other colleagues, such as testers 、 Front end personnel create extra workload .
We can go directly online swagger Document or postman Debug the interface .
postman Test file delete interface
This is a nonexistent file path that I can pass , Deleting must be a direct return to customization msg.

I give a path address to exist , Delete pictures . The demonstration results are as follows :

as for swagger file , The same operation demonstration , I'm not going to show you one by one . If you are not clear, you can ask in the comment area , I must have answered it at the first time .
6️⃣ summary
Relatively speaking , It's still one step , Nothing happened bug, This measurement function is available , You can rest assured copy Ha .
... ...
All right. , The above is all about this issue , Have you learned to give up ? If it helps you , Please don't forget to give bug bacteria [ Three companies support ] yo . If you want to get more learning resources or want to communicate with more technology enthusiasts , You can pay attention to my official account. 『 Ape circle wonderful house 』, The backstage replies the key words to get the learning materials 、 Big factory surface 、 Interview templates and other massive resources , Just wait for you to get .
Four 、 I recommend
For the actual development of the problem feedback module , I combed the teaching and link address of each issue completely , For reference only : I hope it can help you .
- Practice of online problem feedback module ( One ): Sort out business requirements and create database tables
- Practice of online problem feedback module ( Two ): Encapsulate code to automatically generate class file
- Practice of online problem feedback module ( 3、 ... and ): Automatically generate all Controller、Service、Mapper Wait for the documents
- Practice of online problem feedback module ( Four ): Encapsulate generic field classes
- Practice of online problem feedback module ( 5、 ... and ): Realize the automatic filling function of general field content
- Practice of online problem feedback module ( 6、 ... and ): Interface document definition
- Practice of online problem feedback module ( 7、 ... and ): Installation and deployment swagger2
- Practice of online problem feedback module ( 8、 ... and ): Realize image upload function ( On )
- Practice of online problem feedback module ( Nine ): Realize image upload function ( Next )
- Practice of online problem feedback module ( Ten ): Realize the picture preview function
- Practice of online problem feedback module ( 11、 ... and ): Realize the picture download function
- Practice of online problem feedback module ( Twelve ): Realize the function of deleting pictures
- Practice of online problem feedback module ( 13、 ... and ): Realize multi parameter paging query list
- Practice of online problem feedback module ( fourteen ): Realize the online question answering function
- Practice of online problem feedback module ( 15、 ... and ): Realize the function of online updating feedback status
- Practice of online problem feedback module ( sixteen ): Realize the function of checking details
- Practice of online problem feedback module ( seventeen ): Realization excel Template online download function
- Practice of online problem feedback module ( eighteen ): Realization excel Batch import function of account file records
- Practice of online problem feedback module ( nineteen ): Realize batch export of data to excel Function in file
- Practice of online problem feedback module ( twenty ): Conclusion
The above is the content of 20 issues , Each issue is dry , For the development of a module , How to build and test the deployment online bit by bit , I'll say it again , This is not an exercise , It's actual combat ! It's actual combat ! It's actual combat !
If you think you just need to understand one of the knowledge points or business , No objection , Just choose a few of them to study , It's all over anyway ; I just hope you can gain something , Grow up , It's not in vain for me to summarize and update you after work every day .
5、 ... and 、 At the end of the article
If you want to learn more , Little friends can pay attention to bug I created a special column for you 《springboot Zero basic introductory teaching 》, It's all my hands , Ongoing update , I hope I can help more friends .
I am a bug bacteria , A procedural ape who wants to get out of the mountain and change his fate . There's a long way to go , Are waiting for us to break through 、 To challenge . Come on , friends , Let's cheer together ! The future can be expected ,fighting!
Finally, I'll give you two words I like very much , Share with you !
️ Be what you want to be , No time limit , As long as willing , Anytime? start.
You can change... From now on , It can be the same , This matter , There are no rules , You can live your best .

If the article helps you , Just leave your Fabulous Well !(#^.^#);
If you like bug Sharing articles , Just... Please bug Bacteria point Focus on Well !(๑′ᴗ‵๑)づ╭~;
If you have any questions about the article , Please also at the end of the text Leaving a message. perhaps Add group Well 【QQ Communication group :708072830】;
Given the limited personal experience , All viewpoints and technical research points , If there is any objection , Please reply directly to participate in the discussion ( Do not make offensive remarks , thank you );
Copyright notice : Originality is not easy. , For reprint, please attach the original source link and this statement , copyright , Piracy must be investigated !!! thank you .
边栏推荐
- mysql自动生成创建时间和更新时间
- HashSet to array
- Redis特殊数据类型-BitMap
- Breadth first search (template use)
- [lvgl (2)]
- 【C语言】操作符详解(深入理解+整理归类)
- kubernetes简介(kubernetes优点)
- Today, let's talk about the underlying architecture design of MySQL database. How much do you know?
- 使用root用户为创建新用户并设置密码
- [learning notes] possible reasons and optimization methods for white screen on Web pages
猜你喜欢
随机推荐
别太在意别人的眼光,那会抹杀你的光彩
mysql获取自增行标(区别mysql版本)
tensorflow scatter_nd函数
Sealos packages and deploys kubesphere container platform
永远不要迷失自我!
Introduction to kubernetes (kubernetes benefits)
不要太在意别人对你的看法
STM32 MP3 music player based on FatFs r0.14b & SD card (also a simple application of FatFs)
Record the pits encountered in the deserialization of phpserializer tool class
Geek planet ByteDance one stop data governance solution and platform architecture
Random forest, lgbm parameter adjustment based on Bayesian Optimization
[lvgl layout] grid layout
[lvgl layout] flexible layout
ADB interaction - kill the ugly default shell interface
Sealos 打包部署 KubeSphere 容器平台
Camera Hal OEM模块 ---- cmr_grab.c
Special effects - click with the mouse and the fireworks will burst
数据分析思维之从整体出发分析零售行业——全方位多方面细节分析
mysql自动生成创建时间和更新时间
MGR_mysqlsh_keepalive高可用架构部署文档








