From 691dee7a71996cd99fdfe30bd61b2da95cc88ecd Mon Sep 17 00:00:00 2001
From: ANDRU-PC\Andru <769883346@qq.com>
Date: 星期一, 18 九月 2023 16:04:51 +0800
Subject: [PATCH] 添加工具类方法

---
 src/main/java/com/hx/util/DownFileUtil.java |   76 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 76 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/hx/util/DownFileUtil.java b/src/main/java/com/hx/util/DownFileUtil.java
index 26be2ac..a6535b0 100644
--- a/src/main/java/com/hx/util/DownFileUtil.java
+++ b/src/main/java/com/hx/util/DownFileUtil.java
@@ -146,4 +146,80 @@
 		}
 	}
 
+	/**
+	 * 涓嬭浇鏂囦欢宸ュ叿锛堟彁绀洪�夋嫨璺緞锛�
+	 *
+	 * @param inputStream
+	 *            瀵煎嚭鐨勬枃浠舵祦
+	 * @param fileName
+	 *            瀵煎嚭鐨勬枃浠跺悕绉�
+	 */
+	public static void DownFileTips(HttpServletRequest request, HttpServletResponse response, InputStream  inputStream , String fileName) {
+		final String userAgent = request.getHeader("USER-AGENT");
+		try {
+			if(StringUtils.isEmpty(fileName)){
+				throw new RuntimeException("璇疯緭鍏ユ枃浠跺悕绉�");
+			}
+			String filename =  "";
+			if(userAgent.equals("MSIE")){//IE娴忚鍣�
+				filename = URLEncoder.encode(fileName,"UTF8");
+			}else if(userAgent.equals("Mozilla")){//google,鐏嫄娴忚鍣�
+				filename = new String(fileName.getBytes(), "ISO8859-1");
+			}else{
+				filename = URLEncoder.encode(fileName,"UTF8");//鍏朵粬娴忚鍣�
+			}
+			InputStream fis = new BufferedInputStream(inputStream);
+			byte[] buffer = new byte[fis.available()];
+			fis.read(buffer);
+			fis.close();
+
+			response.addHeader("Content-Disposition", "attachment;filename="+ filename);
+			OutputStream toClient= new BufferedOutputStream(response.getOutputStream());
+			response.setContentType("application/vnd.ms-excel;charset=utf-8");
+
+			toClient.write(buffer);
+			toClient.flush();
+			toClient.close();
+		} catch (FileNotFoundException e) {
+			e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+
+	/**
+	 * 涓嬭浇鏂囦欢宸ュ叿锛堟彁绀洪�夋嫨璺緞锛�
+	 *
+	 * @param downfile
+	 *            瀵煎嚭鐨勬枃浠�
+	 * @param fileName
+	 *            瀵煎嚭鐨勬枃浠跺悕绉�
+	 */
+	public static void DownFileTips( HttpServletResponse response, File downfile, String fileName) {
+		try {
+			if(StringUtils.isEmpty(fileName)){
+				fileName = downfile.getName();
+			}
+			String filename =  "";
+			filename = URLEncoder.encode(fileName,"UTF8");//鍏朵粬娴忚鍣�
+			InputStream fis = new BufferedInputStream(new FileInputStream(
+					downfile));
+			byte[] buffer = new byte[fis.available()];
+			fis.read(buffer);
+			fis.close();
+
+			response.addHeader("Content-Disposition", "attachment;filename="+ filename);
+			OutputStream toClient= new BufferedOutputStream(response.getOutputStream());
+			response.setContentType("application/vnd.ms-excel;charset=utf-8");
+
+			toClient.write(buffer);
+			toClient.flush();
+			toClient.close();
+		} catch (FileNotFoundException e) {
+			e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+
 }

--
Gitblit v1.8.0