downlodcontroller
package com.xc.project.controller;import com.xc.project.common.ErrorCode;import com.xc.project.exception.BusinessException;import com.xc.project.service.UserService;import com.xc.xcapicommon.model.entity.User;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.core.io.InputStreamResource;import org.springframework.core.io.Resource;import org.springframework.http.HttpHeaders;import org.springframework.http.ResponseEntity;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest;import java.io.*;import java.nio.file.Files;/** * @author xc * @date 2023/4/19 15:35 */@RestController@Slf4j@RequestMapping("download")public class DownloadController { @Autowired private UserService userService;// 在服务器路径下载文件,这样写是因为我只有一份文件,资源实际上可以根据访问路径进行匹配 private static final String SDK_HELP_ADDRESS = "/home/XC/download/xcapi-client-sdk.zip"; @GetMapping("sdkHelp") public ResponseEntity<Resource> downloadSdkHelp(HttpServletRequest request) throws IOException { // 请求不能是null if (request == null) { throw new BusinessException(ErrorCode.FORBIDDEN_ERROR); } // 当前登录用户必须存在 User loginUser = userService.getLoginUser(request); if (loginUser == null || loginUser.getId() == null) { throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR); } ///获取要下载的文件 File file = new File(SDK_HELP_ADDRESS); InputStream is = Files.newInputStream(file.toPath()); //获取文件输入流 //创建资源对象,加载输入流 InputStreamResource resource = new InputStreamResource(is); //设置响应头,指定文件名称 HttpHeaders headers = new HttpHeaders(); // 只有设置正确的响应头,客户端才知道此时是文件下载 headers.add("Content-Disposition", String.format("attachment; filename=%s", file.getName())); ///下载返回文件响应 return ResponseEntity.ok().headers(headers).contentLength(file.length()).body(resource); }}
前端react页面
import {PageContainer,} from '@ant-design/pro-components';import { Button, Descriptions, Form,} from 'antd';import { getEncryptUsingGET,} from "@/services/xcapi-backend/userController";import {useState} from "react";import {decrypt} from "@/utils/ComplexEncryption";/** * 帮助页面调用SDK * @constructor */const Index: React.FC = () => { const loadData = async () => { const {data} = await getEncryptUsingGET(); return data; } let [encryptAk, setEncryptAk] = useState(''); let [encryptSk, setEncryptSk] = useState(''); loadData().then(data => { console.log(data?.encryptAk) setEncryptAk(decrypt(data?.encryptAk) setEncryptAk(decrypt(data?.encryptAk)); setEncryptSk(decrypt(data?.encryptSk)); }) return ( <PageContainer title=“本地调用帮助文档”> <> {} <Descriptions title={"不能透露"}"> <Descriptions.Item label="AccessKey">{encryptAk}</Descriptions.Item> <Descriptions.Item label="secretKey">{encryptSk}</Descriptions.Item> </Descriptions> <Form name='findHelp' layout='vertical' > // 通过buttonhref下载服务器的压缩包 <Button href={"http://xcapi-backend.wangcheng.love/api/download/sdkHelp"} type='primary' htmlType='submit'> 下载SDK </Button> </Form> </> </PageContainer> );};export default Index;