开发工具  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(上传 | 下载 | 删除)的更多相关文章

  1. coding++: java 操作FastDFS(上传 | 下载 | 删除)

    package cn.com.soundrecording.controller; import cn.com.soundrecording.utils.FastDFSClient;import co ...

  2. JAVA 实现FTP上传下载(sun.net.ftp.FtpClient)

    package com.why.ftp; import java.io.DataInputStream; import java.io.File; import java.io.FileInputSt ...

  3. jm解决乱码问题-参数化-数据库操作-文件上传下载

    jm解决乱码问题-参数化-数据库操作-文件上传下载 如果JM出果运行结果是乱码(解决中文BODY乱码的问题) 找到JM的安装路径,例如:C:\apache-jmeter-3.1\bin 用UE打开jm ...

  4. CentOS下安装配置NFS并通过Java进行文件上传下载

    1:安装NFS (1)安装 yum install nfs-utils rpcbind (2)启动rpcbind服务 systemctl restart rpcbind.service 查看服务状态 ...

  5. java实现文件上传下载

    喜欢的朋友可以关注下,粉丝也缺. 今天发现已经有很久没有给大家分享一篇技术文章了,于是想了一下给大家分享一篇java实现文件上传下载功能的文章,不喜欢的希望大家勿喷. 想必大家都知道文件的上传前端页面 ...

  6. FastDFS上传/下载过程[转载-经典图列]

    FastDFS上传/下载过程: 首先客户端 client 发起对 FastDFS 的文件传输动作,是通过连接到某一台 Tracker Server 的指定端口来实现的,Tracker Server 根 ...

  7. fastDFS与java整合文件上传下载

    准备 下载fastdfs-client-java源码 源码地址 密码:s3sw 修改pom.xml 第一个plugins是必需要的,是maven用来编译的插件,第二个是maven打源码包的,可以不要. ...

  8. java FTP 上传下载删除文件

    在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件.下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在commons-net包中)实现上传下载文件 ...

  9. FasfDFS整合Java实现文件上传下载

    文章目录     一 : 添加配置文件     二 : 加载配置文件         1. 测试加载配置文件         2. 输出配置文件     三:功能实现         1.初始化连接信 ...

  10. FasfDFS整合Java实现文件上传下载功能实例详解

    https://www.jb51.net/article/120675.htm 在上篇文章给大家介绍了FastDFS安装和配置整合Nginx-1.13.3的方法,大家可以点击查看下. 今天使用Java ...

随机推荐

  1. Object-Oriented Programming Summary Ⅱ

    电梯作业总结博客 17373492 电梯,多线程学习中的 "HelloWorld",早在大一就有所耳闻,以至于在坐电梯的时候就思考过:电梯需要怎么写呢? 0. 前言: 偶然的机会, ...

  2. 峰哥说技术:07-SpringBoot 正好Thymeleaf视图

    Spring Boot深度课程系列 峰哥说技术—2020庚子年重磅推出.战胜病毒.我们在行动 07  峰哥说技术:SpringBoot 正好Thymeleaf视图 Spring Boot视图介绍 虽然 ...

  3. 聊聊CAS - 面试官最喜欢问的并发编程专题

    什么是CAS 学习Java并发编程,CAS(Compare And Set)机制都是一个不得不掌握的知识点.除了通过synchronized进行并发控制外,还可以通过CAS的方式控制,大家熟悉的Ree ...

  4. 【字节校招】【实习】【内推】字节跳动春招(校招或实习均可)以及日常实习内推ing

    本人是年前刚刚入职抖音的应届生,职业认证还未来的级更改,但是这些都不重要.重要的是我们不能错过优秀的你~ 字节跳动的相关福利我就不介绍了,技术实习生是400/天,房补是1500/月,三餐免费,下午茶, ...

  5. python学习-练习题兔子生长问题巩固

    有一对兔子,一个月之后成熟,成熟之后每个月会生出一对兔子,理想状态下兔子不会死,请问n个月后有多少兔子? 分析:第一个月:1 第二个月:1 第三个月:2 第四个月:3 第五个月:5 第六个月:8 从前 ...

  6. C语言程序设计(一) 为什么要学C语言

    第一章 为什么要学C语言 学编程的过程,其实就是学习怎样用编程语言说话,让编译器听懂的过程. 汇编语言缺少“可移植性” 除了机器语言和汇编语言以外,几乎所有的编程语言都被统称为高级语言,它的特点是更接 ...

  7. 搭建Java开发环境之配置环境变量

    前言 初学Java不久的二胖在搭建Java开发环境一步就遇到问题,他不由得感叹:万事开头难啊!但感叹之后它还是鼓足了劲去努力解决它,二胖在电脑上Google了一番,最终环境是搭建成功了,但他心中对&q ...

  8. 浅析js中的堆和栈

    这里先说两个概念:1.堆(heap)2.栈(stack)堆 是堆内存的简称.栈 是栈内存的简称.说到堆栈,我们讲的就是内存的使用和分配了,没有寄存器的事,也没有硬盘的事.各种语言在处理堆栈的原理上都大 ...

  9. vue--基础应用 全选

    1.用computed实现全选 <body> <div id="app"> <input type="checkbox" v-mo ...

  10. Day1T3小w的魔术扑克——图论

    为什么不搞\(T2\)??? 因为我太菜了,那题我是真的搞不出来 题目描述 链接:https://ac.nowcoder.com/acm/contest/1100/C 来源:牛客网 小\(w\)喜欢打 ...