coding++:java操作 FastDFS(上传 | 下载 | 删除)
开发工具 IDEAL2017 Springboot 1.5.21.RELEASE
-------------------------------------------------------------------------------------
1、所需要的JAR文件
<!--IO-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!--FastDFS-->
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27-SNAPSHOT</version>
</dependency>
2、fastdfs.properties 属性设置
#FastDFS配置begin-----------除了fastdfs.tracker_servers,其它配置项都是可选的
fastdfs.connect_timeout_in_seconds=5
fastdfs.network_timeout_in_seconds=30
fastdfs.charset=UTF-8
fastdfs.http_anti_steal_token=false
fastdfs.http_secret_key=FastDFS1234567890
fastdfs.http_tracker_http_port=80
fastdfs.tracker_servers=IP:22122
#FastDFS配置end-----------
3、操作工具类
package cn.com.soundrecording.utils; import org.csource.common.NameValuePair;
import org.csource.fastdfs.*; public class FastDFSClient { private TrackerClient trackerClient = null;
private TrackerServer trackerServer = null;
private StorageServer storageServer = null;
private StorageClient1 storageClient = null; public FastDFSClient(String conf) throws Exception {
if (conf.contains("classpath:")) {
conf = conf.replace("classpath:", this.getClass().getResource("/").getPath());
}
ClientGlobal.initByProperties(conf);
trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
storageServer = null;
storageClient = new StorageClient1(trackerServer, storageServer);
} /**
* 上传文件方法
* <p>Title: uploadFile</p>
* <p>Description: </p>
*
* @param fileName 文件全路径
* @param extName 文件扩展名,不包含(.)
* @param metas 文件扩展信息
* @return
* @throws Exception
*/
public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {
String result = storageClient.upload_file1(fileName, extName, metas);
return result;
} public String uploadFile(String fileName) throws Exception {
return uploadFile(fileName, null, null);
} public String uploadFile(String fileName, String extName) throws Exception {
return uploadFile(fileName, extName, null);
} /**
* 上传文件方法
* <p>Title: uploadFile</p>
* <p>Description: </p>
*
* @param fileContent 文件的内容,字节数组
* @param extName 文件扩展名
* @param metas 文件扩展信息
* @return
* @throws Exception
*/
public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception { String result = storageClient.upload_file1(fileContent, extName, metas);
return result;
} public String uploadFile(byte[] fileContent) throws Exception {
return uploadFile(fileContent, null, null);
} public String uploadFile(byte[] fileContent, String extName) throws Exception {
return uploadFile(fileContent, extName, null);
} /**
* 下载文件方法
* <p>Title: uploadFile</p>
* <p>Description: </p>
*
* @param groupName 分组
* @param remoteFileName 文件全路径名称
* @return
* @throws Exception
*/
public byte[] download(String groupName,String remoteFileName)throws Exception{
return storageClient.download_file(groupName, remoteFileName);
}
/**
* 删除文件方法
* <p>Title: uploadFile</p>
* <p>Description: </p>
*
* @param groupName 分组
* @param remoteFileName 文件全路径名称
* @return
* @throws Exception
*/
public Integer delete(String groupName,String remoteFileName)throws Exception{
int i = storageClient.delete_file(groupName, remoteFileName);
return i;
} }
4、调用操作 后台代码
package cn.com.soundrecording.controller; import cn.com.soundrecording.utils.FastDFSClient;
import com.sun.net.httpserver.HttpContext;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List; @RestController
public class UploedController { private final String URL = "http://wlkjs.cn/"; //上传到服务器
@PostMapping("/upload")
@ResponseBody
public String uploed(MultipartFile multipartFile, HttpServletRequest request) throws Exception { //文件类型
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
//02、上传到服务器
FastDFSClient dfsClient = new FastDFSClient("config/fastdfs.properties");
String url = dfsClient.uploadFile(multipartFile.getBytes(), request.getParameter("type"));
System.out.println(url);
return URL + url; } //从服务器下载
@GetMapping("/download")
public void download(String fileName, HttpServletResponse response) throws Exception {
String name, groupName, remoteFileName;
//初始化连接
FastDFSClient dfsClient = new FastDFSClient("config/fastdfs.properties");
//获取 group1 名称
groupName = fileName.substring(fileName.indexOf("group1"), fileName.indexOf("/M00"));
//获取 文件全路径 M00..xxxxx
remoteFileName = fileName.substring(fileName.indexOf("M00"));
name = fileName.substring(fileName.lastIndexOf("/"));
//执行下载
byte[] content = dfsClient.download(groupName, remoteFileName);
//响应到客户端下载
response.setContentType("application/ms-mp3;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename="
.concat(String.valueOf(URLEncoder.encode(name, "UTF-8"))));
OutputStream out = response.getOutputStream();
out.write(content);
out.flush();
out.close();
} //从服务器删除
@PostMapping("/delete")
public Object delete(String fileName) throws Exception {
String groupName, remoteFileName;
//获取 group1 名称
groupName = fileName.substring(fileName.indexOf("group1"), fileName.indexOf("/M00"));
//获取 文件全路径 M00..xxxxx
remoteFileName = fileName.substring(fileName.indexOf("M00"));
//执行删除
FastDFSClient dfsClient = new FastDFSClient("config/fastdfs.properties");
//返回 0 代表成功
int i = dfsClient.delete(groupName, remoteFileName);
System.out.println(i == 0 ? "删除成功" : "删除失败:" + i);
return i;
} }
coding++:java操作 FastDFS(上传 | 下载 | 删除)的更多相关文章
- coding++: java 操作FastDFS(上传 | 下载 | 删除)
package cn.com.soundrecording.controller; import cn.com.soundrecording.utils.FastDFSClient;import co ...
- JAVA 实现FTP上传下载(sun.net.ftp.FtpClient)
package com.why.ftp; import java.io.DataInputStream; import java.io.File; import java.io.FileInputSt ...
- jm解决乱码问题-参数化-数据库操作-文件上传下载
jm解决乱码问题-参数化-数据库操作-文件上传下载 如果JM出果运行结果是乱码(解决中文BODY乱码的问题) 找到JM的安装路径,例如:C:\apache-jmeter-3.1\bin 用UE打开jm ...
- CentOS下安装配置NFS并通过Java进行文件上传下载
1:安装NFS (1)安装 yum install nfs-utils rpcbind (2)启动rpcbind服务 systemctl restart rpcbind.service 查看服务状态 ...
- java实现文件上传下载
喜欢的朋友可以关注下,粉丝也缺. 今天发现已经有很久没有给大家分享一篇技术文章了,于是想了一下给大家分享一篇java实现文件上传下载功能的文章,不喜欢的希望大家勿喷. 想必大家都知道文件的上传前端页面 ...
- FastDFS上传/下载过程[转载-经典图列]
FastDFS上传/下载过程: 首先客户端 client 发起对 FastDFS 的文件传输动作,是通过连接到某一台 Tracker Server 的指定端口来实现的,Tracker Server 根 ...
- fastDFS与java整合文件上传下载
准备 下载fastdfs-client-java源码 源码地址 密码:s3sw 修改pom.xml 第一个plugins是必需要的,是maven用来编译的插件,第二个是maven打源码包的,可以不要. ...
- java FTP 上传下载删除文件
在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件.下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在commons-net包中)实现上传下载文件 ...
- FasfDFS整合Java实现文件上传下载
文章目录 一 : 添加配置文件 二 : 加载配置文件 1. 测试加载配置文件 2. 输出配置文件 三:功能实现 1.初始化连接信 ...
- FasfDFS整合Java实现文件上传下载功能实例详解
https://www.jb51.net/article/120675.htm 在上篇文章给大家介绍了FastDFS安装和配置整合Nginx-1.13.3的方法,大家可以点击查看下. 今天使用Java ...
随机推荐
- python基础-基本概念
python概念介绍 python是一门动态解释型的强类型定义语言,创始人吉多·范罗苏姆(Guido van Rossum) #编译型语言 编译型:一次性将所有程序编译成二进制文件 缺点:开发效率低, ...
- 达拉草201771010105《面向对象程序设计(java)》第十三周学习总结
达拉草201771010105<面向对象程序设计(java)>第十三周学习总结 第一部分:理论知识 事件处理基础: 事件源:能够产生事件的对象都可 以成为事件源,如文本框.按钮等.一个事件 ...
- python自己做计算器
题目: exp = '1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) ...
- 使用EventBus + Redis发布订阅模式提升业务执行性能
前言 最近一直奔波于面试,面了几家公司的研发.有让我受益颇多的面试经验,也有让我感觉浪费时间的面试经历~因为疫情原因,最近宅在家里也没事,就想着使用Redis配合事件总线去实现下具体的业务. 需求 一 ...
- js 打开新窗口方式
之前的项目,有个功能是下载文件,这里只要在浏览器输入 url 就会下载那个文件了.当时我只是简单得使用 window.open ,但是却会被浏览器进行拦截,要手动开启才行,然后就搜索研究其他方法,就看 ...
- MacOS麦克风输入监听的方法
Windows上很多人都知道,然而实际上并没什么用——延迟太大,根本没法用. MacOS上有两种方法: QuickTime Player新建音频录制(不需要真的录音),如下图: 这个方法和Window ...
- Block as a Value for SQL over NoSQL
作者 Yang Cao,Wenfei Fan,Tengfei Yuan University of Edinburgh,Beihang University SICS, Shenzhen Univer ...
- openwrt 外挂usb 网卡 RTL8188CU 及添加 RT5572 kernel支持
RT5572 原来叫 Ralink雷凌 现在被 MTK 收购了,淘宝上买的很便宜50块邮,2.4 5G 双频.在 win10 上插了试试,果然是支持 5G.这上面写着 飞荣 是什么牌子,有知道的和我说 ...
- 痞子衡嵌入式:恩智浦SDK驱动代码风格、模板、检查工具
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家讲的是恩智浦 SDK 驱动的代码风格. 上周痞子衡受领导指示,给 SE 同事做了一个关于 SDK 代码风格的分享.随着组内新人的增多,这样的培训 ...
- SpringBoot2整合Shiro报错 UnavailableSecurityManagerException: No SecurityManager accessible to the calling code 【已解决】
SpringBoot集成Shiro报错 UnavailableSecurityManagerException: No SecurityManager accessible to the callin ...