src/main/java/com/zy/crm/common/service/OssService.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/zy/crm/manager/controller/ContractController.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/zy/crm/manager/controller/PlanController.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/webapp/static/js/contract/contract.js | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/webapp/static/js/plan/plan.js | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
src/main/java/com/zy/crm/common/service/OssService.java
@@ -21,6 +21,7 @@ import java.io.*; import java.net.URL; import java.nio.file.Path; import java.text.SimpleDateFormat; import java.util.Date; import java.nio.file.StandardCopyOption; import java.nio.file.Files; @@ -88,10 +89,7 @@ } @Deprecated public String upload(InputStream inputStream, String suffix,String contentType){ if(suffix.trim().equals("file")){ suffix="jpg"; } public String upload(InputStream inputStream, String filename, String contentType) { OSSClient ossClient = new OSSClient(OSS_ENDPOINT, accessKeyId, accessKeySecret); String name = ""; try { @@ -99,7 +97,8 @@ ossClient.createBucket(bucket); log.info("您的Bucket不存在,创建Bucket:{}",bucket); } name = String.valueOf(snowflakeIdWorker.nextId()).concat(".").concat(suffix); SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd_HHmmss"); name = format.format(new Date()) + "_" + filename; // 添加 ContentType ObjectMetadata objectMetadata = new ObjectMetadata(); // objectMetadata.setContentType("image/jpg"); @@ -137,24 +136,24 @@ return true; } /** * 下载文件 * @param filePath OSS路径 */ public ResponseEntity<InputStreamResource> downloadFile(String filePath) throws IOException { OSSClient ossClient = new OSSClient(OSS_ENDPOINT, accessKeyId, accessKeySecret); OSSObject ossObject = ossClient.getObject(bucket, filePath); InputStream inputStream = ossObject.getObjectContent(); // 创建响应实体,将输出流作为文件内容返回 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); headers.setContentDispositionFormData("attachment", "output.docx"); InputStreamResource resource = new InputStreamResource(convertToByteArrayInputStream(inputStream)); return ResponseEntity.ok() .headers(headers) .body(resource); } // /** // * 下载文件 // * @param filePath OSS路径 // */ // public ResponseEntity<InputStreamResource> downloadFile(String filePath) throws IOException { // OSSClient ossClient = new OSSClient(OSS_ENDPOINT, accessKeyId, accessKeySecret); // OSSObject ossObject = ossClient.getObject(bucket, filePath); // InputStream inputStream = ossObject.getObjectContent(); // // // 创建响应实体,将输出流作为文件内容返回 // HttpHeaders headers = new HttpHeaders(); // headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); // headers.setContentDispositionFormData("attachment", "output.docx"); // InputStreamResource resource = new InputStreamResource(convertToByteArrayInputStream(inputStream)); // return ResponseEntity.ok() // .headers(headers) // .body(resource); // } public static ByteArrayInputStream convertToByteArrayInputStream(InputStream inputStream) throws IOException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); @@ -167,51 +166,13 @@ return new ByteArrayInputStream(data); } /** * 下载OSS文件 */ // public void download(String name) { // OSSClient ossClient = new OSSClient(OSS_ENDPOINT, accessKeyId, accessKeySecret); // try { // OSSObject ossObject = ossClient.getObject(bucket, name); // InputStream inputStream = ossObject.getObjectContent(); // // do transfer // inputStream.close(); // log.info("OSS文件下载成功: {}", name); // } catch (Exception e){ // e.printStackTrace(); // } finally { // ossClient.shutdown(); // } // } @Deprecated public String download(String name) { OSSClient ossClient = new OSSClient(OSS_ENDPOINT, accessKeyId, accessKeySecret); try { OSSObject ossObject = ossClient.getObject(bucket, name); InputStream inputStream = ossObject.getObjectContent(); // 获取默认下载目录 String defaultDownloadDir = System.getProperty("user.home") + "/Downloads"; // 创建文件保存路径 Path filePath = Paths.get(defaultDownloadDir, name); // 将输入流复制到文件 Files.copy(inputStream, filePath, StandardCopyOption.REPLACE_EXISTING); // 关闭输入流 inputStream.close(); log.info("OSS文件下载成功,保存路径: {}", filePath); return ""+filePath; } catch (Exception e) { e.printStackTrace(); } finally { ossClient.shutdown(); } return ""; //获取临时下载链接 URL url = ossClient.generatePresignedUrl(bucket, name, new Date(new Date().getTime() + 3600 * 1000)); log.info("OSS文件临时下载路径,URL路径: {}", url); return url.toString(); } /** src/main/java/com/zy/crm/manager/controller/ContractController.java
@@ -271,7 +271,7 @@ //文件后缀名 String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); //上传文件名 String filename = format.format(new Date()) + suffix; String filename = format.format(new Date()) + "_" + file.getOriginalFilename(); //最终文件路径 String filepath = path + "/" + filename; //OSS文件存储路径 @@ -299,20 +299,17 @@ @RequestMapping(value = "/contract/download/auth") @ManagerAuth(memo = "下载合同") public ResponseEntity<InputStreamResource> download(@RequestParam("id") Integer id) { public R download(@RequestParam("id") Integer id) { Contract contract = contractService.selectById(id); if (contract == null) { return null; return R.error(); } if (Cools.isEmpty(contract.getFilepath())) { return null; return R.error(); } try { return ossService.downloadFile(contract.getFilepath());//从OSS中下载文件 } catch (Exception e) { return null; } String download = ossService.download(contract.getFilepath());//获取OSS临时下载URL return R.ok().add(download); } @RequestMapping(value = "/contract/export/auth") src/main/java/com/zy/crm/manager/controller/PlanController.java
@@ -262,8 +262,7 @@ InputStream inputStream = file.getInputStream(); // 获取文件的内容类型 String contentType = file.getContentType(); String suffix = getFileSuffix(file.getOriginalFilename()); String url = ossService.upload(inputStream, suffix,contentType); String url = ossService.upload(inputStream, file.getOriginalFilename(), contentType); // String url = "http://tjdt.oss-cn-hangzhou.aliyuncs.com/"+file.getOriginalFilename(); String[] names = url.split("com/"); String name= names[1]; src/main/webapp/static/js/contract/contract.js
@@ -432,30 +432,28 @@ headers: {'token': localStorage.getItem('token')}, data: data, method: 'GET', xhrFields: { responseType: "blob" // 设置响应类型为二进制数据 }, success: function (res) { // 创建一个临时的下载链接 const url = window.URL.createObjectURL(res); // 创建一个隐藏的 <a> 元素并设置下载链接 const a = document.createElement("a"); a.style.display = "none"; a.href = url; let list = data.filepath.split(".") let suffix = "." + list[list.length - 1]//获取后缀名 a.download = data.name + suffix; // 指定下载的文件名 document.body.appendChild(a); // 触发点击事件以开始下载 a.click(); // 清理临时资源 setTimeout(function () { window.URL.revokeObjectURL(url); document.body.removeChild(a); }, 100); location.href = res.data // // 创建一个临时的下载链接 // const url = window.URL.createObjectURL(res); // // 创建一个隐藏的 <a> 元素并设置下载链接 // const a = document.createElement("a"); // a.style.display = "none"; // a.href = url; // // let list = data.filepath.split(".") // let suffix = "." + list[list.length - 1]//获取后缀名 // a.download = data.name + suffix; // 指定下载的文件名 // document.body.appendChild(a); // // // 触发点击事件以开始下载 // a.click(); // // // 清理临时资源 // setTimeout(function () { // window.URL.revokeObjectURL(url); // document.body.removeChild(a); // }, 100); } }); } src/main/webapp/static/js/plan/plan.js
@@ -646,7 +646,9 @@ data: { downloadUrl: downloadUrl }, // 传递下载链接作为参数 success: function(response) { console.log(response) layer.msg(response.msg+',已下载至默认路径!详细下载路径:'+response.data, {icon: 1}); // layer.msg(response.msg+',已下载至默认路径!详细下载路径:'+response.data, {icon: 1}); layer.msg(response.msg, {icon: 1}); location.href = response.data // // window.open(baseUrl+response.msg) // // 创建一个Blob对象 // var blob = new Blob([response]);