上传下载

下载

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

        ResultData 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 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 files) {

        MultipleFileResult multipleFileResult = new MultipleFileResult();
        multipleFileResult.setFileTotal(files.size());
        List fileIndexList = new ArrayList<>();
        for (MultipartFile mTemp : files) {
            ResultData 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 uploadFileBase(MultipartFile file) {

        ResultData 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();
    }

  

你可能感兴趣的:(上传下载)