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



猜你喜欢

转载自www.cnblogs.com/cuiguangpeng/p/12980824.html