文件处理下载后缀被截掉

  • 原始@GetMapping(value = "/files/download/{real_name}/{file_name}")

  • 现在@GetMapping(value = "/files/download/{real_name:.+}/{file_name:.+}")

/**
     * @param fileName 儲存文件名
     * @param realName 下载文件名
     * @param response
     **/
    @GetMapping(value = "/files/download/{real_name:.+}/{file_name:.+}")
    public void downFile(@PathVariable("real_name") String realName, @PathVariable("file_name") String fileName, HttpServletResponse response) {
     
        //获取文件上传路径
        String uploadPath = "C:/uploadPath";
        downFile(response, realName, fileName, uploadPath);
    }


    /**
     * @param response
     * @param realName   文件储存名称
     * @param fileName   文件下载名称
     * @param uploadPath 文件储存地址
     * @return void
     **/
    public static void downFile(HttpServletResponse response, String realName, String fileName, String uploadPath) {
     
        //下載文件
        String filePath = uploadPath + File.separator + realName;
        //原始文件
        String path = uploadPath + File.separator + fileName;

        //判断文件是否存在
        File file = new File(filePath);
        if (!file.exists()) {
     
            System.out.println("文件不存在");
        }
        File file1 = new File(path);
        FileInputStream fis = null;
        OutputStream myout = null;
        BufferedInputStream buff = null;
        try {
     
            if (file.exists()) {
     
                response.setHeader("Content-Disposition",
                        "attachment;filename=" + new String((file1.getName()).getBytes("UTF-8"), "ISO8859-1"));
                response.setContentLength((int) file.length());
                // 定义输出类型
                response.setContentType("application/octet-stream");
                fis = new FileInputStream(file);
                buff = new BufferedInputStream(fis);
                // 相当于我们的缓存
                byte[] b = new byte[1024];
                // 该值用于计算当前实际下载了多少字节
                long k = 0;
                // 从response对象中得到输出流,准备下载
                myout = response.getOutputStream();
                // 开始循环下载
                while (k < file.length()) {
     
                    int j = buff.read(b, 0, 1024);
                    k += j;
                    myout.write(b, 0, j);
                }
            }
        } catch (Exception e) {
     
            throw new RuntimeException("文件下载流错误,错误原因:" + e);
        } finally {
     
            if (fis != null) {
     
                try {
     
                    fis.close();
                } catch (IOException e) {
     
                    throw new RuntimeException("流关闭异常,错误原因:" + e);
                }
            }
            if (myout != null) {
     
                try {
     
                    myout.flush();
                    myout.close();
                } catch (IOException e) {
     
                    throw new RuntimeException("流关闭异常,错误原因:" + e);
                }
            }
            if (buff != null) {
     
                try {
     
                    buff.close();
                } catch (IOException e) {
     
                    throw new RuntimeException("流关闭异常,错误原因:");
                }
            }
        }
    }
  • 文件上传

	@RequestMapping(value = "/multifileUpload", method = RequestMethod.POST)
    public List<Map<String, Object>> multifileUpload(HttpServletRequest request) {
     
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("fileName");
        if (files.isEmpty()) {
     
            throw new RuntimeException("当前上传文件为空");
        }
        //上传地址
        String path = "H:/test";
        List<Map<String, Object>> fileNameList = new ArrayList<>();
        String newFileName = "";
        Map map = null;
        for (MultipartFile file : files) {
     
            map = new HashMap();
            String fileName = file.getOriginalFilename();
            //旧文件名
            map.put("originalFileName", fileName);
            newFileName = Calendar.getInstance().getTimeInMillis() + "_" + fileName;
            //新文件名
            map.put("newFileName", newFileName);
            if (file.isEmpty()) {
     
                throw new RuntimeException("当前上传文件为空!");
            } else {
     
                File dest = new File(path + "/" + newFileName);
                if (!dest.getParentFile().exists()) {
      //判断文件父目录是否存在
                    dest.getParentFile().mkdir();
                }
                try {
     
                    file.transferTo(dest);
                } catch (Exception e) {
     
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    throw new RuntimeException("上传文件错误");
                }
            }
            fileNameList.add(map);
        }
        return fileNameList;
    }

你可能感兴趣的:(java)