|
@@ -8,57 +8,63 @@ import org.springframework.web.bind.annotation.RestController;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.*;
|
|
|
+import java.net.URLEncoder;
|
|
|
|
|
|
@RestController
|
|
|
@RequestMapping("upload")
|
|
|
@PassToken
|
|
|
public class UploadController {
|
|
|
|
|
|
- @GetMapping("downloadFile")
|
|
|
+ /**
|
|
|
+ * @param response
|
|
|
+ * @功能描述 下载文件:
|
|
|
+ */
|
|
|
+ @RequestMapping("/download")
|
|
|
@PassToken
|
|
|
- public String downloadFile(HttpServletRequest request, HttpServletResponse response) {
|
|
|
- String fileName = "tld-consumer-9560.jar";// 设置文件名,根据业务需要替换成要下载的文件名
|
|
|
- if (fileName != null) {
|
|
|
- //设置文件路径
|
|
|
- String realPath = "c:\\java";
|
|
|
- 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();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ public void download(HttpServletResponse response) {
|
|
|
+ String path = "E:\\pda\\tld.apk";
|
|
|
+ try {
|
|
|
+ // path是指想要下载的文件的路径
|
|
|
+ File file = new File(isLinux(path));
|
|
|
+ // 获取文件名
|
|
|
+ String filename = file.getName();
|
|
|
+ // 获取文件后缀名
|
|
|
+ String ext = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
|
|
|
+ // 将文件写入输入流
|
|
|
+ FileInputStream fileInputStream = new FileInputStream(file);
|
|
|
+ InputStream fis = new BufferedInputStream(fileInputStream);
|
|
|
+ byte[] buffer = new byte[fis.available()];
|
|
|
+ fis.read(buffer);
|
|
|
+ fis.close();
|
|
|
+
|
|
|
+ // 清空response
|
|
|
+ response.reset();
|
|
|
+ // 设置response的Header
|
|
|
+ response.setCharacterEncoding("UTF-8");
|
|
|
+ //Content-Disposition的作用:告知浏览器以何种方式显示响应返回的文件,用浏览器打开还是以附件的形式下载到本地保存
|
|
|
+ //attachment表示以附件方式下载 inline表示在线打开 "Content-Disposition: inline; filename=文件名.mp3"
|
|
|
+ // filename表示文件的默认名称,因为网络传输只支持URL编码的相关支付,因此需要将文件名URL编码后进行传输,前端收到后需要反编码才能获取到真正的名称
|
|
|
+ response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
|
|
|
+ // 告知浏览器文件的大小
|
|
|
+ response.addHeader("Content-Length", "" + file.length());
|
|
|
+ OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
|
|
|
+ response.setContentType("application/octet-stream");
|
|
|
+ outputStream.write(buffer);
|
|
|
+ outputStream.flush();
|
|
|
+ } catch (IOException ex) {
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * linux将磁盘转换成本地路径
|
|
|
+ * @param path
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String isLinux(String path) {
|
|
|
+ if(System.getProperty("os.name").toLowerCase().indexOf("linux") >= 0) {
|
|
|
+ path = path.replaceAll("E:", "/data").replaceAll("//","/").replaceAll("\\\\\\\\", "/").replaceAll("\\\\", "/");
|
|
|
}
|
|
|
- return null;
|
|
|
+ return path;
|
|
|
}
|
|
|
}
|