后台涉农结果导入excel模板

This commit is contained in:
majintao
2024-07-23 10:02:12 +08:00
parent a41d31feb7
commit 553b053d45
3 changed files with 134 additions and 48 deletions
@@ -11,11 +11,17 @@ import org.apache.commons.lang.StringUtils;
import org.sothis.core.util.Pager; import org.sothis.core.util.Pager;
import org.sothis.web.mvc.ActionContext; import org.sothis.web.mvc.ActionContext;
import org.sothis.web.mvc.interceptors.MultipartHttpServletRequest; import org.sothis.web.mvc.interceptors.MultipartHttpServletRequest;
import org.springframework.core.io.ClassPathResource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
import java.net.URLEncoder;
public class VillageIdentifyController { public class VillageIdentifyController {
private VillageIdentifyService villageIdentifyService; private VillageIdentifyService villageIdentifyService;
@@ -27,6 +33,47 @@ public class VillageIdentifyController {
return model; 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 { public Object uploadAction(HttpServletRequest request) throws IOException {
Object fileInputStream = ActionContext.getContext().getParameters().get("fileInputStream")[0]; Object fileInputStream = ActionContext.getContext().getParameters().get("fileInputStream")[0];
JsonModel ret = new JsonModel(); JsonModel ret = new JsonModel();
@@ -2,6 +2,7 @@ package com.fangjia.open.controllers;
import com.fangjia.core.util.HttpRequest; import com.fangjia.core.util.HttpRequest;
import com.fangjia.dal.common.DistrictKey; import com.fangjia.dal.common.DistrictKey;
import com.fangjia.dal.mongo.model.VillageIdentify;
import com.fangjia.dal.seaweed.model.District; import com.fangjia.dal.seaweed.model.District;
import com.fangjia.open.dto.addressGovernance.*; import com.fangjia.open.dto.addressGovernance.*;
import com.fangjia.open.interceptors.Authority; import com.fangjia.open.interceptors.Authority;
@@ -14,6 +15,7 @@ import com.fangjia.open.util.CommonDtoUtils;
import com.fangjia.service.bo.DistrictStatBo; import com.fangjia.service.bo.DistrictStatBo;
import com.fangjia.service.datastore.DistrictService; import com.fangjia.service.datastore.DistrictService;
import com.fangjia.service.district.DistrictStatService; import com.fangjia.service.district.DistrictStatService;
import com.fangjia.service.mongo.fangjia.VillageIdentifyService;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
@@ -28,6 +30,7 @@ import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.sothis.core.util.Pager;
import sun.misc.BASE64Encoder; import sun.misc.BASE64Encoder;
import java.io.IOException; import java.io.IOException;
@@ -38,7 +41,6 @@ import java.net.Proxy;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Stack;
/** /**
* 地址治理 * 地址治理
@@ -51,6 +53,7 @@ public class AddrSvcController {
private DistrictStatService districtStatService; private DistrictStatService districtStatService;
private DistrictService districtService; private DistrictService districtService;
private VillageIdentifyService villageIdentifyService;
/** /**
* 地址结构化 * 地址结构化
@@ -506,8 +509,38 @@ public class AddrSvcController {
String address = model.getAddress(); String address = model.getAddress();
params.put("address", address); params.put("address", address);
String type = StringUtils.isNotBlank(model.getType()) ? model.getType() : null; String type = StringUtils.isNotBlank(model.getType()) ? model.getType() : null;
String villageType = "2".equals(model.getType()) ? "对公" : "对私";
params.put("type", type); params.put("type", type);
params.put("showFullValue", "2"); params.put("showFullValue", "2");
Pager pager = new Pager(1, 1);
VillageIdentify villageResult = villageIdentifyService.findAll(villageType, model.getCity(), model.getAddress(), pager).get(0);
if (villageResult != null && StringUtils.isNotBlank(villageResult.getVerifyFlag())) {
Map<String, String> resultMap = new HashMap<String, String>();
resultMap.put("province",villageResult.getProvince());
resultMap.put("city",villageResult.getCity1());
resultMap.put("district",villageResult.getDistrict());
resultMap.put("town",villageResult.getTown());
resultMap.put("committee",villageResult.getCommittee());
resultMap.put("provinceCode",villageResult.getCityCode());
resultMap.put("cityCode",villageResult.getCityCode());
resultMap.put("districtCode",villageResult.getDistrictCode());
resultMap.put("townCode",villageResult.getTownCode());
resultMap.put("committeeCode",villageResult.getCommitteeCode());
resultMap.put("typeCode",villageResult.getTypeCode());
resultMap.put("typeName",villageResult.getTypeName());
resultMap.put("verifyFlag",villageResult.getVerifyFlag());
resultMap.put("addrVerify", villageResult.getAddrVerify());
resultMap.put("countyLevelCity",villageResult.getCountyLevelCity());
resultMap.put("administrativeTowns",villageResult.getAdministrativeTowns());
resultMap.put("countyGov",villageResult.getCountyGov());
resultMap.put("devDistrict",villageResult.getDevDistrict());
resultMap.put("devDistrictName",villageResult.getDevDistrictName());
resultMap.put("devLevel",villageResult.getDevLevel());
resultMap.put("addrArea",villageResult.getAddrArea());
BasicModel basicModel = new BasicModel(200, "OK");
basicModel.setResult(resultMap);
mav.setModel(basicModel);
} else {
// params.put("showTestValue", "2"); // params.put("showTestValue", "2");
Integer radius = model.getRadius(); Integer radius = model.getRadius();
params.put("radius", (radius != null && radius != 0) ? radius.toString() : null); params.put("radius", (radius != null && radius != 0) ? radius.toString() : null);
@@ -521,6 +554,7 @@ public class AddrSvcController {
} }
} }
if (json != null) { if (json != null) {
int code = json.getInt("msgCode"); int code = json.getInt("msgCode");
String msg = json.getString("msg"); String msg = json.getString("msg");
@@ -580,6 +614,7 @@ public class AddrSvcController {
} else { } else {
mav.setModel(BasicModel.buildServerErrorModel().setMsg("服务升级中...")); mav.setModel(BasicModel.buildServerErrorModel().setMsg("服务升级中..."));
} }
}
return mav; return mav;
} }
@@ -793,4 +828,8 @@ public class AddrSvcController {
public void setDistrictService(DistrictService districtService) { public void setDistrictService(DistrictService districtService) {
this.districtService = districtService; this.districtService = districtService;
} }
public void setVillageIdentifyService(VillageIdentifyService villageIdentifyService) {
this.villageIdentifyService = villageIdentifyService;
}
} }