-
Notifications
You must be signed in to change notification settings - Fork 518
增加一个Spring分段下载示例 #216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LoopOuroboros
wants to merge
3
commits into
tobato:master
Choose a base branch
from
LoopOuroboros:Branch_PartDownload
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
增加一个Spring分段下载示例 #216
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/test/java/com/github/tobato/fastdfs/controller/FileDownloadDemoController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package com.github.tobato.fastdfs.controller; | ||
|
|
||
| import com.github.tobato.fastdfs.service.DefaultGenerateStorageClient; | ||
| import org.apache.commons.io.IOUtils; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.http.*; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RequestHeader; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| import javax.servlet.http.HttpServletResponse; | ||
| import java.net.URLConnection; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * @author MagakiReimu | ||
| * @version 1.0 | ||
| */ | ||
| @RestController | ||
| @RequestMapping("/download") | ||
| public class FileDownloadDemoController { | ||
|
|
||
| @Autowired | ||
| private DefaultGenerateStorageClient defaultGenerateStorageClient; | ||
|
|
||
| @GetMapping("/demo") | ||
| public void downloadDemo(@RequestHeader(value = HttpHeaders.RANGE, required = false) String rangeHeader, | ||
| HttpServletResponse response) { | ||
| // 以下数据,请从自己的开发系统中获取 | ||
| String fileName = "abc.txt"; | ||
| String group = "group1"; | ||
| String path = "xxxxx"; | ||
| long fileSize = 0L; | ||
|
|
||
| // 分段下载计算参数 | ||
| long rangeStart = 0L; | ||
| long rangeEnd = 0L; | ||
| long downloadSize = 0L; | ||
| long contentLength = fileSize; | ||
|
|
||
| // 获取分段下载请求 | ||
| List<HttpRange> httpRangeList = HttpRange.parseRanges(rangeHeader); | ||
| if (httpRangeList.size() == 0) { | ||
| // 无分段下载请求,下载为全量下载 | ||
| // 调用downloadFile下载整个文件, 调用接口为只有group, path参数时 | ||
| // 下载接口统一, 保持二次调用中rangeStart, downloadSize为0 | ||
| // 普通下载状态码200 | ||
| response.setStatus(HttpStatus.OK.value()); | ||
| } else if (httpRangeList.size() == 1) { | ||
| // 分段下载只解析第一个,一般请求只包含一个 | ||
| HttpRange httpRange = httpRangeList.get(0); | ||
| // 请求起始字节 | ||
| rangeStart = httpRange.getRangeStart(fileSize); | ||
| // 请求结束字节 | ||
| rangeEnd = httpRange.getRangeEnd(fileSize); | ||
| // 请求分段长度 | ||
| contentLength = rangeEnd - rangeStart + 1L; | ||
| // 请求下载长度 | ||
| downloadSize = contentLength; | ||
| // Content-Range: bytes [unit first byte pos] - [last byte pos] / [entity length] | ||
| String contentRange = String.format("bytes %d-%d/%d", rangeStart, rangeEnd, fileSize); | ||
| response.addHeader(HttpHeaders.CONTENT_RANGE, contentRange); | ||
| // 分段下载状态码206 | ||
| response.setStatus(HttpStatus.PARTIAL_CONTENT.value()); | ||
| } else { | ||
| // 协议支持一个请求包含多段下载, Demo不做多段解析 | ||
| response.setStatus(HttpStatus.BAD_REQUEST.value()); | ||
| return; | ||
| } | ||
|
|
||
| // Content-Type | ||
| // 方式一:写死Content-Type | ||
| MediaType contentType = MediaType.APPLICATION_OCTET_STREAM; | ||
| // 方式二:根据文件名猜测Content-Type | ||
| contentType = MediaType.parseMediaType(URLConnection.guessContentTypeFromName(fileName)); | ||
| response.setContentType(contentType.toString()); | ||
| // Content-Disposition | ||
| //@formatter:off | ||
| ContentDisposition contentDisposition = ContentDisposition | ||
| .builder("form-data") | ||
| .name("attachment") | ||
| .filename(fileName, StandardCharsets.UTF_8) | ||
| .build(); | ||
| //@formatter:on | ||
| response.addHeader(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString()); | ||
| // 本次下载大小 | ||
| response.setContentLengthLong(contentLength); | ||
| // 支持分段下载 | ||
| response.addHeader(HttpHeaders.ACCEPT_RANGES, "bytes"); | ||
|
|
||
| // 根据上方计算值,下载区间数据 | ||
| defaultGenerateStorageClient.downloadFile(group, path, rangeStart, downloadSize, | ||
| inputStream -> { | ||
| IOUtils.copy(inputStream, response.getOutputStream()); | ||
| return null; | ||
| }); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.