后台涉农结果导入excel模板
This commit is contained in:
+47
@@ -11,11 +11,17 @@ import org.apache.commons.lang.StringUtils;
|
||||
import org.sothis.core.util.Pager;
|
||||
import org.sothis.web.mvc.ActionContext;
|
||||
import org.sothis.web.mvc.interceptors.MultipartHttpServletRequest;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import java.net.URLEncoder;
|
||||
|
||||
public class VillageIdentifyController {
|
||||
private VillageIdentifyService villageIdentifyService;
|
||||
@@ -27,6 +33,47 @@ public class VillageIdentifyController {
|
||||
return model;
|
||||
}
|
||||
|
||||
public void expectAction(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||||
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
||||
|
||||
// 编码文件名,避免乱码
|
||||
String fileName = "涉农结果导入后台模板.xlsx";
|
||||
String encodedFileName = encodeFileName(fileName, request);
|
||||
|
||||
// 设置 Content-Disposition 响应头
|
||||
response.setHeader("Content-Disposition", "attachment; filename=\"" + encodedFileName + "\"");
|
||||
|
||||
// 使用 ClassPathResource 来获取资源文件
|
||||
ClassPathResource resource = new ClassPathResource(fileName);
|
||||
InputStream in = resource.getInputStream();
|
||||
OutputStream out = response.getOutputStream();
|
||||
|
||||
byte[] buffer = new byte[1024];
|
||||
int bytesRead;
|
||||
|
||||
while ((bytesRead = in.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, bytesRead);
|
||||
}
|
||||
|
||||
out.flush();
|
||||
|
||||
}
|
||||
|
||||
private String encodeFileName(String fileName, HttpServletRequest request) throws IOException {
|
||||
String userAgent = request.getHeader("User-Agent");
|
||||
String encodedFileName;
|
||||
|
||||
if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
|
||||
// 针对IE浏览器
|
||||
encodedFileName = URLEncoder.encode(fileName, "UTF-8").replace("+", "%20");
|
||||
} else {
|
||||
// 针对其他浏览器
|
||||
encodedFileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
|
||||
}
|
||||
|
||||
return encodedFileName;
|
||||
}
|
||||
|
||||
public Object uploadAction(HttpServletRequest request) throws IOException {
|
||||
Object fileInputStream = ActionContext.getContext().getParameters().get("fileInputStream")[0];
|
||||
JsonModel ret = new JsonModel();
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user