首页 > 其他分享 >公文传输系统代码

公文传输系统代码

时间:2023-12-17 20:56:04浏览次数:36  
标签:gw 公文 file String 代码 传输 File new import

`package com.fq.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;

@Data
public class Gw {
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private String uname;//名字
private Integer uid;
private String file;
private String type;
@TableField("key")
private String key;
private String date;
private String uname2;//发送名字
private Integer uid2;
private String state;
private String file2;

}
`

`package com.fq.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;

//用户
@Data
public class User {
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private String name;//名字
private String pwd;//密码
private String perms;//权限
// private double balance;

}
`

`package com.fq.vo;

import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class DataVo{
private Integer code;
private String msg; //chu
private Long count;//共几条
private T data;
}`

`package com.fq.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fq.entity.Gw;
import com.fq.entity.User;
import com.fq.mapper.GwMapper;
import com.fq.mapper.UserMapper;
import com.fq.utils.SM4Decryptor;
import com.fq.utils.SM4Encryptor;
import com.fq.vo.DataVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Base64;
@Controller
@RequestMapping("user")
public class UserController {
@Autowired
UserMapper userMapper;
@Autowired
GwMapper gwMapper;

@RequestMapping("index")
public String index(){
    return "user/index";
}
//个人管理
@RequestMapping("my")
public String user(){
    return "user/my";
}
@ResponseBody
@RequestMapping("userList")
public DataVo userList(@RequestParam(value = "page",defaultValue = "1") Integer page, @RequestParam(value = "limit",defaultValue = "10") Integer limit){
    QueryWrapper<User> ordersQueryWrapper=new QueryWrapper<>();
    Subject subject = SecurityUtils.getSubject();
    User currentUser=(User)subject.getPrincipal();//当前登录对象
    ordersQueryWrapper.eq("id",currentUser.getId());
    IPage<User> page1=new Page<>(page,limit);//每页放几条

    IPage<User> labelIPage = userMapper.selectPage(page1, ordersQueryWrapper);//页数
    return DataVo.builder().code(0).msg("成功").data(labelIPage.getRecords()).count(labelIPage.getTotal()).build();
}
@RequestMapping("toUserEdit")
public String toUserEdit(){
    return "user/service/userEdit";
}

@RequestMapping("userEdit")
@ResponseBody
public Map<String,Object> userEdit(@ModelAttribute("User")User user){
    SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss");
    Date date = new Date(System.currentTimeMillis());

    User user1 = userMapper.selectById(user.getId());

    user1.setPwd(user.getPwd());

    Map<String,Object> map=new HashMap<>();

    userMapper.updateById(user1);
    map.put("code",1);
    return map;
}



//公文
@RequestMapping("gw")
public String gw(){
    return "user/gw";
}
@ResponseBody
@RequestMapping("gwList")
public DataVo gwList(@RequestParam(value = "page",defaultValue = "1") Integer page, @RequestParam(value = "limit",defaultValue = "10") Integer limit){
    Subject subject = SecurityUtils.getSubject();
    User currentUser=(User)subject.getPrincipal();//当前登录对象
    QueryWrapper<Gw> ordersQueryWrapper=new QueryWrapper<>();
    ordersQueryWrapper.eq("uid",currentUser.getId());
    IPage<Gw> page1=new Page<>(page,limit);//每页放几条

    IPage<Gw> labelIPage = gwMapper.selectPage(page1, ordersQueryWrapper);//页数
    return DataVo.builder().code(0).msg("成功").data(labelIPage.getRecords()).count(labelIPage.getTotal()).build();
}

// @ResponseBody
// @RequestMapping("gw_search")
// public DataVo gw_search(@RequestParam(value = "page",defaultValue = "1") Integer page, @RequestParam(value = "limit",defaultValue = "10") Integer limit,
// String name){
// IPage page1=new Page<>(page,limit);//每页放几条
// QueryWrapper bookQueryWrapper = new QueryWrapper<>();
// bookQueryWrapper.like(
// "uname",name
// );
// IPage resourceIPage = gwMapper.selectPage(page1, bookQueryWrapper);//页数
// return DataVo.builder().code(0).msg("成功").data(resourceIPage.getRecords()).count(resourceIPage.getTotal()).build();
// }
@RequestMapping("toGwAdd")
public String toGwAdd(){
return "user/service/gwAdd";
}
@RequestMapping("gwAdd")
@ResponseBody
public Map<String,Object> gwAdd(@ModelAttribute("Gw")Gw gw) throws InvalidCipherTextException, IOException {
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd '-' HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
Subject subject = SecurityUtils.getSubject();
User currentUser=(User)subject.getPrincipal();//当前登录对象

    gw.setUid(currentUser.getId());
    gw.setUname(currentUser.getName());
    QueryWrapper<User> ordersQueryWrapper=new QueryWrapper<>();
    ordersQueryWrapper.eq("name",gw.getUname2());
    User user = userMapper.selectOne(ordersQueryWrapper);

gw.setDate(formatter.format(date));
gw.setUid2(user.getId());
if (gw.getState().equals("加密")){
Map<String, Object> stringObjectMap = saveFile2(gw.getFile());

        gw.setKey((String) stringObjectMap.get("key"));
        gw.setFile2((String) stringObjectMap.get("file"));
    }


    Map<String,Object> map=new HashMap<>();

// lt.setDate(formatter.format(date));
gwMapper.insert(gw);
map.put("code",1);
return map;
}

@RequestMapping("toGwEdit")
public String toGwEdit(){
    return "user/service/gwEdit";
}

@RequestMapping("gwEdit")
@ResponseBody
public Map<String,Object> gwEdit(@ModelAttribute("Gw")Gw gw) throws InvalidCipherTextException, IOException {
    SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss");
    Date date = new Date(System.currentTimeMillis());


    Gw gw1 = gwMapper.selectById(gw.getId());
    if (!ObjectUtils.isEmpty(gw.getFile())){
        gw1.setFile(gw.getFile());
    }
    gw1.setType(gw.getType());
    gw1.setState(gw.getState());
    if (gw.getState().equals("加密")){
        Map<String, Object> stringObjectMap = saveFile2(gw1.getFile());

        gw1.setKey((String) stringObjectMap.get("key"));
        gw1.setFile2((String) stringObjectMap.get("file"));
    }

    if (gw.getState().equals("公开")){


        gw1.setKey(null);
        gw1.setFile2(null);
    }


    Map<String,Object> map=new HashMap<>();

    gwMapper.updateById(gw1);
    map.put("code",1);
    return map;
}
@PostMapping("deleteGw")
@ResponseBody
public Map<String,Object> deleteGw(@ModelAttribute("Gw") Gw gw){

// SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss");
// Date date = new Date(System.currentTimeMillis());
// user.setDate(formatter.format(date));
Map<String,Object> map=new HashMap<>();

    gwMapper.deleteById(gw.getId());
    map.put("code",1);
    return map;
}


//收件箱
@RequestMapping("gw2")
public String gw2(){
    return "user/gw2";
}
@ResponseBody
@RequestMapping("gwList2")
public DataVo gwList2(@RequestParam(value = "page",defaultValue = "1") Integer page, @RequestParam(value = "limit",defaultValue = "10") Integer limit){
    Subject subject = SecurityUtils.getSubject();
    User currentUser=(User)subject.getPrincipal();//当前登录对象
    QueryWrapper<Gw> ordersQueryWrapper=new QueryWrapper<>();
    ordersQueryWrapper.eq("uid2",currentUser.getId());
    IPage<Gw> page1=new Page<>(page,limit);//每页放几条

    IPage<Gw> labelIPage = gwMapper.selectPage(page1, ordersQueryWrapper);//页数
    return DataVo.builder().code(0).msg("成功").data(labelIPage.getRecords()).count(labelIPage.getTotal()).build();
}
@ResponseBody
@RequestMapping("gw_search2")
public DataVo gw_search(@RequestParam(value = "page",defaultValue = "1") Integer page, @RequestParam(value = "limit",defaultValue = "10") Integer limit,
                        String name){
    Subject subject = SecurityUtils.getSubject();
    User currentUser=(User)subject.getPrincipal();//当前登录对象
    IPage<Gw> page1=new Page<>(page,limit);//每页放几条
    QueryWrapper<Gw> bookQueryWrapper = new QueryWrapper<>();
    bookQueryWrapper.eq("uid2",currentUser.getId());
    bookQueryWrapper.like(
            "uname",name
    );
    IPage<Gw> resourceIPage = gwMapper.selectPage(page1, bookQueryWrapper);//页数
    return DataVo.builder().code(0).msg("成功").data(resourceIPage.getRecords()).count(resourceIPage.getTotal()).build();
}




@Value("${pro.uploadPath}")
private String uploadPath;
public final static String UPLOAD_PATH_PREFIX = "gw/src/main/resources/static";

//工具
@RequestMapping("upload")
@ResponseBody
public Map<String,Object> upload(@RequestParam("file") MultipartFile file){
    Map<String,Object> map=new HashMap<>();
    map.put("file",(String)saveFile(file));
    map.put("code",1);
    return map;
}

private Object saveFile(MultipartFile file){
    if (file.isEmpty()){
        return "未选择文件";
    }
    String filename = file.getOriginalFilename(); //获取上传文件原来的名称
    //文件名
    String name = filename.substring(0, filename.indexOf("."));
    //文件后缀
    String suffix = filename.substring(filename.lastIndexOf("."));

// File filePath = new File(UPLOAD_PATH_PREFIX + uploadPath + File.separator + "temps");
// File filePath = new File(UPLOAD_PATH_PREFIX + File.separator + uploadPath + File.separator + "temps");
Path uploadPath = Paths.get(UPLOAD_PATH_PREFIX, this.uploadPath, "temps");
File filePath = uploadPath.toFile();

    System.out.println("多文件上传的位置: " + filePath);
    System.out.println("多文件上传的绝对路径: " + filePath.getAbsolutePath());
    System.out.println("文件名: " + file.getOriginalFilename());

// String filePath = "C://Users/付奇/Desktop/工作/school/school1/src/main/resources/static/images/";
// if (!filePath.isDirectory()) {
// filePath.mkdirs();
// }
if (!filePath.exists()) {
filePath.mkdirs();
}

    File dest = new File(filePath.getAbsolutePath() + File.separator + file.getOriginalFilename());
    int i = 1;
    //若文件存在重命名
    String newFilename = filename;
    while (dest.exists()) {
        newFilename = name + "(" + i + ")" + suffix;
        filename=newFilename;
        String parentPath = dest.getParent();
        dest = new File(parentPath + File.separator + newFilename);
        i++;
    }
    try {
        file.transferTo(dest);
    } catch (IOException e) {
        e.printStackTrace();
        return file.getOriginalFilename() + "上传失败";
    }

// File localFile = new File(filePath+filename);
// try {
// file.transferTo(localFile); //把上传的文件保存至本地
// System.out.println(file.getOriginalFilename()+" 上传成功");
// }catch (IOException e){
// e.printStackTrace();
// return "filename";
// }

    return filename;
}


private Map<String,Object>  saveFile2(String file) throws InvalidCipherTextException, IOException {
    Map<String,Object> map=new HashMap<>();
    if (file.isEmpty()){
        return map;
    }
    String filename = file; //获取上传文件原来的名称

// //文件名
String name = filename.substring(0, filename.indexOf("."));
//文件后缀
// String suffix = filename.substring(filename.lastIndexOf("."));
String suffix = ".enc";
// File filePath = new File(UPLOAD_PATH_PREFIX + uploadPath + File.separator + "temps");
// File filePath = new File(UPLOAD_PATH_PREFIX + File.separator + uploadPath + File.separator + "temps");
Path uploadPath = Paths.get(UPLOAD_PATH_PREFIX, this.uploadPath, "temps");
File filePath = uploadPath.toFile();

    System.out.println("多文件上传的位置: " + filePath);
    System.out.println("多文件上传的绝对路径: " + filePath.getAbsolutePath());

// System.out.println("文件名: " + file.getOriginalFilename());
// String filePath = "C://Users/付奇/Desktop/工作/school/school1/src/main/resources/static/images/";
// if (!filePath.isDirectory()) {
// filePath.mkdirs();
// }
if (!filePath.exists()) {
filePath.mkdirs();
}

    File dest = new File(filePath.getAbsolutePath() + File.separator + file);
    String inputFilePath =dest.getAbsolutePath();
    System.out.println("cc:"+inputFilePath);
    int i = 1;
    //若文件存在重命名
    String newFilename = filename;
    while (dest.exists()) {
        newFilename = name + "(" + i + ")" + suffix;
        filename=newFilename;
        String parentPath = dest.getParent();
        dest = new File(parentPath + File.separator + newFilename);
        i++;
    }
    byte[] key = SM4Encryptor.generateRandomKey();
    String encryptedFilePath = dest.getAbsolutePath();
    System.out.println("cc2:"+encryptedFilePath);
    // 加密文件
    // 创建加密器实例
    SM4Encryptor encryptor = new SM4Encryptor(key);
    encryptor.encryptFile(inputFilePath, encryptedFilePath);
    System.out.println("加密成功");

    map.put("file",encryptedFilePath);
    map.put("key",Base64.getEncoder().encodeToString(key));

// File localFile = new File(filePath+filename);
// try {
// file.transferTo(localFile); //把上传的文件保存至本地
// System.out.println(file.getOriginalFilename()+" 上传成功");
// }catch (IOException e){
// e.printStackTrace();
// return "filename";
// }

    return map;
}




@GetMapping("/download")
public ResponseEntity<?> downloadFile(String base64Key,String file,String file2) {

    String filename = file; //获取上传文件原来的名称

// //文件名
String name = filename.substring(0, filename.indexOf("."));
//文件后缀
String suffix = filename.substring(filename.lastIndexOf("."));
Path uploadPath = Paths.get(UPLOAD_PATH_PREFIX, this.uploadPath, "temps");
File filePath = uploadPath.toFile();
if (!filePath.exists()) {
filePath.mkdirs();
}

    File dest = new File(filePath.getAbsolutePath() + File.separator + file);
    File dest2 = new File(filePath.getAbsolutePath() + File.separator + file2);

    String inputFilePath =dest.getAbsolutePath();
    System.out.println("cc:"+inputFilePath);
    int i = 1;
    //若文件存在重命名
    String newFilename = filename;
    while (dest.exists()) {
        newFilename = name + "(" + i + ")" + suffix;
        filename=newFilename;
        String parentPath = dest.getParent();
        dest = new File(parentPath + File.separator + newFilename);
        i++;
    }
    try {
        // 将Base64编码的密钥转换为字节
        byte[] key = Base64.getDecoder().decode(base64Key);

        // 加密文件路径和临时解密文件路径
        String encryptedFilePath = file2;
        System.out.println("yy:"+encryptedFilePath);
        String decryptedFilePath = dest.getAbsolutePath();
        System.out.println("yy2:"+decryptedFilePath);

        // 解密文件
        SM4Decryptor decryptor = new SM4Decryptor(key);
        decryptor.decryptFile(encryptedFilePath, decryptedFilePath);

        // 读取解密后的文件并返回
        Path path = Paths.get(decryptedFilePath);
        byte[] data = Files.readAllBytes(path);

        // 清理临时文件
        Files.delete(path);

        return ResponseEntity
                .ok()
                .contentLength(data.length)
                .header("Content-Type", "application/octet-stream")
                .header("Content-Disposition", "attachment; filename=\"" + path.getFileName() + "\"")
                .body(data);

    } catch (Exception e) {
        // 密钥不正确或解密失败
        return ResponseEntity
                .badRequest()
                .body("密钥不正确");
    }
}

}
`

