网站跨域是指请求的目标网站与当前所在的发请求的网站,不在一个域名映射下面,这样的请求就会被浏览器拒绝,认为是跨域调用
解决办法
1 jsonp进行异步跨域请求
2 被调方设置请求头允许跨域
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
3 httpclient进行内部转发
4 通过nginx api网站进行反向代理转发
5 通过spring-cloude zuul网关转发
var province=$("#province").val();
if(province!=0){
$("select[name='province']").val(province);
$("select[name='province']").trigger("change");
var city=$("#city").val();
$("select[name='city']").val(city);
}
//导出数据
$("#export").bind("click",function(){
var param=$("#searchForm").serialize();
window.location.href="${ctx}/dealer/enroll/export.do?
dealerId=${dealerId}&"+param;
});
后台返回jsp页面,js异步加载回来一个页面 往指定区域丢就可以了使用html标签做全局替换
$.ajax({
type: "GET",
url: tml,
data:{
param:param,
pageNo:pageNo,
pageSize:pageSize
} ,
dataType: 'html',
success: function(res){
$("#wrapper").html(res);
},
errot:function(error){
poptys("网络异常,请稍后再试!");
}
});
(1)集成java包
org.apache.poi
poi
${poi.version}
org.apache.poi
poi-ooxml
${poi.version}
org.apache.poi
poi-ooxml-schemas
${poi.version}
(2)编写工具类
public class ExcelUtils {
/**
* 导出Excel
* @param sheetName sheet名称
* @param title 标题
* @param values 内容
* @param wb HSSFWorkbook对象
* @return
*/
public static SXSSFWorkbook getXSSFWorkbook(int total,String []title,String [][]values, XSSFWorkbook wbs){
// 第一步,创建一个HSSFWorkbook,对应一个Excel文件
if(wbs == null){
wbs = new XSSFWorkbook();
}
SXSSFWorkbook wb= new SXSSFWorkbook(wbs, 100);
double rel=Double.valueOf(total)/1000000;
int step=(int) Math.ceil(rel);
for(int st=0;st
(3) 实现导出控制
@RequestMapping(value="/export.do")
public String export(HttpServletRequest request, HttpServletResponse response) throws Exception {
Env env=EnvUtils.getEnv();
Map queryparam = new HashMap();
paramQuery(env, queryparam);
long dealerId = env.paramLong("dealerId");
int total = dealerEnrollService.findTotalByDearId(dealerId, queryparam);
List dealerEnrolls = dealerEnrollService.findByDearId(0,0,dealerId,queryparam);
SXSSFWorkbook wb=null;
OutputStream os=null;
long start = System.currentTimeMillis();
try {
String[] title = {"序号","投放序号","姓名","手机","城市","车系","报名时间","效果"};
String fileName = "线索信息表"+System.currentTimeMillis()+".xlsx";
String[][] content = new String[total][title.length];
for (int i = 0; i < total; i++) {
content[i][0] = dealerEnrolls.get(i).getId()+"";
content[i][1] = dealerEnrolls.get(i).getLuanchId()+"";
content[i][2] = dealerEnrolls.get(i).getName();
content[i][3] = dealerEnrolls.get(i).getPhone();
content[i][4] = dealerEnrolls.get(i).getCity();
content[i][5] = dealerEnrolls.get(i).getSerialName();
content[i][6] = T.format(dealerEnrolls.get(i).getCreateat(), "yyyy-MM-dd");
content[i][7] = dealerEnrolls.get(i).getExportDesc();
}
wb = ExcelUtils.getXSSFWorkbook(total,title, content, null);
response.setContentType("application/octet-stream;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename="+java.net.URLEncoder.encode(fileName, "UTF-8"));
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
os= response.getOutputStream();
wb.write(os);
long end = System.currentTimeMillis();
logger.info("export data expend time:="+(end-start));
} catch (Exception ex) {
throw new RuntimeException(ex.getMessage());
}finally{
try {
os.flush();
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e.getMessage());
}
}
return "dealer/admin/enroll/list";
}
加上这一句
response.setHeader("Content-Disposition", "attachment;filename="+java.net.URLEncoder.encode(fileName, "UTF-8"));
这个问题是我在跟其他团队进行接口联调过程中碰到的,因为不知道到底是发送方问题导致,还是接收方问题导致的乱码
问题,以至于互相甩锅了很久。最后自己想到了一个办法,检查对方传过来的编码,然后自己再对应的转化一下
/**
* @Title: getCode
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param str
* @return
* @author xiaozhengwen
* JSONObject 返回类型
* @throws
*/
private String getCode(String str) {
try {
String encode = "GB2312";
if (str.equals(new String(str.getBytes(encode), encode))) { //判断是不是GB2312
String s = encode;
return s; //是的话,返回“GB2312“,以下代码同理
}
encode = "ISO-8859-1";
if (str.equals(new String(str.getBytes(encode), encode))) { //判断是不是ISO-8859-1
String s1 = encode;
return s1;
}
encode = "UTF-8";
if (str.equals(new String(str.getBytes(encode), encode))) { //判断是不是UTF-8
String s2 = encode;
return s2;
}
encode = "GBK";
if (str.equals(new String(str.getBytes(encode), encode))) { //判断是不是GBK
String s3 = encode;
return s3;
}
encode= "GB18030";
if (str.equals(new String(str.getBytes(encode), encode))) { //判断是不是GBK
String s4 = encode;
return s4;
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
@RequestMapping(value="/asyncContractInto.do")
@ResponseBody
public JSONObject asyncContractInto(HttpServletRequest request,HttpServletResponse response,@RequestParam String cdata) {
String realIpAdrress = IpUtils.getRealIpAdrress(request);
try {
request.setCharacterEncoding("utf-8");
String str=cdata;
String code = getCode(str);
logger.info("原始合同数据编码格式:"+code);
logger.info("remote ip "+realIpAdrress);
//if(IP.indexOf(realIpAdrress)==-1) return new ResultJsonObj().unOk("", "IP校验不过").translateToJSON();
if(T.isBlank(cdata)) return new ResultJsonObj().unOk("", "请求参数为空").translateToJSON();
String strx=new String(str.getBytes("ISO-8859-1"),"UTF-8");
logger.info("cdata u8 decode:= "+strx);
String decode = URLDecoder.decode(strx);
logger.info("cdata := "+decode);
dealerContractService.asynContract(decode);
return new ResultJsonObj().ok("更新成功", "ok").translateToJSON();
}catch (DateDuplictException e){
return new ResultJsonObj().code(e.getCode(),e.getMsg()).translateToJSON();
}catch (AsynDealerException e){
return new ResultJsonObj().code(e.getCode(),e.getMsg()).translateToJSON();
}catch (RefundException e){
return new ResultJsonObj().code(e.getCode(),e.getMsg()).translateToJSON();
}catch (NoneDealerException e) {
return new ResultJsonObj().code(e.getCode(),e.getMsg()).translateToJSON();
}catch(AsynAuthException e){
logger.info("car server exception :="+e.getMsg());
return new ResultJsonObj().code(e.getCode(),e.getMsg()).translateToJSON();
}catch(AddBrandNumException e) {
return new ResultJsonObj().code(e.getCode(),e.getMsg()).translateToJSON();
}catch(Exception e) {
e.printStackTrace();
return new ResultJsonObj().error("", "服务器异常!"+e.getMessage()).translateToJSON();
}
}