packageshopping.controller;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.IOException;importjava.util.List;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.springframework.stereotype.Controller;importorg.springframework.util.ResourceUtils;importorg.springframework.web.bind.annotation.CrossOrigin;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.ResponseBody;importcom.fasterxml.jackson.databind.util.JSONPObject;importshopping.template.ResponseTemplate;importshopping.utils.ReadExcel;
@Controller
@CrossOrigin
@RequestMapping("/shopping")public classMyShopping {@RequestMapping("/get") //通过http响应解决调用public void get(HttpServletRequest req,HttpServletResponse res) throwsFileNotFoundException {
ReadExcel obj= newReadExcel();//此处为我创建Excel路径:E:/zhanhj/studysrc/jxl下//File file = new File(this.getClass().getResource("/")+"test.xlsx");
File file= ResourceUtils.getFile("classpath:test1.xls");
List excelList=obj.readExcel(file);
res.setContentType("text/plain");
String callbackFunName=req.getParameter("callback");//得到js函数名称
try{
res.getWriter().write(callbackFunName+ "([ { data:\"excelList\"}])"); //返回jsonp数据
} catch(IOException e) {
e.printStackTrace();
}
}
@RequestMapping("/getJsonp")
@ResponseBodypublic JSONPObject getJsonp(String callback) throwsFileNotFoundException{
ReadExcel obj= newReadExcel();//此处为我创建Excel路径:E:/zhanhj/studysrc/jxl下//File file = new File(this.getClass().getResource("/")+"test.xlsx");
File file= ResourceUtils.getFile("classpath:test1.xls");
List excelList=obj.readExcel(file);
ResponseTemplate response=new ResponseTemplate(0, "成功", excelList);return newJSONPObject(callback, response);
}
}