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 ...
随机推荐
- Linux - 修改系统的max open files、max user processes(附ulimit的使用方法)【转载】
Linux - 修改系统的max open files.max user processes(附ulimit的使用方法)目录 1 问题说明2 修改max open files3 修改max user ...
- 内存:你跑慢点行不行?CPU:跑慢点你养我吗?内存:我不管!(内附超全思维导图)
主存(RAM) 是一件非常重要的资源,必须要认真对待内存.虽然目前大多数内存的增长速度要比 IBM 7094 要快的多,但是,程序大小的增长要比内存的增长还快很多.不管存储器有多大,程序大小的增长速度 ...
- git add的各种情况分类
· git add -A 提交所有变化 · git add -u 提交被修改(modified)和被删除(deleted)文件,不包括新文件(new) · git add . 提交新文件( ...
- var, let ,const区别
ES6中加入了let,const字符,先说说新的区别: 作用域:let 声明的变量只在它所在的代码块内有效,而且不存在变量提升,即变量可以在声明之前使用,值为undefined.let未声明变量前会报 ...
- Markdown使用说明
# Markdown 使用说明 Markdown 是一种**轻量级标记语言** 使用规则: 1. 标题 2. 列表 3. 引用 4. 图片与链接 5. 粗体与斜体 6.表格 7. 代码框 8. 分 ...
- 2.5D地图系统技术方案
1. 2.5D地图概述 1.1. 概述 2.5维地图就是根据dem.dom.dlg等数据,以及真三维模型在一定高度.视角和灯光效果,按照轴侧投影的方式生成的地图.本文以臻图信息ZTMapE ...
- 032.核心组件-kube-proxy
一 kube-proxy原理 1.1 kube-proxy概述 Kubernetes为了支持集群的水平扩展.高可用性,抽象出了Service的概念.Service是对一组Pod的抽象,它会根据访问策略 ...
- hdu(杭电oj)输入输出练习题目总结
1000.1001 .1089.1090.1091.1092.1093.1094.1095.1096
- Elasticsearch系列---多字段搜索
概要 本篇介绍一下multi_match的best_fields.most_fields和cross_fields三种语法的场景和简单示例. 最佳字段 bool查询采取"more-match ...
- Leetcode_面试题 17.24. 最大子矩阵
最大子矩阵问题,n是200,枚举上下行,O(N)求一下最大子段和. code class Solution { public: vector<int> getMaxMatrix(vecto ...