上传文件

/**
     * 上传文件
     * @param files    文件
     */
    @PostMapping("upload")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "上传文件", notes = "传入文件")
    public R<List<OaAttachment>> upload(@RequestParam List<MultipartFile> files) {
        List<OaAttachment> list = new ArrayList<OaAttachment>();
        //String dirPath = getRequest().getServletContext().getRealPath("/") +"uploadFile/";
        String dirPath = "E:/attachment/oa/uploadFile/" ;
        File dir = new File(dirPath);
        if (!dir.exists()) {
            dir.mkdir();
        }
        files.forEach(file -> {
            try {
                String fileName = file.getOriginalFilename();
                String fileType = fileName.substring(fileName.indexOf("."));
                String filePath = UUID.randomUUID().toString() +fileType;
                File newFile = new File(dirPath,  filePath);
                FileCopyUtils.copy(file.getInputStream(),Files.newOutputStream(newFile.toPath()));
                OaAttachment attachment = new OaAttachment();
                attachment.setName(fileName);
                attachment.setType(fileType);
                attachment.setUrl("oa/uploadFile/"+filePath);
                attachment.setAttachmentSize(file.getSize());
                oaAttachmentService.save(attachment);
                list.add(attachment);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        return R.data(list);
    }

 

posted @ 2020-08-18 14:49  红尘沙漏  阅读(141)  评论(0编辑  收藏  举报