Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1、return null处理console报错(虽不影响功能,但是老报错,流关闭后不能再返回东西) #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions src/main/java/com/github/andyczy/java/excel/ExcelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
Expand Down Expand Up @@ -136,7 +133,7 @@ public Boolean exportForExcelsOptimize() {
e.printStackTrace();
}
log.info("=== === === :Excel tool class export run time:" + (System.currentTimeMillis() - startTime) + " ms!");
return true;
return null;
}


Expand All @@ -159,7 +156,7 @@ public Boolean exportForExcelsNoStyle() {
e.printStackTrace();
}
log.info("=== === === :Excel tool class export run time:" + (System.currentTimeMillis() - startTime) + " ms!");
return true;
return null;
}


Expand Down Expand Up @@ -220,7 +217,7 @@ public static Boolean exportForExcel(HttpServletResponse response, List<List<Str
e.printStackTrace();
}
log.info("=== === === :Excel tool class export run time:" + (System.currentTimeMillis() - startTime) + " ms!");
return true;
return null;
}


Expand All @@ -240,7 +237,11 @@ private static void setIo(SXSSFWorkbook sxssfWorkbook, OutputStream outputStream
response.setHeader("Charset", "UTF-8");
response.setHeader("Content-Type", "application/force-download");
response.setHeader("Content-Type", "application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(fileName == null ? sheetName[0] : fileName, "utf8") + ".xlsx");
// response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(fileName == null ? sheetName[0] : fileName, "utf8") + ".xlsx");
String headerValue = "attachment;";
headerValue += " filename=\"" + encodeURIComponent(fileName) +"\";";
headerValue += " filename*=utf-8''" + encodeURIComponent(fileName == null ? sheetName[0] : fileName)+ ".xlsx";
response.setHeader("Content-Disposition", headerValue);
response.flushBuffer();
outputStream = response.getOutputStream();
}
Expand All @@ -250,6 +251,23 @@ private static void setIo(SXSSFWorkbook sxssfWorkbook, OutputStream outputStream
}
}

/**
* <pre>
* 符合 RFC 3986 标准的“百分号URL编码”
* 在这个方法里,空格会被编码成%20,而不是+
* 和浏览器的encodeURIComponent行为一致
* </pre>
* @param value
* @return
*/
public static String encodeURIComponent(String value) {
try {
return URLEncoder.encode(value, "UTF-8").replaceAll("\\+", "%20");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}



Expand Down