//导出
public void excel(List<Long> ids, HttpServletResponse response) {
List<StockPageVo> excel = stockMapper.excel(ids);
try {
ExportParams params = new ExportParams();
params.setSheetName("历史入库导出");//设置sheet名
Workbook workbook = ExcelExportUtil.exportExcel(params, StockPageVo.class, excel);
//返回头设置下载,并设置文件名,返回
DownloadUtils.setExportExcelFormat(response, workbook, "历史入库");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void setExportExcelFormat(HttpServletResponse response, Workbook workbook, String fileName) throws Exception {
response.reset();
response.setContentType("application/vnd.ms-excel");//下载
fileName = fileName + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1) + ".xls");
try (ServletOutputStream outStream = response.getOutputStream()) {
workbook.write(outStream);
}
}
//图片上传
public static String upload(MultipartFile file) {
if (file == null) throw new BusinessException("图片为空");
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
String uuid = Utility.generateShortUuid() + "." + extension;
try {
//图片上传的路径
String path = IntegerFaceConfig.EXCEP_Path;
file.transferTo(new File(path + uuid));
return path + uuid;
} catch (IOException e) {
e.printStackTrace();
}
return throw new BusinessException("图片上传失败");
}
//图片的删除
File file123 = new File(ImgUser);//图片的路径
file123.delete();
//校验id和端口
public static boolean checkIp(String ipAddress) {
String ip = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
Pattern pattern = Pattern.compile(ip);
Matcher matcher = pattern.matcher(ipAddress);
return matcher.matches();
}
public static boolean checkPort(String port) {
//端口号验证 1 ~ 65535
String regex = "^([1-9]|[1-9]\\d{1,3}|[1-6][0-5][0-5][0-3][0-5])$";
return Pattern.matches(regex, port);
}
//校验电话号码
public static boolean isMobileNo(String mobiles) {
// ^ 匹配输入字符串开始的位置
// \d 匹配一个或多个数字,其中 \ 要转义,所以是 \\d
// $ 匹配输入字符串结尾的位置
String regExp = "^((13[0-9])|(14[5,7,9])|(15[0-3,5-9])|(166)|(17[3,5,6,7,8])" +
"|(18[0-9])|(19[8,9]))\\d{8}$";
Pattern p = Pattern.compile(regExp);
Matcher m = p.matcher(mobiles);
return m.matches();
}
//对参数格式的校验
public static Boolean isNull(String str) {
if (str==null) return false;
boolean blank = StringUtils.isBlank(str);
if (!blank) {
return !str.equals("''");
} else {
return false;
}
}
//获取当前时间
public static String getTimestamps() {
Date date = new Date();
DateTime dateTime = new DateTime();
String endOfDay = getEndOfDay(date);
/*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
String formats = sdf.format(endOfDay);*/
return endOfDay;
}
public static String getEndOfDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
/*calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);*/
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
}
//获取当前年月日
public static String getDate() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);
}
//获取本年
public static String getYear() {
Calendar c = Calendar.getInstance();
String getCurrentTime = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
return String.valueOf(c.get(Calendar.YEAR));
}
//计算两个数占得百分比
public static String getPercent(int x, int y) {
double d1 = x * 1.0;
double d2 = y * 1.0;
NumberFormat percentInstance = NumberFormat.getPercentInstance();
// 设置保留几位小数,这里设置的是保留两位小数
percentInstance.setMinimumFractionDigits(2);
return percentInstance.format(d1 / d2);
}
标签:return,String,Calendar,static,new,工具,public
From: https://www.cnblogs.com/srjx/p/16714560.html