zoukankan      html  css  js  c++  java
  • 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, "删除失败:文件不存在!","");
            }
  • 相关阅读:
    NLP(七):textCNN
    pandas(一):选取部分(行、列)写入到另一个文件
    (二) PCA的数学原理
    NLP(六):BiLSTM_Attention实现
    python(三):collection模块
    tensorflow(二十五):Tensorboard可视化
    推荐系统(二):基于pytorch的textdeepfm
    引用相关
    带缺省参数的重复声明
    运算符优先级
  • 原文地址:https://www.cnblogs.com/bestxyl/p/9066711.html
Copyright © 2011-2022 走看看