• 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);
    }
    }



  • 相关阅读:
    第03次作业-栈和队列
    第02次作业-线性表
    Data-Structure01-绪论
    c语言第二次实验报告
    C语言第一次实验报告
    KD-tree讲解
    AAAA、
    清北学堂 day one
    生长,开始记录!
    Linux命令(待完善)
  • 原文地址:https://www.cnblogs.com/cuiguangpeng/p/12980824.html
Copyright © 2020-2023  润新知