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 ...
随机推荐
- go微服务框架kratos学习笔记十(熔断器)
目录 go微服务框架kratos学习笔记十(熔断器) 什么是熔断 熔断器逻辑 kratos Breaker kratos 熔断逻辑 kratos熔断器使用说明 bladmaster client br ...
- Javascript学习笔记-基本概念-语句
1.if语句 if (condition) statement1 else statement2 也可以像下面这样把整个if 语句写在一行代码中: if (condition1) statement1 ...
- SQL语句中in 与 exists的区别
SQL语句中in 与 exists的区别 SQL中EXISTS检查是否有结果,判断是否有记录,返回的是一个布尔型(true/false); IN是对结果值进行比较,判断一个字段是否存在于几个值的范围中 ...
- [React技术内幕] setState的秘密
对于大多数的React开发者,setState可能是最常用的API之一.React作为View层,通过改变data从而引发UI的更新.React不像Vue这种MVVM库,直接修改data并不能视图的改 ...
- 【前端】这可能是你看过最全的css居中解决方案了~
1.水平居中:行内元素解决方案 适用元素:文字,链接,及其其它inline或者inline-*类型元素(inline-block,inline-table,inline-flex) html部分代码: ...
- TensorFlow入门知识
Tensorflow基本操作 Tensorflow是一种计算图模型,即用图的形式来表示运算过程的一种模型.Tensorflow程序一般分为图的构建和图的执行两个阶段.图的构建阶段也称为图的定义阶段,该 ...
- Java8 内置的函数式接口
1.Java8 内置的四大核心函数式接口 (1)Consumer<T> : 消费型接口 void accept(T t); (2)Supplier<T> : 供 ...
- Node的require和module.exports
node编程中最重要的思想之一就是模块,在 Node.js 模块系统中,每个文件都被视为独立的模块.这是这个思想,让javascript的大规模工程成为可能.模块化编程在前端大肆盛行,在node中导出 ...
- Eureka停更了?试试Zookpper和Consul
在Spring Cloud Netflix中使用Eureak作为注册中心,但是Eureka2.0停止更新,Eureka1.0 进入了维护状态.就像win7一样,同样可以用,但是官方对于新出现的问题并不 ...
- 内网渗透之信息收集-windows系统篇
windows 用户相关 query user #查看当前在线的用户 whoami #查看当前用户 net user #查看当前系统全部用户 net1 user #查看当前系统全部用户(高权限命令) ...