上传下载

下载

@RequestMapping("/file")
    public void downLoad(@RequestParam String fileUrl,@RequestParam String fileName, HttpServletResponse response) throws UnsupportedEncodingException {

        ResultData<String> diskUrlResultData = FileUtil.getLocalUrl(fileConfig.getAttribute(),fileUrl);
        if (!Objects.equals(ResultEnum.SUCCESS.getCode(),diskUrlResultData.getCode())){
            return;
        }
        String extName = FileUtil.getFileExtension(fileUrl);
        try {
            File file =new File(diskUrlResultData.getData());
            FileInputStream fileInputStream=new FileInputStream(file);
            response.setContentType("application/gorce-download");
            response.addHeader("Content-disposition","attachment;fileName="+fileName+"."+extName);
            OutputStream outputStream = response.getOutputStream();
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = fileInputStream.read(bytes))!=-1){
                outputStream.write(bytes,0,len);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

  上传

@PostMapping("/single")
    public ResultData singleUpload(@RequestParam("file") MultipartFile file) {

        ResultData<FileIndex> fileIndexResultData = uploadFileBase(file);
        if (Objects.equals(ResultEnum.SUCCESS.getCode(), fileIndexResultData.getCode())){
            FileIndex fileIndex = fileIndexResultData.getData();
            fileIndexService.save(fileIndex);
            return ResultDataUtil.setSuccessResult(fileIndex.getNetPath());
        }
        return fileIndexResultData;
    }

    @PostMapping("/multiple")
    public ResultData multipleUpload(@RequestParam("files") List<MultipartFile> files) {

        MultipleFileResult multipleFileResult = new MultipleFileResult();
        multipleFileResult.setFileTotal(files.size());
        List<FileIndex> fileIndexList = new ArrayList<>();
        for (MultipartFile mTemp : files) {
            ResultData<FileIndex> resultData = uploadFileBase(mTemp);
            if (Objects.equals(ResultEnum.SUCCESS.getCode(),resultData.getCode())) {
                multipleFileResult.getResultMap().put(mTemp.getOriginalFilename(),resultData.getData().getNetPath());
                multipleFileResult.setSuccessCount(multipleFileResult.getSuccessCount()+1);
                fileIndexList.add(resultData.getData());
            }else {
                multipleFileResult.getFailureFileNames().add(mTemp.getOriginalFilename());
            }
        }
        if (fileIndexList.size()>0){
            fileIndexService.saveAll(fileIndexList);
        }
        return ResultDataUtil.setSuccessResult(multipleFileResult);
    }

    /**
     * @Author AlanMa
     * @Description 上传文件
     * @Date 2019/11/6
     * @Param [file]
     * @return com.hiynn.data.visual.file.vo.ResultData
     */
    private ResultData<FileIndex> uploadFileBase(MultipartFile file) {

        ResultData<FileIndex> resultData = UploadUtil.getUploadInfo(fileConfig,file);
        if (!Objects.equals(ResultEnum.SUCCESS.getCode(),resultData.getCode())) {
            return resultData;
        }
        FileIndex fileIndex = resultData.getData();
        File dest = new File(fileIndex.getStoragePath());
        try {
            file.transferTo(dest);
            return ResultDataUtil.setSuccessResult(fileIndex);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResultDataUtil.setFailedResult();
    }

  

posted @ 2019-11-07 17:54  青竹玉简  阅读(142)  评论(0编辑  收藏  举报