FileDealController.java 6.7 KB
Newer Older
qq_53854309's avatar
qq_53854309 已提交
1 2
package com.hao.digitalsignature.controller;

H
hml 已提交
3 4 5
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hao.digitalsignature.encryption.AES1;
import com.hao.digitalsignature.encryption.AESmiyao;
H
hml 已提交
6
import com.hao.digitalsignature.encryption.DownloadMsg;
H
hml 已提交
7
import com.hao.digitalsignature.encryption.RSAEncrypt;
qq_53854309's avatar
qq_53854309 已提交
8
import com.hao.digitalsignature.entity.Download;
qq_53854309's avatar
123  
qq_53854309 已提交
9
import com.hao.digitalsignature.entity.Files;
qq_53854309's avatar
qq_53854309 已提交
10
import com.hao.digitalsignature.mapper.DownloadMapper;
H
hml 已提交
11 12
import com.hao.digitalsignature.mapper.FileMapper;
import org.springframework.beans.factory.annotation.Autowired;
qq_53854309's avatar
qq_53854309 已提交
13 14 15 16 17 18
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

H
hml 已提交
19

qq_53854309's avatar
qq_53854309 已提交
20 21
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
H
hml 已提交
22
import java.io.*;
qq_53854309's avatar
qq_53854309 已提交
23 24
import java.net.SocketException;
import java.net.URLEncoder;
H
hml 已提交
25
import java.util.*;
qq_53854309's avatar
qq_53854309 已提交
26 27 28 29

@RequestMapping("/file")
@RestController
public class FileDealController {
H
hml 已提交
30 31 32 33 34


