fastDfs 下载和上传
Posted cuiguangpeng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
}
}
以上是关于fastDfs 下载和上传的主要内容,如果未能解决你的问题,请参考以下文章