diff --git a/driver/src/main/java/xyz/longicorn/driver/controller/DownloadController.java b/driver/src/main/java/xyz/longicorn/driver/controller/DownloadController.java new file mode 100644 index 0000000..b4e034a --- /dev/null +++ b/driver/src/main/java/xyz/longicorn/driver/controller/DownloadController.java @@ -0,0 +1,56 @@ +package xyz.longicorn.driver.controller; + +import cn.hutool.core.net.URLEncodeUtil; +import cn.hutool.http.HttpUtil; +import lombok.SneakyThrows; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import xyz.longicorn.driver.pojo.FileInfo; +import xyz.longicorn.driver.service.FileService; + +import javax.annotation.Resource; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletResponse; +import java.io.FileInputStream; +import java.io.InputStream; +import java.net.URL; +import java.net.URLConnection; + +@RestController +public class DownloadController { + @Resource + private FileService fileService; + + @SneakyThrows + @RequestMapping("/download") + public void download(String id, HttpServletResponse response) { + System.out.println(id); + String[] idList = id.split(","); + final FileInfo file = fileService.getById(id); + // TODO 验证文件的信息 + + response.setHeader("Content-Disposition", "attachment; filename="+file.getName() + +"; filename*=utf-8''" + URLEncodeUtil.encode(file.getName())); + final ServletOutputStream os = response.getOutputStream(); // 获取网络输出流 + InputStream is = null; + if(file.getPath().startsWith("http")){ + // 对于网络文件 直接 先下载在输出 + URL url = new URL(file.getPath()); + URLConnection connection = url.openConnection(); // 通过网址打开一个连接 + is = connection.getInputStream();//获取文件内容的输入流 + }else{ + is = new FileInputStream(file.getPath()); // 获取文件输入流 + } + + byte[] bytes = new byte[10240]; + int len; + while ((len = is.read(bytes)) != -1) { + os.write(bytes, 0, len); + } + os.close(); + is.close(); + +// response.getWriter().println("xxx"); + } + +} diff --git a/web/src/pages/Share.vue b/web/src/pages/Share.vue index 35bfae8..20661fd 100644 --- a/web/src/pages/Share.vue +++ b/web/src/pages/Share.vue @@ -1,3 +1,111 @@ + @@ -41,13 +149,22 @@ {{ formatDate(shareInfo.createTime, 'YYYY-MM-DD HH:mm') }} {{ shareInfo.live == -1 ? '长期有效' : (shareInfo.live / 3600 / 24 + '天') }} + + 下载 + - + + + 返回上一级 + + + - + {{ file.row.name }} @@ -86,10 +203,9 @@ import FileIcon from "../components/FileIcon.vue"; export default { name: "Share", components: {AppLogo, FileIcon}, - setup(props) { + setup(props, ctx) { const route = useRoute(); const id = route.params.id // 获取动态路由的参数 实例: this.$route.params.id - /** * 分享信息有误 */ @@ -175,7 +291,7 @@ export default { }).catch(e => console.log(e.message)); } const router = useRouter() - watchEffect(()=>{ + watchEffect(() => { showShareFiles() }) // 显示下一级目录 @@ -184,98 +300,24 @@ export default { const currentPath = route.query.path || ''; router.push('?path=' + currentPath + '/' + file.name) }; + const fileTable = ref() + const downloadSelect = () => { + // 1.直接通过文件编号构建下载链接(今天使用) + // 2.通过文件编号创建下载任务,然后再下载(更好) + // console.log() + const url = 'http://localhost:8080/download?id=' + const idArr = []; + fileTable.value.getSelectionRows().forEach(f => idArr.push(f.id)); + // const idList = fileTable.value.getSelectionRows().map(f=>f.id) + // console.log(idArr,idList) + location.href = url + idArr.join(',') + + } return { shareError, loadingData, shareInfo, passwordBox, shareUser, - submitView, fileListData, showChild, - ...strings, + submitView, fileListData, showChild, fileTable, + ...strings, downloadSelect } } } - - \ No newline at end of file