Springboot+Vue实现Base64编码与解码

HTML

 1 <template>
 2   <nav-bar/>
 3   <div class="body">
 4 
 5     <div style="margin: 0 auto;width: 100%;height: auto;text-align: center">
 6       <h2>Base64加密</h2>
 7     </div>
 8     <div class="request_outer">
 9       <el-input
10           v-model="requestContent"
11           :autosize="{ minRows: 10, maxRows: 20 }"
12           type="textarea"
13           placeholder="编码内容"
14       />
15     </div>
16 
17     <div style="margin: 15px auto 15px auto;width: 100%;height: auto;text-align: center">
18       <el-button type="danger" @click="fun_Base64_Encode()">编码</el-button>
19       <el-button type="primary" @click="fun_Base64_Decoder()">解码</el-button>
20       <el-button type="info" @click="fun_QingKong()">清空</el-button>
21     </div>
22 
23 
24     <div class="response_outer">
25       <el-input
26           v-model="responseContent"
27           :autosize="{ minRows: 20, maxRows: 40 }"
28           type="textarea"
29           placeholder="解码结果"
30       />
31     </div>
32 
33   </div>
34 </template>
axios请求
 1 <script>
 2 import {ElLoading} from "element-plus";
 3 import axios from "axios";
 4 
 5 export default {
 6   name: "Base64",
 7   data() {
 8     return {
 9       requestContent: "",
10       responseContent: ""
11     }
12   },
13   methods: {
14 
15     //编码
16     fun_Base64_Encode() {
17 
18       if (this.requestContent.trim() === '') {
19         this.$message.error('输入不能为空')
20       } else {
21         //全屏加载
22         const loading = ElLoading.service({
23           lock: true,
24           text: '请求中...',
25           background: 'rgba(0, 0, 0, 0.7)',
26         })
27         //提交
28         const _this = this;
29         axios({
30           method: "post",
31           url: "http://localhost:8080/tools/encryption/Base64_Encode",
32           data: {input: _this.requestContent},
33           headers: {
34             "Content-Type": "application/json"
35           }
36         }).then(result => {
37           if (result.status === 200) {
38             console.log(result);
39             loading.close();//关闭全屏加载
40             _this.responseContent = String(result.data.result);
41           }
42         }).catch(error => {
43           console.log(error);
44           loading.close();//关闭全屏加载
45           _this.responseContent = error.toString();
46         });
47       }
48 
49     },
50 
51     //解码
52     fun_Base64_Decoder() {
53       if (this.requestContent.trim() === '') {
54         this.$message.error('输入不能为空')
55       } else {
56         //全屏加载
57         const loading = ElLoading.service({
58           lock: true,
59           text: '请求中...',
60           background: 'rgba(0, 0, 0, 0.7)',
61         })
62         //提交
63         const _this = this;
64         axios({
65           method: "post",
66           url: "http://localhost:8080/tools/encryption/Base64_Decoder",
67           data: {input: _this.requestContent},
68           headers: {
69             "Content-Type": "application/json"
70           }
71         }).then(result => {
72           if (result.status === 200) {
73             console.log(result);
74             loading.close();//关闭全屏加载
75             _this.responseContent = result.data.result;
76           }
77         }).catch(error => {
78           console.log(error);
79           loading.close();//关闭全屏加载
80           _this.responseContent = error.toString();
81         });
82       }
83     },
84 
85     //清空
86     fun_QingKong() {
87       this.responseContent = '';
88       this.requestContent = '';
89     }
90   }
91 }
92 </script>

后端Interface

1 public interface IEncryptionService {
2 
3     //Base64编码
4     public String Base64_Encode(String input) throws UnsupportedEncodingException;
5 
6     //Base64解码
7     public String Base64_Decoder(String input) throws IOException;
8 }

后端实现类impl

 1 package com.example.demo.itkb.tools.encryption.service.impl;
 2 
 3 import com.example.demo.itkb.tools.encryption.service.IEncryptionService;
 4 import org.springframework.stereotype.Service;
 5 import java.nio.charset.StandardCharsets;
 6 import java.util.Base64;
 7 
 8 
 9 @Service
10 @SuppressWarnings("all")
11 public class EncryptionServiceImpl implements IEncryptionService {
12     final static Base64.Encoder encoder = Base64.getEncoder();
13     final static Base64.Decoder decoder = Base64.getDecoder();
14 
15     /**
16      * Base64编码
17      *
18      * @param input
19      * @return
20      */
21     @Override
22     public String Base64_Encode(String input) {
23         String result = null;
24         try {
25             result = encoder.encodeToString(input.getBytes(StandardCharsets.UTF_8));
26         } catch (Exception e) {
27             e.printStackTrace();
28             result = e.getMessage().toString();
29         }
30         return result;
31     }
32 
33 
34     /**
35      * Base64解码
36      *
37      * @param input
38      * @return
39      */
40     @Override
41     public String Base64_Decoder(String input) {
42         String result = null;
43         try {
44             byte[] decodedBytes = Base64.getDecoder().decode(input);
45             result = new String(decodedBytes);
46         } catch (Exception e) {
47             e.printStackTrace();
48             result = e.getMessage().toString();
49         }
50         return result;
51     }
52 }

后端Controller

package com.example.demo.itkb.tools.encryption.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.itkb.tools.encryption.service.IEncryptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin //支持跨域
@SuppressWarnings("all")
@RestController
@RequestMapping("/tools/encryption")
public class EncryptionController {

    @Autowired
    IEncryptionService iEncryptionService;


    /**
     * Base64编码
     *
     * @param jsonObject
     * @return
     * @throws UnsupportedEncodingException
     */
    @PostMapping(value = "Base64_Encode")
    public Map<String, String> Base64_Encode(@RequestBody JSONObject jsonObject) throws UnsupportedEncodingException {
        System.out.println(jsonObject);
        String input = jsonObject.getString("input");
        String result = iEncryptionService.Base64_Encode(input);
        Map<String, String> resultMap = new HashMap<String, String>();
        resultMap.put("result", result);
        return resultMap;
    }


    /**
     * @param jsonObject
     * @return
     * @throws IOException
     * @RequestBody注解,接收json数据,请求时使用“data" 不能使用parms参数
     */
    @PostMapping(value = "Base64_Decoder")
    public Map<String, String> Base64_Decoder(@RequestBody JSONObject jsonObject) throws IOException {
        System.out.println(jsonObject);
        String input = jsonObject.getString("input");
        String result = iEncryptionService.Base64_Decoder(input);
        Map<String, String> resultMap = new HashMap<String, String>();
        resultMap.put("result", result);
        return resultMap;
    }
}

运行效果(编码)

 

运行效果(解码)

 

posted @ 2022-11-12 14:35  勤快的懒羊羊  阅读(1232)  评论(0编辑  收藏  举报