码迷,mamicode.com
首页 > Web开发 > 详细

fastDfs 下载和上传

时间:2020-05-28 14:57:50      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:coding   rac   param   get   nload   下载   ora   new t   浏览器   

/**
* 上传文件到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 下载和上传

标签:coding   rac   param   get   nload   下载   ora   new t   浏览器   

原文地址:https://www.cnblogs.com/cuiguangpeng/p/12980824.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!