Java文件上传下载。

前言

碰到一些时候自己要用到文件上传下载的功能,之前没有写这个博客,网上找其他的有时看起来比较乱,所以自己弄得一个文件上传下载类,方便自己copy。

文件配置类

public class FileConfig {
    /**
     * 文件上传路径在项目根路径
     */
    public static String FILE_ADDRESS= System.getProperty("user.dir")+"\\files\\";
    /**
     * @param multipartFile
     * @return
     * @throws IOException
     */
    public static String copyFile(MultipartFile multipartFile) throws IOException {
        //文件名规则 当前系统毫秒数+随机数+文件名
        String fileName = System.currentTimeMillis()+"_"+(int)(Math.random()*90+10)+"_"+multipartFile.getOriginalFilename();
        //文件目录
        String filePath = FILE_ADDRESS;
        File dirPath = new File(filePath);
        if (!dirPath.exists()) {
            dirPath.mkdirs();
        }
        File file = new File(filePath+fileName);
        multipartFile.transferTo(file);
        return fileName;
    }
    /**
     *
     * @param fileName 数据库中记录的文件名
     * @param newFileName 在页面下载显示的文件名 可以不需要。
     * @param response
     * @throws IOException
     */
    public static void download(String fileName, String newFileName, HttpServletResponse response) throws IOException {
        File file = new File(FileConfig.FILE_ADDRESS+fileName);
        // 穿件输入对象
        FileInputStream fis = new FileInputStream(file);
        // 设置相关格式
        response.setContentType("application/force-download");
        // 设置下载后的文件名以及header
        response.addHeader("Content-disposition", "attachment;fileName=" + newFileName);
        // 创建输出对象
        OutputStream os = response.getOutputStream();
        // 常规操作
        byte[] buf = new byte[1024];
        int len = 0;
        while((len = fis.read(buf)) != -1) {
            os.write(buf, 0, len);
        }
        fis.close();
    }
}

使用上传

采用异步上传的方式 返回值可以修改为自己的统一返回格式

@RequestMapping("upload2")
    public void upload2(@RequestParam("file") List<MultipartFile> files) throws IOException {
        if (!files.isEmpty() && files.size() > 0) {
            for (MultipartFile file : files) {
                if(!file.isEmpty()) {
                    //新的文件名
                    String copyName = FileConfig.copyFile(file);
                    //文件本身的名字
                    String filename = file.getOriginalFilename();
                    //把文件名保存到数据库的操作我就不写了
                }
            }
        }
    }

使用下载

前端代码
这个th标签是手写的吗,没测,有错误正常。。

<a th:href="@{'/download2/'+${file.newFilename}+'/'+${file.filename}}">下载</a>

后端代码
同样是ajax异步下载,传值可以是文件在数据库的id ,然后查询就行了

@RequestMapping("download2/{newFilename}/{filename}")
    public void download2(@PathVariable String newFilename, @PathVariable String filename, HttpServletResponse response) throws IOException {
        FileConfig.download(newFilename, filename, response);
    }

后语

代码有的是借鉴别的博客的,下载的代码看了还有其他的方式,我就用了这一种。下面放一些其他的上传下载

文件批量上传下载
文件上传下载

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值