package com.vci.ubcs.starter.web.util;
|
|
import org.apache.commons.io.IOUtils;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springblade.core.tool.utils.StringUtil;
|
|
import javax.servlet.http.Cookie;
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.*;
|
import java.net.URLEncoder;
|
import java.util.Map;
|
import java.util.concurrent.ConcurrentHashMap;
|
|
public class ControllerUtil {
|
private static Logger logger = LoggerFactory.getLogger(ControllerUtil.class);
|
public static final Map<String, String> tempFileForDownloadMap = new ConcurrentHashMap();
|
|
public ControllerUtil() {
|
}
|
|
public static void writeFileToResponse(HttpServletResponse response, String fileName) throws FileNotFoundException, IOException {
|
writeFileToResponse(response, fileName, true);
|
}
|
|
public static void writeFileToResponse(HttpServletResponse response, String fileName, boolean deleteFile) throws FileNotFoundException, IOException {
|
writeFileToResponse(response, (String)fileName, (String)null, deleteFile);
|
}
|
|
public static void writeFileToResponse(HttpServletResponse response, String fileName, String showName, boolean deleteFile) throws FileNotFoundException, IOException {
|
File file = new File(fileName);
|
if (!file.exists()) {
|
throw new FileNotFoundException(fileName);
|
} else {
|
writeFileToResponse(response, file, showName, deleteFile);
|
}
|
}
|
|
public static void writeFileToResponse(HttpServletResponse response, File file) throws FileNotFoundException, IOException {
|
writeFileToResponse(response, file, true);
|
}
|
|
public static void writeFileToResponse(HttpServletResponse response, File file, boolean deleteFile) throws FileNotFoundException, IOException {
|
writeFileToResponse(response, (File)file, (String)null, deleteFile);
|
}
|
|
public static void writeFileToResponse(HttpServletResponse response, File file, String showName, boolean deleteFile) throws FileNotFoundException, IOException {
|
writeFileToResponse(response, file, showName, deleteFile, (String)null);
|
}
|
|
public static void writeFileToResponse(HttpServletResponse response, File file, String showName, boolean deleteFile, String contentType) throws FileNotFoundException, IOException {
|
VciBaseUtil.alertNotNull(new Object[]{file, "文件对象"});
|
if (!file.exists()) {
|
throw new FileNotFoundException(file.getName());
|
} else {
|
if (StringUtil.isBlank(showName)) {
|
showName = file.getName();
|
}
|
|
FileInputStream in = null;
|
boolean var11 = false;
|
|
File parentFile;
|
label96: {
|
try {
|
var11 = true;
|
in = new FileInputStream(file);
|
writeFileToResponse(response, in, showName, contentType);
|
var11 = false;
|
break label96;
|
} catch (IOException var12) {
|
if (logger.isErrorEnabled()) {
|
logger.error("写入文件到响应流出错", var12);
|
var11 = false;
|
} else {
|
var11 = false;
|
}
|
} finally {
|
if (var11) {
|
IOUtils.closeQuietly(in);
|
if (deleteFile) {
|
File parentFile1 = file.getParentFile();
|
file.delete();
|
parentFile1.delete();
|
}
|
|
}
|
}
|
|
IOUtils.closeQuietly(in);
|
if (deleteFile) {
|
parentFile = file.getParentFile();
|
file.delete();
|
parentFile.delete();
|
}
|
|
return;
|
}
|
|
IOUtils.closeQuietly(in);
|
if (deleteFile) {
|
parentFile = file.getParentFile();
|
file.delete();
|
parentFile.delete();
|
}
|
|
}
|
}
|
|
public static void writeFileToResponse(HttpServletResponse response, InputStream ins, String showName) throws IOException {
|
writeFileToResponse(response, ins, showName, (String)null);
|
}
|
|
public static void writeFileToResponse(HttpServletResponse response, InputStream ins, String showName, String contentType) throws IOException {
|
if (StringUtil.isBlank(contentType)) {
|
contentType = "application/force-download";
|
}
|
|
if (StringUtil.isBlank(showName)) {
|
showName = "下载文件";
|
}
|
|
response.setContentType(contentType);
|
|
try {
|
String fileName = URLEncoder.encode(showName, "UTF8");
|
response.addHeader("Content-Disposition", "attachment; filename=" + fileName + ";filename*=utf-8''" + fileName);
|
} catch (Exception var12) {
|
if (logger.isErrorEnabled()) {
|
logger.error("设置文件的名称到响应流的时候出错", var12);
|
}
|
}
|
|
response.setCharacterEncoding("UTF-8");
|
Cookie cookie = new Cookie("fileDownload", "true");
|
cookie.setPath("/");
|
response.addCookie(cookie);
|
|
try {
|
IOUtils.copy(ins, response.getOutputStream());
|
} catch (IOException var10) {
|
if (logger.isErrorEnabled()) {
|
logger.error("写入文件到响应流出错", var10);
|
}
|
|
throw var10;
|
} finally {
|
IOUtils.closeQuietly(ins);
|
}
|
|
}
|
|
public static void writeDataToResponse(HttpServletResponse response, byte[] data, String contentType) throws IOException {
|
if (StringUtil.isBlank(contentType)) {
|
contentType = "application/force-download";
|
}
|
|
response.setContentType(contentType);
|
response.setCharacterEncoding("UTF-8");
|
Cookie cookie = new Cookie("fileDownload", "true");
|
cookie.setPath("/");
|
response.addCookie(cookie);
|
|
try {
|
response.getOutputStream().write(data);
|
} catch (IOException var5) {
|
if (logger.isErrorEnabled()) {
|
logger.error("写入文件到响应流出错", var5);
|
}
|
|
throw var5;
|
}
|
}
|
|
public static void writeClasspathFileToResponse(HttpServletResponse response, String classPathFileName) throws FileNotFoundException, IOException {
|
writeClasspathFileToResponse(response, classPathFileName, (String)null);
|
}
|
|
public static void writeClasspathFileToResponse(HttpServletResponse response, String classPathFileName, String showName) throws FileNotFoundException, IOException {
|
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(classPathFileName);
|
if (StringUtil.isBlank(showName)) {
|
showName = classPathFileName.contains("/") ? classPathFileName.substring(classPathFileName.lastIndexOf("/") + 1) : classPathFileName;
|
}
|
|
try {
|
writeFileToResponse(response, in, showName);
|
} catch (IOException var8) {
|
throw var8;
|
} finally {
|
IOUtils.closeQuietly(in);
|
}
|
|
}
|
|
public static String getClientInfo(HttpServletRequest request) {
|
String ip = request.getHeader("X-Forwarded-For");
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("Proxy-Client-IP");
|
}
|
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("WL-Proxy-Client-IP");
|
}
|
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("HTTP_CLIENT_IP");
|
}
|
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
|
}
|
|
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
ip = request.getRemoteAddr();
|
}
|
|
if (ip == null || ip.length() == 0 || ip.indexOf("0:0:0:0:0:0:0:1") > -1) {
|
ip = "127.0.0.1";
|
}
|
|
return ip;
|
}
|
|
public static void setQueryTotal(HttpServletRequest request, boolean isQueryTotal) {
|
WebThreadLocalUtil.getNeedQueryTotalInThread().set(isQueryTotal ? "true" : "false");
|
}
|
|
public static String putErrorFile(String errorFile) {
|
String uuid = VciBaseUtil.getPk();
|
tempFileForDownloadMap.put(uuid, errorFile);
|
return uuid;
|
}
|
|
public static void downloadErrorFile(HttpServletResponse response, String uuid) throws IOException {
|
String errorFile = (String)tempFileForDownloadMap.getOrDefault(uuid, "");
|
|
try {
|
if (StringUtil.isNotBlank(errorFile)) {
|
writeFileToResponse(response, new File(errorFile), (String)null, true, "application/msexcel");
|
}
|
} finally {
|
tempFileForDownloadMap.remove(uuid);
|
}
|
|
}
|
}
|