(本机)文件上传:什么文件都可一试(线上不行)

 /**
     * 上传文件
     *
     * @param file
     * @return
     * @throws Exception
     */
    @ResponseBody
    @PostMapping("addFileForNotice")
    @ApiOperation(value = "上传文件for通知公告", notes = "")
    public ResultDTO<String> addFileForNotice(HttpServletRequest request, MultipartFile file, String groupId, String fileSourceId,String type) throws Exception {
        ResultDTO<String> resultDTO = new ResultDTO<>();
        String filePath = null;
        // 当前登录人
        LoginOperator nowOperDTO = OperatorUtil.getOperator(request);
        if (null == nowOperDTO) {
            return resultDTO.set(ResultCodeEnum.ERROR_UNKNOW, "请先登陆,才能进行操作!");
        }

        if(!file.isEmpty()) {
            try {
                 filePath = uploadFileService.addFile2(nowOperDTO, file.getOriginalFilename(), file.getInputStream(), groupId, fileSourceId,type);
            } catch (Exception e) {
                e.printStackTrace();
                return resultDTO.set(ResultCodeEnum.ERROR_UNKNOW, e.getMessage());
            }
        }else {
            return resultDTO.set(ResultCodeEnum.ERROR_UNKNOW, "文件为空!");
        }
        return resultDTO.set(ResultCodeEnum.SUCCESS, "发布成功",filePath);
    }



/**
     * 获取一个返回值for 通知公告的详情附件下载链接
     * @param nowOperDTO
     * @param fileName
     * @param fileInputStream
     * @param groupId
     * @param fileSourceId
     * @return
     * @throws Exception
     */
    String addFile2(LoginOperator nowOperDTO, String fileName, InputStream fileInputStream, String groupId, String fileSourceId,String type) throws Exception;



@Override
    public String addFile2(LoginOperator nowOperDTO, String fileName, InputStream fileInputStream, String groupId, String fileSourceId,String type) throws Exception {
        String originalFileName = fileName;
        fileName = genFileNameOnDisk(fileName);
        String targetFilePath = uploadFilePath + File.separator + fileName;

        File targetFile = new File(targetFilePath);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        Files.copy(fileInputStream, targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING);

        SmUploadFile uploadFile = new SmUploadFile();
        SmVideo smVideo = new SmVideo();
        String filePath = null;
        if ("video".equals(type)) {
            smVideo.setCreatedDate(new Date());
            smVideo.setCreater(nowOperDTO.getOpName());
            smVideo.setIsdelete(0);
            smVideo.setVideoName(originalFileName);
            smVideo.setVideoPath(uploadFilePath + File.separator + fileName);
            smVideo.setFileSize(targetFile.length());

            sqlManager.insertTemplate(SmVideo.class, smVideo, true);
            filePath = smVideo.getVideoPath();
        } else {
            uploadFile.setFileName(originalFileName);
            uploadFile.setFilePath(uploadFilePath + File.separator + fileName);
            uploadFile.setFileSize(targetFile.length());
            uploadFile.setGroupId(groupId);
            uploadFile.setFileSourceId(fileSourceId);
            uploadFile.setCreateOpId(nowOperDTO.getOpId());
            uploadFile.setCreateDate(new Date());
            sqlManager.insertTemplate(SmUploadFile.class, uploadFile, true);
            filePath = uploadFile.getFilePath();
        }
        return filePath;
    }


private String genFileNameOnDisk(String fileName){
        String prefix = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
        String fileNameNew = prefix + "_" + fileName;
        return fileNameNew;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值