|
|
@@ -7,8 +7,18 @@ import cn.seecoder.fdroidrepository.DataObject.VO.BuildTaskVO;
|
|
|
import cn.seecoder.fdroidrepository.DataObject.VO.Response;
|
|
|
import cn.seecoder.fdroidrepository.Service.ServiceImpl.ApkService;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.core.io.FileSystemResource;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+
|
|
|
@RestController
|
|
|
@RequestMapping("/app")
|
|
|
public class AppController {
|
|
|
@@ -54,6 +64,48 @@ public class AppController {
|
|
|
return Response.buildSuccess(appDetailVO);
|
|
|
}
|
|
|
|
|
|
+ @GetMapping("/download")
|
|
|
+ public ResponseEntity<FileSystemResource> downloadFile(
|
|
|
+ @RequestParam Integer apkId,
|
|
|
+ @RequestParam String apkName,
|
|
|
+ @RequestHeader(value = "Range", required = false) String range) throws IOException {
|
|
|
+
|
|
|
+ String filePath = apkService.isExistsApk(apkName);
|
|
|
+ if(filePath==null){
|
|
|
+ return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
|
|
+ }
|
|
|
+ File file = new File(filePath);
|
|
|
+
|
|
|
+// apkService.addDownloadCount(apkId);
|
|
|
+ // 设置响应头
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
|
|
|
+ headers.setContentDispositionFormData("attachment", apkName);
|
|
|
+ headers.setContentLength(file.length());
|
|
|
+
|
|
|
+ // 处理断点续传
|
|
|
+ if (range != null && range.startsWith("bytes=")) {
|
|
|
+ String[] ranges = range.substring(6).split("-");
|
|
|
+ long start = Long.parseLong(ranges[0]);
|
|
|
+ long end = file.length() - 1;
|
|
|
+ if (ranges.length == 2) {
|
|
|
+ end = Long.parseLong(ranges[1]);
|
|
|
+ }
|
|
|
+
|
|
|
+ headers.set("Content-Range", "bytes " + start + "-" + end + "/" + file.length());
|
|
|
+ headers.setContentLength(end - start + 1);
|
|
|
+ InputStream inputStream = new FileInputStream(file);
|
|
|
+ inputStream.skip(start);
|
|
|
+ return new ResponseEntity<>(new FileSystemResource(file), headers, HttpStatus.PARTIAL_CONTENT);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 使用Streaming方式下载文件
|
|
|
+ InputStream inputStream = new FileInputStream(file);
|
|
|
+
|
|
|
+ // 返回ResponseEntity
|
|
|
+ return new ResponseEntity<>(new FileSystemResource(file), headers, HttpStatus.OK);
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
|
|
|
}
|