|
@@ -0,0 +1,63 @@
|
|
|
+package com.tld.controller;
|
|
|
+
|
|
|
+import com.tld.util.PassToken;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.*;
|
|
|
+
|
|
|
+@RestController
|
|
|
+@RequestMapping("upload")
|
|
|
+public class UploadController {
|
|
|
+
|
|
|
+ @GetMapping("downloadFile")
|
|
|
+ @PassToken
|
|
|
+ public String downloadFile(HttpServletRequest request, HttpServletResponse response) {
|
|
|
+ String fileName = "tld-consumer-9560.jar";// 设置文件名,根据业务需要替换成要下载的文件名
|
|
|
+ if (fileName != null) {
|
|
|
+ //设置文件路径
|
|
|
+ String realPath = "D:\\project\\tld-consumer-9560\\build\\libs";
|
|
|
+ File file = new File(realPath , fileName);
|
|
|
+ if (file.exists()) {
|
|
|
+ response.setContentType("application/octet-stream");//
|
|
|
+ response.setHeader("content-type", "application/octet-stream");
|
|
|
+ response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ FileInputStream fis = null;
|
|
|
+ BufferedInputStream bis = null;
|
|
|
+ try {
|
|
|
+ fis = new FileInputStream(file);
|
|
|
+ bis = new BufferedInputStream(fis);
|
|
|
+ OutputStream os = response.getOutputStream();
|
|
|
+ int i = bis.read(buffer);
|
|
|
+ while (i != -1) {
|
|
|
+ os.write(buffer, 0, i);
|
|
|
+ i = bis.read(buffer);
|
|
|
+ }
|
|
|
+ System.out.println("success");
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (bis != null) {
|
|
|
+ try {
|
|
|
+ bis.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (fis != null) {
|
|
|
+ try {
|
|
|
+ fis.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|