java file upload and download function

For the front-end code upload code, I use the element-ui template:

<el-upload  action="http://localhost:8090/sysFile/upload"   :on-success="handleSuccess" :show-file-list="false" style="display: inline-block;">
   <el-button type="primary">上传文件</i></i><i class="el-icon-circle-plus"></i></el-button>
</el-upload>

Path in application.yml:

files:
  upload:
    path: D:/桌面/xiangmu/springboot_vue_manager/src/main/java/com/example/springboot_vue_manager/file/

Backend interface, here you only need to upload files and use MultipartFile to receive them

//在配置文件中配置了文件的路径,这里获取到application中文件的路径
    @Value("${files.upload.path}")
    private String fileUploadPath;

    /**
     * 文件上传的接口
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("upload")
    public boolean upload(MultipartFile file) throws IOException {
    
    
        //先获取原始的文件名
        String originalFilename=file.getOriginalFilename();
        //使用hotool工具判断文件的类型
        String type= FileUtil.extName(originalFilename);
        //获取文件的大小
        long size=file.getSize();
        //传入路径
        File uploadParentFileFile=new File(fileUploadPath);
        //判断文件夹存,不存在的话在这个文件下的路径下创建一个文件夹
        if (!uploadParentFileFile.exists()){
    
    
            uploadParentFileFile.mkdirs();
        }
        //唯一标识
        String uuid= IdUtil.fastSimpleUUID();
        //唯一标识+文件类型
        String fileUuid=uuid+ StrUtil.DOT+type;
        //文件路径+唯一标识和类型
        File uploadFile=new File(fileUploadPath+fileUuid);
        //transferTo转移的意思,把这个文件转移到设置的目录下面
        file.transferTo(uploadFile);
        String url;
        boolean code = true;    //返回状态
        String md5;
        //通过文件名生成MD5
        md5= SecureUtil.md5(uploadFile);
        //通过MD5在数据库进行查询看是否有这条数据
        List<SysFile> byMd5=sysFileService.queryByMd5(md5);
        //如果查询有这条记录的话
        if (byMd5.size()!=0){
    
    
            //已经有这个文件了文件了
            code=false;
            //那么久获取到他的url
//            url = byMd5.get(0).getUrl();
//            由于判断文件存在所以删除存入磁盘的文件路径
            uploadFile.delete();
        }else {
    
    
            url= "http://localhost:8090/sysFile/download/"+fileUuid;
            //存储数据库
            System.out.println("如果文件存在也进来了");
            SysFile sysFile=new SysFile();
            sysFile.setName(originalFilename);
            sysFile.setSize(size/1024);
            sysFile.setType(type);
            sysFile.setUrl(url);
            sysFile.setMd5(md5);
            sysFile.setEnable(true);
            sysFile.setIsDelete(false);
            sysFileService.insert(sysFile);
        }
        return code;
    }

Download function:

<template slot-scope="scope">
	<el-button type="primary" @click="download(scope.row.url)">下载</el-button>
</template>

// vue中method方法:
 //   下载
download(res){
    
    
   window.open(res)
},
    @GetMapping("/download/{fileUuid}")
    public void download(@PathVariable String fileUuid, HttpServletResponse response) throws IOException {
    
    
        //根据文件唯一标识码获取文件
        File uploadFile=new File(fileUploadPath+fileUuid);
        //设置输出流格式
        ServletOutputStream os=response.getOutputStream();
        response.addHeader("Content-Disposition","attachment;filename="+
                URLEncoder.encode(fileUuid,"UTF-8"));
        response.setContentType("application/octet-stream");
        //读取文件的字节流
        os.write(FileUtil.readBytes(uploadFile));
        os.flush();
        os.close();
    }

Guess you like

Origin blog.csdn.net/qq_14930709/article/details/124520178