    @Autowired
    private FileMapper fileMapper;

qq_53854309's avatar
qq_53854309 已提交
35 36 37
    @Autowired
    private DownloadMapper downloadMapper;

qq_53854309's avatar
qq_53854309 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    @RequestMapping(value = "upload")
    public String upload(@RequestParam("file") MultipartFile pic) throws SocketException, IOException {
        String newName = "";
        if (null!=pic&&pic.getSize()>0){//上传了图片
            //获取文件的原始名称
            String oname=pic.getOriginalFilename();//例如:121.jpg
            //获取文件的后缀名
            String suffix=oname.substring(oname.lastIndexOf("."));  //  .jpg
            //新的文件名
            newName= UUID.randomUUID()+suffix;


            try {
                // 获取当前项目下路径:方式一
                File file = new File("");
                String filePath = file.getCanonicalPath();
//                pic.transferTo(new File("../pic",newName));
                pic.transferTo(new File(filePath+"\\src\\main\\resources\\static\\img",newName));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return newName;
    }

    /**
     * 下载
     * @param request
     * @param response
     * @param fileName
     * @return
     */
    @RequestMapping(value = "download")
H
hml 已提交
71
    public void downFile(@RequestBody String fileName, HttpServletRequest request,
qq_53854309's avatar
qq_53854309 已提交
72 73 74
                                HttpServletResponse response) {
        // 得到要下载的文件名
        fileName = fileName.substring(0,fileName.length()-1);
H
hml 已提交
75
        try {
H
hml 已提交
76

H
hml 已提交
77
            //下载图片
qq_53854309's avatar
qq_53854309 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
            File filep = new File("");
            String filePath = filep.getCanonicalPath();
            // 上传位置
            String fileSaveRootPath =filePath+"\\src\\main\\resources\\static\\img";

            System.out.println(fileSaveRootPath + "\\" + fileName);
            // 得到要下载的文件
            File file = new File(fileSaveRootPath + "\\" + fileName);

            // 如果文件不存在
            if (!file.exists()) {
                request.setAttribute("message", "您要下载的资源已被删除!!");
                System.out.println("您要下载的资源已被删除!!");
                return ;
            }
            // 处理文件名
            String realname = fileName.substring(fileName.indexOf("_") + 1);
H
hml 已提交
95

qq_53854309's avatar
qq_53854309 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
            response.setHeader("Content-Disposition", "attachment;filename="
                    + URLEncoder.encode(realname, "UTF-8"));
            // 读取要下载的文件,保存到文件输入流
            FileInputStream in = new FileInputStream(fileSaveRootPath + "\\" + fileName);
           // response.setHeader("Location",);
            // 创建输出流
            OutputStream out = response.getOutputStream();
            // 创建缓冲区
            byte buffer[] = new byte[1024];
            int len = 0;
            // 循环将输入流中的内容读取到缓冲区当中
            while ((len = in.read(buffer)) > 0) {
                // 输出缓冲区的内容到浏览器,实现文件下载
                out.write(buffer, 0, len);
            }
            // 关闭文件输入流
            in.close();
            // 关闭输出流
            out.close();
qq_53854309's avatar
123  
qq_53854309 已提交
115

qq_53854309's avatar
qq_53854309 已提交
116 117 118 119 120
        } catch (Exception e) {
            System.out.println("error");
        }
    }

H
hml 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

    @RequestMapping(value = "/downloadMsg")
    public void exportKtrAndKjb(@RequestBody String fileName, HttpServletRequest request, HttpServletResponse response) throws Exception {
        fileName = fileName.substring(0,fileName.length()-1);

        //获取摘要
        try {

        QueryWrapper<Files> wrapper = new QueryWrapper<>();
        wrapper.eq("picture_realname", fileName.split("\\.")[0]);
        Files files = fileMapper.selectOne(wrapper);
        System.out.println("文件对象:"+files);
        System.out.println("摘要:"+files.getDig().split(";")[2]);

        List<String> dig=new ArrayList<String>(Arrays.asList(files.getDig().split(";")));
        dig.add(files.getCreatetime());
        String[] dig1=new String[dig.size()];
        dig.toArray(dig1);
        for(int i=0;i<dig1.length;i++)
            System.out.println("正确签名和时间戳"+dig1[i]);
        //生成AES密钥
        String aesPassword =AESmiyao.getKey();
qq_53854309's avatar
qq_53854309 已提交
143 144 145 146 147 148 149 150
        if(files.getPicture_realname()!=null){
            Download download=new Download(0,files.getPicture_realname(),aesPassword);
            System.out.println(download);
            downloadMapper.insert(download);
            System.out.println("成功");
        }else{
            System.out.println("有了");
        }
H
hml 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163
        //AES加密
        String encryContent= AES1.encryptAES(files.getDig().split(";")[2],aesPassword);
        //对称加密后的签名
        dig1[2]=encryContent;
        for(int i=0;i<dig1.length;i++)
            System.out.println("加密摘要的签名和时间戳"+dig1[i]);
        //发送RSA
        System.out.println(Arrays.toString(dig1));
        String rsaMsg=new String();
        for(int i=0;i<dig1.length;i++){
            rsaMsg+=dig1[i];
            rsaMsg+=";";
        }
H
hml 已提交
164
            System.out.println(rsaMsg);
H
hml 已提交
165 166 167 168 169 170 171
        byte[] rsaEnf=RSAEncrypt.RSAen(rsaMsg.substring(0,rsaMsg.length()/2));
        byte[] rsaEns=RSAEncrypt.RSAen(rsaMsg.substring(rsaMsg.length()/2));

        //RSA解密
        String rsaDef=RSAEncrypt.RSAde(rsaEnf);
        String rsaDes=RSAEncrypt.RSAde(rsaEns);
        String recRes=rsaDef+rsaDes;
qq_53854309's avatar
qq_53854309 已提交
172

H
hml 已提交
173 174 175 176 177 178 179
       DownloadMsg.downloadByStringContent(request, response, fileName.split("\\.")[0], recRes);

        }catch (Exception e) {
            System.out.println("error");
        }
    }

qq_53854309's avatar
qq_53854309 已提交
180
}