文件处理下载后缀被截掉

前言

Java学习路线个人总结-博客
❤欢迎点赞👍收藏⭐留言 📝分享给需要的小伙伴

  • 原始@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;
    }

iSO:镜像文件 RAR:压缩包 html:网页 zip:压缩包 exe:可执行文件 pdf:pdf文档 rm:视频文件 avi:视频文件 tmp:临时文件 xls:excel工作表 mdf:虚拟光驱镜像文件 txt:记事本 doc:word文档 MID:声卡声乐文件 文件类型 扩展名及打开方式 文档文件 txt(所有文处理软件或编辑器都可打开)、doc(word及wps等软件可打开)、hlp(adobe acrobat reader可打开)、wps(wps软件可打开)、rtf(word及wps等软件可打开)、html(各种浏览器可打开、用写板打开可查看其源代码)、pdf(adobe acrobat reader 和各种电子阅读软件可打开) 压缩文件 rar(winrar可打开)、zip(winzip可打开)、arj(用arj解压缩后可打开)、gz(unix系统的压缩文件,用winzip可打开)、z(unix系统的压缩文件,用winzip可打开) 图形文件 bmp、gif、jpg、pic、png、tif(这些文件类型用常用图像处理软件可打开) 声音文件 wav(媒体播放器可打开)、aif(常用声音处理软件可打开)、au(常用声音处理软件可打开)、mp3(由winamp播放)、ram(由realplayer播放)、wma、mmf、amr、aac、flac 动画文件 avi(常用动画处理软件可播放)、mpg(由vmpeg播放)、mov(由activemovie播放)、swf(用flash自的players程序可播放) 系统文件 int、sys、dll、adt 可执行文件 exe、com 语言文件 c、asm、for、lib、lst、msg、obj、pas、wki、bas
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冒险的梦想家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值