标签:gw,公文,file,String,代码,传输,File,new,import
From: https://www.cnblogs.com/qqhope666/p/17909778.html

相关文章

  • 电子公文传输系统——个人贡献
    我完成的工作:主要负责后端编写,参与了部分前端编写和文本编写。我们小组的相关代码链接:https://gitee.com/an-jiran/dzgwxt/tree/master/(撰写代码时未统计代码行数)我们小组总共发布14篇博客,我贡献了其中6篇,相关链接如下:https://www.cnblogs.com/lanxin5kesong/p/17780874.htm......
  • 电子公文传输系统——个人贡献
    一、个人工作简述(一)在网上检索资料现在网上有许多java-web工程的框架,我们可以在此基础上实现电子公文传输的相关功能,这样会比较好上手一些。所以首先是要搜集资料,在自己的idea里面进行调通。另外我们借鉴别人的项目,最基本的还是要对整个项目体系有清晰的认识:要明白每个项目......
  • 电子公文传输-个人贡献 20211314王艺达
    主要工作数据库构建和代码具体分工内容1)明确电子公文网站设计的用户管理、菜单管理、角色管理、权限管理、字典管理、部门管理、岗位管理、电子公文、密钥管理、在线用户、定时任务等的具体功能需求。2)了解系统的需求,包括数据存储和检索的需求。明确系统中需要存储的数据类型......
  • 电子公文传输系统1-个人贡献
    1.简述你完成的工作整理团队作业1,并发布相应博客;撰写博客冲刺总结2、5-7;对小组成员进行各次任务分配,协助其他组员完成相应部分任务;协助设计系统前端布局;完善后端数据库。2.你们小组总共的代码行数,你贡献的代码行数?相关代码链接?总共的代码行数:11000贡献的代码行数:2010相关......
  • 电子公文系统个人贡献
    20211325_高进涛_实验三_电子公文系统_个人贡献1)防御跨站脚本攻击(XSS)2).防御跨站点请求伪造(CSRF)3).防御SQL注入4).防御访问劫持5).SSL/HTTPS6).Host头部验证7).Referrer策略8).会话安全9).数据库创建与连接10).访问控制(只能访问不大于自己密级的文件)11).文件加密传输12).文件......
  • 电子公文传输系统——个人贡献
    电子公文传输系统——个人贡献一、个人工作简述代码总行数:9032个人完成的代码行数:2104(一)学习Java接口语句返回类和分页在项目中,我学习了Java接口语句的编写,特别是涉及到返回给前端的数据时,需要使用Response类和ResponsePages类进行封装。这两个类分别用于包含返回码......
  • 电子公文传输系统——个人贡献
    电子公文传输系统——个人贡献简述我完成的工作将本小组的博客园进行美化加入背景图片你们小组总共的代码行数,你贡献的代码行数?相关代码链接?通过码云统计后,我们小组代码行数共51932行,我贡献了约11000行代码,主要为对用户输入数据的加密解密模块相关代码链接如下:https://gite......
  • 三菱变频器故障报警代码及故障
    三菱变频器故障报警代码之严重故障OC1加速时过电流断路是否急加速运转输出是否短路,主回路电源(R,S,T)是否供电。E.OC2定速时过电流断路负荷是否有急速变化,输出是否短路。E.OC3减速时过电流断路是否急减速运转,输出是否短路,电机的机械制动是否过早E.OV1加速时再生过电压断路加速度是......
  • 电子公文系统冲刺第三天
    1.活动图片 2.燃尽图3.项目进展实现电子公文传输和存储功能,包括文件上传和下载实现用户身份认证功能,包括多因素身份验证实现国密算法sm2在文件传输中的应用实现用户权限管理功能,包括创建、编辑和删除用户完善密钥生成和存储功能4.今日问题掌握新的编程语言或框架的学......
  • 实验三-电子公文传输系统1-个人贡献
    实验三-电子公文传输系统1-个人贡献任务详情1简述你完成的工作2你们小组总共的代码行数,你贡献的代码行数?相关代码链接?3你们小组总共的文档数?你贡献的文档数?相关链接?主要处理完成的工作1我完成了项目冲刺的5和6两天的实现情况的撰写,编写了部分后端代码和系统的使用指南2......