|
@@ -8,12 +8,17 @@ import com.mrxu.framework.boot.bean.ResponseObj;
|
|
|
import com.mrxu.framework.common.util.BaseCode;
|
|
import com.mrxu.framework.common.util.BaseCode;
|
|
|
import com.mrxu.framework.common.util.BusinessException;
|
|
import com.mrxu.framework.common.util.BusinessException;
|
|
|
import com.mrxu.framework.common.util.StrFunc;
|
|
import com.mrxu.framework.common.util.StrFunc;
|
|
|
|
|
+import lombok.SneakyThrows;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.util.ResourceUtils;
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import javax.servlet.http.HttpSession;
|
|
import javax.servlet.http.HttpSession;
|
|
|
|
|
+import java.io.File;
|
|
|
|
|
+import java.io.OutputStream;
|
|
|
import java.net.URLEncoder;
|
|
import java.net.URLEncoder;
|
|
|
|
|
+import java.nio.file.Files;
|
|
|
import java.util.Collection;
|
|
import java.util.Collection;
|
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
@@ -244,7 +249,7 @@ public class BaseController {
|
|
|
* @param clazz
|
|
* @param clazz
|
|
|
* @param data
|
|
* @param data
|
|
|
*/
|
|
*/
|
|
|
- public void export(HttpServletResponse response,String fileName,Class<?> clazz,Collection<?> data) {
|
|
|
|
|
|
|
+ protected void export(HttpServletResponse response,String fileName,Class<?> clazz,Collection<?> data) {
|
|
|
try {
|
|
try {
|
|
|
response.setContentType("application/vnd.ms-excel");
|
|
response.setContentType("application/vnd.ms-excel");
|
|
|
response.setCharacterEncoding("utf-8");
|
|
response.setCharacterEncoding("utf-8");
|
|
@@ -257,4 +262,27 @@ public class BaseController {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 下载resource下的文件
|
|
|
|
|
+ * @param response
|
|
|
|
|
+ * @param fileName
|
|
|
|
|
+ */
|
|
|
|
|
+ @SneakyThrows
|
|
|
|
|
+ protected void downloadFile(HttpServletResponse response,String fileName) {
|
|
|
|
|
+ File file = ResourceUtils.getFile("classpath:file/"+fileName);
|
|
|
|
|
+ // 文件转成字节数组
|
|
|
|
|
+ byte[] fileByte = Files.readAllBytes(file.toPath());
|
|
|
|
|
+ // 设置response的Header
|
|
|
|
|
+ response.setCharacterEncoding("UTF-8");
|
|
|
|
|
+ // 指定下载文件名(attachment-以下载方式保存到本地,inline-在线预览)
|
|
|
|
|
+ response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(fileName, "UTF-8") + "\"");
|
|
|
|
|
+ // 告知浏览器文件的大小
|
|
|
|
|
+ response.addHeader("Content-Length", "" + file.length());
|
|
|
|
|
+ // 内容类型为通用类型,表示二进制数据流
|
|
|
|
|
+ response.setContentType("application/octet-stream");
|
|
|
|
|
+ OutputStream os = response.getOutputStream();
|
|
|
|
|
+ os.write(fileByte);
|
|
|
|
|
+ os.flush();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|