• java 下载文件的两种方式和java文件的上传


    一:以网络的方式下载文件

    try {      // path是指欲下载的文件的路径。
                File file = new File(path);
                // 以流的形式下载文件。
                InputStream fis = new BufferedInputStream(new FileInputStream(path));
                byte[] buffer = new byte[fis.available()];
                fis.read(buffer);
                fis.close();
                // 清空response
                response.reset();
                // 设置response的Header
                response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes()));
                response.addHeader("Content-Length", "" + file.length());
                OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
                response.setContentType("application/octet-stream");
                toClient.write(buffer);
                toClient.flush();
                toClient.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            return response;

    二:下载本地文件

            File outFile = new File(path);
            if(!outFile.exists()){
                return new ReturnResponse<String>(0, "FAILURE","文件不存在");
            }
            OutputStream outputStream = null;
            try {
                response.setContentType("application/x-download");
                response.addHeader("Content-Disposition", "attachment;filename="+fileName);
                outputStream = response.getOutputStream();
    
                byte[] filebytes = org.apache.commons.io.FileUtils.readFileToByteArray(outFile);
                outputStream.write(filebytes);
                outputStream.flush();
    
                return new ReturnResponse<String>(0, "SUCCESS","");
            } catch (IOException e) {
                e.printStackTrace();
                return new ReturnResponse<String>(0, "FAILURE","读取文件失败");
            }finally{
                if(outputStream!=null){
                    outputStream.close();
                }
            }
        }

     三:文件的上传

    //获取文件信息和存储路径
                String fileName = file.getOriginalFilename();
                File outFile = new File(path);
                if (!outFile.exists()) {
                    outFile.mkdirs();
                }
    
                //保存文件
                String newFileName = UUIDUtil.uuid()+fileUtils.getSuffixFromFileName(fileName);
                String filepath = Paths.get(outFile.getPath(), newFileName).toString();
                BufferedOutputStream stream =
                        new BufferedOutputStream(new FileOutputStream(new File(filepath)));
                stream.write(file.getBytes());
                stream.close();

    四:文件的删除

    File file = new File(path);
            if (file.exists() && file.isFile()) {
                if (file.delete()) {
                    return new ReturnResponse<String>(0, "删除成功!","");
                } else {
                    return new ReturnResponse<String>(0, "删除失败!","");
                }
            } else {
                return new ReturnResponse<String>(0, "删除失败:文件不存在!","");
            }
  • 相关阅读:
    面经分享 | B站 | 数据分析 | 2021.1--转载
    TensorFlow 2.0 学习笔记--第六章 循环神经网络
    TensorFlow 2.0 学习笔记--第五章 神经网络卷积计算
    TensorFlow 2.0 学习笔记--第一章 神经网络计算过程及介绍
    免费服务器
    Nginx采坑日记(后台响应ResponseEntity时,Nginx将部分数据过滤)
    Vue 注意事项
    服务熔断&服务降级
    阿里微服务解决方案-Alibaba Cloud之负载均衡(Feign)(五)
    阿里微服务解决方案-Alibaba Cloud之服务消费方(Feign)(四)
  • 原文地址:https://www.cnblogs.com/bestxyl/p/9066711.html
Copyright © 2020-2023  润新知