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 ...
随机推荐
- 【Django】接收照片,储存文件 前端代码
后端: from rest_framework.views import APIView from car import settings from django.shortcuts import r ...
- js轮询及踩过的坑
背景 下午四点,天气晴朗,阳光明媚,等着下班产品:我希望页面上的这个数据实时变化开发:···,可以,用那个叫着WebSocket的东西,再找一个封装好框架,如:mqtt(感觉自己好机智)产品:要开发好 ...
- Redis系列四 - 分布式锁的实现方式
前言 分布式锁一般有3中实现方式: 数据库乐观锁: 基于Redis的分布式锁: 基于ZooKeeper的分布式锁. 以下将详细介绍如何正确地实现Redis分布式锁. 可靠性 首先,为了确保分布式锁的可 ...
- 第一篇:解析Linux是什么?能干什么?它的应用领域!
不得不说的前言(不看完睡觉会尿床):饿货们~!你说你们上学都学了点啥?这不懂那也不懂,快毕业了啥也不会.专业课程不学好毕业了也找不到好工作.爸妈给你养大,投资了多少钱.你毕业后随便找了个什么鸡毛工作开 ...
- Idea安装教程以及环境变量配置
IDEA安装以及JDK环境变量 环境变量配置 下载jdk
- 以正确的方式下载和配置 ASP.NET Core 官方源码
我们可以在Github上面直接查看ASP.NET Core 3.x的源代码,但是我们也可以把源代码下载下来进行查看. 而下载源代码进行查看有很多好处: 任意的导航源代码 内置了一个示例项目 直接调试源 ...
- node打开本地应用程序
1.打开浏览器 最简单的方法: const cp = require('child_process') cp.exec('start http://127.0.0.1:8889/'); // 自动打开 ...
- windows 安装 jenkins 自动化构建部署至linux服务器上
一.环境准备 1.git安装环境 参考链接 https://www.cnblogs.com/yuarvin/p/12500038.html 2.maven安装环境,包括jdk环境安装 参考链接 htt ...
- mybatis返回自增主键踩坑记
背景 MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以对配置和原生Map ...
- Ng-Matero V9 正式发布!
距离 Ng-Matero 第一版发布已经过去了半年多,该项目获得了越来越多的关注及喜爱,甚至得到了外国友人的赞助.借此项目也认识了很多对 Angular 和 Material 感兴趣的朋友,如今对项目 ...