# 上传下载
# fastDFS
fastDFS
完美集成fastDFS文件服务客户端, 支持断点续传, 分块上传, 文件秒传, 稳定的前后端组件(前端H5), 支持多用户并发上传文件校验(文件锁), 可以快速集成fastDFS文件服务, 免去繁琐的配置, 拿来即用, 目前暂时仅支持springboot开发环境
# fastDFS后端环境配置
# 修改pom文件
加入仓库地址:
<repositories>
<repository>
<id>gomyck-repo</id>
<name>Gomyck proxy Ali</name>
<url>http://nexus.gomyck.com/nexus/content/repositories/gomyck-repo/</url>
</repository>
<!-- 快照版本更新频繁, 如不想和作者保持同步, 请删除下面仓库地址并使用release版本: 1.0.1-release -->
<repository>
<id>gomyck-repo-snapshot</id>
<name>Gomyck proxy Ali Snapshot</name>
<url>http://nexus.gomyck.com/nexus/content/repositories/gomyck-repo-snapshot/</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
加入依赖:
<dependency>
<groupId>com.gomyck</groupId>
<artifactId>gomyck-fastdfs-spring-boot-starter</artifactId>
<version>1.0.2-SNAPSHOT</version>
</dependency>
2
3
4
5
# 修改配置文件
#单个文件上传大小限制
spring:
servlet:
multipart:
max-file-size: 5000MB
#fastdfs客户端配置
fdfs:
connect-timeout: 1601
thumb-image:
width: 150
height: 150
pool:
jmx-name-prefix: 1
jmx-name-base: 1
max-wait-millis: 102
tracker-list:
- 192.168.1.1:22122 #fastdfs服务地址
so-timeout: 1501
pool:
max-total: 153
gomyck:
config:
redis: true #是否使用redis存储文件上传信息以及上传锁
redis:
host: 127.0.0.1
password: xxxxx
port: 6379
fastdfs: #fastdfs上传配置
chunk-size: 5 #分块大小, 上传文件分块的大小 单位: MB
download-chunk-size: 100 #分块下载大小 单位: byte
group-id: group1 #fastdfs的组, 文件会被存到这个组下
file-server-protocol: http #远程文件服务连接协议
file-server-url: 192.168.1.196 #远程文件服务连接地址
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 增加配置类信息
在静态资源映射表中加入以下配置
@Configuration
public class config extends WebMvcConfigurationSupport {
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("ck-fastdfs/**").addResourceLocations("classpath:/META-INF/resources/ck-fastdfs/");
registry.addResourceHandler("ck-util/**").addResourceLocations("classpath:/META-INF/resources/ck-util/");
registry.addResourceHandler("ck-3pty/**").addResourceLocations("classpath:/META-INF/resources/ck-3pty/");
super.addResourceHandlers(registry);
}
}
2
3
4
5
6
7
8
9
10
# 启动服务
启动服务, 访问示例页面: host{:port}/{contextPath/}ck-fastdfs/view/ckFastDFS.html (可以渲染表示环境配置成功)
# 前端开发
# 引入必要js
host{:port}/{contextPath/}/ck-3pty/jquery/jquery-core.min.js
host{:port}/{contextPath/}/ck-fastdfs/js/webuploader.nolog.js
host{:port}/{contextPath/}/ck-fastdfs/js/ckFastDFS.js
2
3
# 实例化上传实例
const option = {
//config something.....
};
const cfd = new CkFastDFS(option);
2
3
4
每个实例可以绑定多个上传按钮, 支持id选择器, 类选择器等jq插件支持的选择器类型
多实例存在的场景: 多个文件服务分组, 当不同的按钮上传文件到不同分组时, 可能需要页面多实例来处理
# option说明
option参数说明:
{
baseURI: "../../", //后端服务URI(包括上下文) (非必填)
fastDFSGroup: "group1", //文件上传至fastdfs的组名 (非必填)
uploaderConfig: {}, //webUploader配置 (非必填)
uploadButton: { //按钮配置 (必填)
buttonId: "#btn1", //选择器 (必填), 支持jq插件所支持的所有selector类型
multiple: true //是否允许多文件选择 (非必填, 默认false)
},
uploadProgressBar: { //进度条 (非必填)
changeBar: function (refer, file, progressVal) { //文件上传中, 进度改变时会触发该方法
console.log("进度条改变: " + refer + "|||" + file.id + "|||" + progressVal);
}
},
uploadListener: { //上传监听(非必填)
// 参数说明:
// refer:点击上传的按钮jq对象
// file:上传的文件
// result:后端服务返回的结果
// reason:错误类型, 通常为字符串: server
//添加文件信息
appendFileInfo: function (refer, file) {
console.log("选择文件: " + refer + "|||" + file.id);
},
//添加到上传队列之前
beforeAppendFileInQueued: function (refer, file) {
console.log("添加到队列之前: " + refer + "|||" + file.id);
return true;
},
//开始上传
beginUpload: function (refer, file) {
console.log("开始上传: " + refer + "|||" + file.id);
},
//分块上传成功
chunkUploadSuccess: function (refer, file, result) {
console.log("分块上传成功:" + refer + "|||" + file.id + "|||" + JSON.stringify(result))
},
//上传出错
uploadError: function (refer, file, reason) {
console.log("上传失败: " + refer + "|||" + file.id + "|||" + reason);
},
//上传成功
uploadSuccess: function (refer, file, result) {
console.log("上传成功: " + refer + "|||" + file.id + "|||" + JSON.stringify(result));
},
//上传完成(不管上传成功失败, 都会触发该方法)
uploadComplete: function (refer, file) {
console.log("上传完成: " + refer + "|||" + file.id);
},
//全局错误
error: function (type, tips) {
console.log("全局错误: " + type + "|||" + tips)
}
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 实例函数说明
cfd.addButton(selector); //向实例中添加按钮
cfd.pauseUpload(param); //暂停上传 param 为布尔值时, 为暂停正在上传的文件, file类型时, 暂停指定file的上传, null|undefined时为全部暂停
cfd.cancleUpload(file); //取消指定文件的上传
2
3
# 其他说明
- 如何在自己的项目中添加fastdfs客户端实例:
- 使用@Autowired即可注入以下接口实例
- TrackerClient - TrackerServer接口
- GenerateStorageClient - 一般文件存储接口 (StorageServer接口)
- FastFileStorageClient - 为方便项目开发集成的简单接口(StorageServer接口)
- AppendFileStorageClient - 支持文件续传操作的接口 (StorageServer接口)
- 如何扩展前端js:
- 请参考类:CkFastDFS.js
- 请参考webUploader官网API
- 当然, 你也可以fork master分支代码, 打包后供他人使用(修改后的分支代码, pom文件中的parent请删掉, 并手动指定依赖版本, 否则可能会出现版本不一致问题)
# JAVA实现大文件多线程下载
# HTTP协议Range请求头
Range主要是针对只需要获取部分资源的范围请求,通过指定Range即可告知服务器资源的指定范围。格式: Range: bytes=start-end
比如: 获取字节范围 5001-10000,Range: bytes=5001-10000
也可以指定开始位置不指定结束位置,表示获取开始位置之后的全部数据Range: bytes=5001-
服务器接收到带有Range的请求,会在处理请求之后返回状态码为206 Partial Content的响应。基于Range的特性,我们就可以实现文件的多线程下载,文件的断点续传
# JAVA实现大文件多线程下载准备工作
需要知道每秒传输的字节数是多少,为了监控传输数据的过程,我们需要了解SpringMVC中的接口ResponseExtractor
# 基于Spring Boot的文件上传下载
- 限制文件大小
spring:
servlet:
multipart:
max-request-size: 50MB
max-file-size: 50MB
2
3
4
5
- service
import com.ramostear.springboot.uploadfile.service.FileStorageService;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Service;
import org.springframework.util.FileSystemUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
@Service("fileStorageService")
public class FileStorageServiceImpl implements FileStorageService {
private final Path path = Paths.get("fileStorage");
@Override
public void init() {
try {
Files.createDirectory(path);
} catch (IOException e) {
throw new RuntimeException("Could not initialize folder for upload!");
}
}
@Override
public void save(MultipartFile multipartFile) {
try {
Files.copy(multipartFile.getInputStream(),this.path.resolve(multipartFile.getOriginalFilename()));
} catch (IOException e) {
throw new RuntimeException("Could not store the file. Error:"+e.getMessage());
}
}
@Override
public Resource load(String filename) {
Path file = path.resolve(filename);
try {
Resource resource = new UrlResource(file.toUri());
if(resource.exists() || resource.isReadable()){
return resource;
}else{
throw new RuntimeException("Could not read the file.");
}
} catch (MalformedURLException e) {
throw new RuntimeException("Error:"+e.getMessage());
}
}
@Override
public Stream<Path> load() {
try {
return Files.walk(this.path,1)
.filter(path -> !path.equals(this.path))
.map(this.path::relativize);
} catch (IOException e) {
throw new RuntimeException("Could not load the files.");
}
}
@Override
public void clear() {
FileSystemUtils.deleteRecursively(path.toFile());
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
- controller
import com.ramostear.springboot.uploadfile.service.FileStorageService;
import com.ramostear.springboot.uploadfile.valueobject.Message;
import com.ramostear.springboot.uploadfile.valueobject.UploadFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
import java.util.List;
import java.util.stream.Collectors;
/**
* @ClassName FileUploadController
* @Description TODO
* @Author 树下魅狐
* @Date 2020/4/28 0028 18:52
* @Version since 1.0
**/
@RestController
public class FileUploadController {
@Autowired
FileStorageService fileStorageService;
@PostMapping("/upload")
public ResponseEntity<Message> upload(@RequestParam("file")MultipartFile file){
try {
fileStorageService.save(file);
return ResponseEntity.ok(new Message("Upload file successfully: "+file.getOriginalFilename()));
}catch (Exception e){
return ResponseEntity.badRequest()
.body(new Message("Could not upload the file:"+file.getOriginalFilename()));
}
}
@GetMapping("/files")
public ResponseEntity<List<UploadFile>> files(){
List<UploadFile> files = fileStorageService.load()
.map(path -> {
String fileName = path.getFileName().toString();
String url = MvcUriComponentsBuilder
.fromMethodName(FileUploadController.class,
"getFile",
path.getFileName().toString()
).build().toString();
return new UploadFile(fileName,url);
}).collect(Collectors.toList());
return ResponseEntity.ok(files);
}
@GetMapping("/files/{filename:.+}")
public ResponseEntity<Resource> getFile(@PathVariable("filename")String filename){
Resource file = fileStorageService.load(filename);
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION,
"attachment;filename=\""+file.getFilename()+"\"")
.body(file);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
- 启动时操作,清理历史残留
@Service
public class FileUploadConfiguration implements CommandLineRunner {
@Autowired
FileStorageService fileStorageService;
@Override
public void run(String... args) throws Exception {
fileStorageService.clear();
fileStorageService.init();
}
}
2
3
4
5
6
7
8
9
10
11
12
# java里设置下载文件的content-type
# 一、Content-type基本概念
HTTP协议提供了Content-Type实体首部字段来描述报文实体的媒体格式,说明请求或返回的消息是用什么格式进行编码的,在request header和response header里都有存在。 用来向服务器或者浏览器说明传输的文件格式,以便服务器和浏览器按照正确的格式进行解析。在最初的的http post请求只支持application/x-www-form-urlencoded,参数是通过浏览器的url进行传递,但此种方法不支持文件上传,所以后来Content-type 扩充了multipart/form-data类型以支持向服务器发送二进制数据,以及随着后面web应用的日益发展增加了application/json的类型
# 二、MIME类型说明:
语法结构
Content-Type: [type]/[subtype];parameter
- 1、type 有下面的形式:
- Text:用于标准化地表示的文本信息,文本消息可以是多种字符集和或者多种格式的;
- Multipart:用于连接消息体的多个部分构成一个消息,这些部分可以是不同类型的数据;
- Application:用于传输应用程序数据或者二进制数据;
- Message:用于包装一个E-mail消息;
- Image:用于传输静态图片数据;
- Audio:用于传输音频或者音声数据;
- Video:用于传输动态影像数据,可以是与音频编辑在一起的视频数据格式。
- 2、subtype 用于指定 type的详细形式。“type/subtype”配对的集合和与此相关的参数。下面是最经常用到的一些 MIME 类型:
- text/html(HTML 文档);
- text/plain(纯文本);
- text/css(CSS 样式表);
- image/gif(GIF 图像);
- image/jpeg(JPG 图像);
- application/x-javascript(JavaScript 脚本);
- application/x-shockwave-flash(Flash);
- application/x- www-form-urlencoded(使用 HTTP 的 POST方法提交的表单);
- multipart/form-data(同上,但主要用于表单提交时伴随文件上传的场合)。
- 3、常用类型
| 类型 | 描述 | 典型示例 |
|---|---|---|
| text | 表明文件是普通文本,理论上是人类可读 | text/plain, text/html, text/css, text/javascript |
| image | 表明是某种图像。不包括视频,但是动态图(比如动态gif)也使用image类型 | image/gif, image/png, image/jpeg, image/bmp, image/webp, image/x-icon, image/vnd.microsoft.icon |
| audio | 表明是某种音频文件 | audio/midi, audio/mpeg, audio/webm, audio/ogg, audio/wav |
| video | 表明是某种视频文件 | video/webm, video/ogg |
| application | 表明是某种二进制数据 | application/octet-stream, application/pkcs12, application/vnd.mspowerpoint, application/xhtml+xml, application/xml, application/pdf |
# 六、Mime 类型列表
office 所有后缀对应的 content-type
| 类型/子类型 | 扩展名 |
|---|---|
| application/msword | .doc |
| application/msword | .dot |
| application/vnd.openxmlformats-officedocument.wordprocessingml.document | .docx |
| application/vnd.openxmlformats-officedocument.wordprocessingml.template | .dotx |
| application/vnd.ms-word.document.macroEnabled.12 | .docm |
| application/vnd.ms-word.template.macroEnabled.12 | .dotm |
| application/vnd.ms-excel | .xls |
| application/vnd.ms-excel | .xlt |
| application/vnd.ms-excel | .xla |
| application/vnd.openxmlformats-officedocument.spreadsheetml.sheet | .xlsx |
| application/vnd.openxmlformats-officedocument.spreadsheetml.template | .xltx |
| application/vnd.ms-excel.sheet.macroEnabled.12 | .xlsm |
| application/vnd.ms-excel.template.macroEnabled.12 | .xltm |
| application/vnd.ms-excel.addin.macroEnabled.12 | .xlam |
| application/vnd.ms-excel.sheet.binary.macroEnabled.12 | .xlsb |
| application/vnd.ms-powerpoint | .ppt |
| application/vnd.ms-powerpoint | .pot |
| application/vnd.ms-powerpoint | .pps |
| application/vnd.ms-powerpoint | .ppa |
| application/vnd.openxmlformats-officedocument.presentationml.presentation | .pptx |
| application/vnd.openxmlformats-officedocument.presentationml.template | .potx |
| application/vnd.openxmlformats-officedocument.presentationml.slideshow | .ppsx |
| application/vnd.ms-powerpoint.addin.macroEnabled.12 | .ppam |
| application/vnd.ms-powerpoint.presentation.macroEnabled.12 | .pptm |
| application/vnd.ms-powerpoint.presentation.macroEnabled.12 | .potm |
| application/vnd.ms-powerpoint.slideshow.macroEnabled.12 | .ppsm |
其他后缀对应的 content-type
| 类型/子类型 | 扩展名 |
|---|---|
| application/envoy | evy |
| application/fractals | fif |
| application/futuresplash | spl |
| application/hta | hta |
| application/internet-property-stream | acx |
| application/mac-binhex40 | hqx |
| application/msword | doc |
| application/msword | dot |
| application/octet-stream | * |
| application/octet-stream | bin |
| application/octet-stream | class |
| application/octet-stream | dms |
| application/octet-stream | exe |
| application/octet-stream | lha |
| application/octet-stream | lzh |
| application/oda | oda |
| application/olescript | axs |
| application/pdf | |
| application/pics-rules | prf |
| application/pkcs10 | p10 |
| application/pkix-crl | crl |
| application/postscript | ai |
| application/postscript | eps |
| application/postscript | ps |
| application/rtf | rtf |
| application/set-payment-initiation | setpay |
| application/set-registration-initiation | setreg |
| application/vnd.ms-excel | xla |
| application/vnd.ms-excel | xlc |
| application/vnd.ms-excel | xlm |
| application/vnd.ms-excel | xls |
| application/vnd.ms-excel | xlt |
| application/vnd.ms-excel | xlw |
| application/vnd.ms-outlook | msg |
| application/vnd.ms-pkicertstore | sst |
| application/vnd.ms-pkiseccat | cat |
| application/vnd.ms-pkistl | stl |
| application/vnd.ms-powerpoint | pot |
| application/vnd.ms-powerpoint | pps |
| application/vnd.ms-powerpoint | ppt |
| application/vnd.ms-project | mpp |
| application/vnd.ms-works | wcm |
| application/vnd.ms-works | wdb |
| application/vnd.ms-works | wks |
| application/vnd.ms-works | wps |
| application/winhlp | hlp |
| application/x-bcpio | bcpio |
| application/x-cdf | cdf |
| application/x-compress | z |
| application/x-compressed | tgz |
| application/x-cpio | cpio |
| application/x-csh | csh |
| application/x-director | dcr |
| application/x-director | dir |
| application/x-director | dxr |
| application/x-dvi | dvi |
| application/x-gtar | gtar |
| application/x-gzip | gz |
| application/x-hdf | hdf |
| application/x-internet-signup | ins |
| application/x-internet-signup | isp |
| application/x-iphone | iii |
| application/x-javascript | js |
| application/x-latex | latex |
| application/x-msaccess | mdb |
| application/x-mscardfile | crd |
| application/x-msclip | clp |
| application/x-msdownload | dll |
| application/x-msmediaview | m13 |
| application/x-msmediaview | m14 |
| application/x-msmediaview | mvb |
| application/x-msmetafile | wmf |
| application/x-msmoney | mny |
| application/x-mspublisher | pub |
| application/x-msschedule | scd |
| application/x-msterminal | trm |
| application/x-mswrite | wri |
| application/x-netcdf | cdf |
| application/x-netcdf | nc |
| application/x-perfmon | pma |
| application/x-perfmon | pmc |
| application/x-perfmon | pml |
| application/x-perfmon | pmr |
| application/x-perfmon | pmw |
| application/x-pkcs12 | p12 |
| application/x-pkcs12 | pfx |
| application/x-pkcs7-certificates | p7b |
| application/x-pkcs7-certificates | spc |
| application/x-pkcs7-certreqresp | p7r |
| application/x-pkcs7-mime | p7c |
| application/x-pkcs7-mime | p7m |
| application/x-pkcs7-signature | p7s |
| application/x-sh | sh |
| application/x-shar | shar |
| application/x-shockwave-flash | swf |
| application/x-stuffit | sit |
| application/x-sv4cpio | sv4cpio |
| application/x-sv4crc | sv4crc |
| application/x-tar | tar |
| application/x-tcl | tcl |
| application/x-tex | tex |
| application/x-texinfo | texi |
| application/x-texinfo | texinfo |
| application/x-troff | roff |
| application/x-troff | t |
| application/x-troff | tr |
| application/x-troff-man | man |
| application/x-troff-me | me |
| application/x-troff-ms | ms |
| application/x-ustar | ustar |
| application/x-wais-source | src |
| application/x-x509-ca-cert | cer |
| application/x-x509-ca-cert | crt |
| application/x-x509-ca-cert | der |
| application/ynd.ms-pkipko | pko |
| application/zip | zip |
| audio/basic | au |
| audio/basic | snd |
| audio/mid | mid |
| audio/mid | rmi |
| audio/mpeg | mp3 |
| audio/x-aiff | aif |
| audio/x-aiff | aifc |
| audio/x-aiff | aiff |
| audio/x-mpegurl | m3u |
| audio/x-pn-realaudio | ra |
| audio/x-pn-realaudio | ram |
| audio/x-wav | wav |
| image/bmp | bmp |
| image/cis-cod | cod |
| image/gif | gif |
| image/ief | ief |
| image/jpeg | jpe |
| image/jpeg | jpeg |
| image/jpeg | jpg |
| image/pipeg | jfif |
| image/svg+xml | svg |
| image/tiff | tif |
| image/tiff | tiff |
| image/x-cmu-raster | ras |
| image/x-cmx | cmx |
| image/x-icon | ico |
| image/x-portable-anymap | pnm |
| image/x-portable-bitmap | pbm |
| image/x-portable-graymap | pgm |
| image/x-portable-pixmap | ppm |
| image/x-rgb | rgb |
| image/x-xbitmap | xbm |
| image/x-xpixmap | xpm |
| image/x-xwindowdump | xwd |
| message/rfc822 | mht |
| message/rfc822 | mhtml |
| message/rfc822 | nws |
| text/css | css |
| text/h323 | 323 |
| text/html | htm |
| text/html | html |
| text/html | stm |
| text/iuls | uls |
| text/plain | bas |
| text/plain | c |
| text/plain | h |
| text/plain | txt |
| text/richtext | rtx |
| text/scriptlet | sct |
| text/tab-separated-values | tsv |
| text/webviewhtml | htt |
| text/x-component | htc |
| text/x-setext | etx |
| text/x-vcard | vcf |
| video/mpeg | mp2 |
| video/mpeg | mpa |
| video/mpeg | mpe |
| video/mpeg | mpeg |
| video/mpeg | mpg |
| video/mpeg | mpv2 |
| video/quicktime | mov |
| video/quicktime | qt |
| video/x-la-asf | lsf |
| video/x-la-asf | lsx |
| video/x-ms-asf | asf |
| video/x-ms-asf | asr |
| video/x-ms-asf | asx |
| video/x-msvideo | avi |
| video/x-sgi-movie | movie |
| x-world/x-vrml | flr |
| x-world/x-vrml | vrml |
| x-world/x-vrml | wrl |
| x-world/x-vrml | wrz |
| x-world/x-vrml | xaf |
| x-world/x-vrml | xof |