zoukankan      html  css  js  c++  java
  • fastDfs 下载和上传

    /**
    * 上传文件到fastdfs图片服务器
    * @param file
    * @return
    * @throws IOException
    * @throws MyException
    */
    public HashMap<String, String> fastdfsUpload(MultipartFile file ) throws IOException, MyException {
    String file1 = FastDfsController.class.getResource("/fdfs_tracker.conf").getFile();
    ClientGlobal.init(file1);
    TrackerClient trackerClient = new TrackerClient();
    TrackerServer trackerServer = trackerClient.getConnection();
    StorageServer storageServer = null;
    StorageClient1 storageClient1 = new StorageClient1(trackerServer, storageServer);
    // 获取上传文件的后缀名
    String ext = StringUtils.substringAfterLast(file.getOriginalFilename(), ".");
    //上传的原文件名
    String originalshouquanFile = file.getOriginalFilename();
    //fastDfs文件存储后路径
    String fastdfs_path = storageClient1.upload_appender_file1(file.getBytes(), ext, null);

    HashMap<String, String> hashMap = new HashMap<>();
    hashMap.put("filename",originalshouquanFile);
    hashMap.put("filepath",fastdfs_path);
    return hashMap;
    }

    //从浏览器下载
    @RequestMapping("/fastdfs/download")
    public void fastupload(String fileName , String filePath, HttpServletRequest request, HttpServletResponse response) throws IOException, MyException {
    String downLoadPath = filePath;
    // 获取文件的后缀名
    String ext = StringUtils.substringAfterLast(fileName, ".");
    String file = FastDfsController.class.getResource("/fdfs_tracker.conf").getFile();
    ClientGlobal.init(file);
    TrackerClient trackerClient = new TrackerClient();
    TrackerServer trackerServer = trackerClient.getConnection();
    StorageServer storageServer = null;
    StorageClient1 storageClient1 = new StorageClient1(trackerServer, storageServer);
    byte[] by = storageClient1.download_file1(downLoadPath);
    response.setCharacterEncoding("UTF-8");
    response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("test.jpg", "UTF-8"));
    if (by!=null){
    ServletOutputStream outputStream = response.getOutputStream();
    IOUtils.write(by,outputStream);
    }
    }



  • 相关阅读:
    ssh.sh_for_ubuntu1604
    ssh.sh_for_ubuntu1404
    ssh.sh_for_ubuntu1204
    ssh.sh_for_centos
    raw,cow,qcow,qcow2镜像的比较
    Oz 创建Windows2008R2镜像
    Oz 创建Ubuntu镜像
    Oz 创建Debian8镜像
    Oz 创建CentOS7镜像
    Oz 创建CentOS6镜像
  • 原文地址:https://www.cnblogs.com/cuiguangpeng/p/12980824.html
Copyright © 2011-2022 走看看