serviceImpl业务逻辑层:
package ssmy.service.impl;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;
import ssmy.dao.CreditDao;
import ssmy.dto.Credit;
import ssmy.service.CreditService;
import ssmy.vo.CreditVO;
@Service("creditService")
public class CreditServiceImpl implements CreditService {
@Resource
CreditDao creditDao;
public void setCreditDao(CreditDao creditDao) {
this.creditDao = creditDao;
}
/**
* 增加
*/
@Override
public boolean createUser(CreditVO creditVo) {
Credit credit=new Credit();
if(creditVo.getCreditId()!=null)
{
credit.setCreditId(creditVo.getCreditId());
}
String loginName=creditVo.getLoginName();
if(StringUtils.isNotBlank(loginName))
{
credit.setLoginName(loginName.trim());
}
String loginPwd=creditVo.getLoginPwd();
if(loginPwd!=null&& !"".trim().equals(loginPwd))
{
credit.setLoginPwd(loginPwd.trim());
}
String againPwd=creditVo.getAgainPwd();
if(againPwd!=null && !"".trim().equals(againPwd))
{
credit.setAgainPwd(againPwd.trim());
}
String IDCard=creditVo.getIDCard();
if(IDCard!=null && !"".trim().equals(IDCard))
{
credit.setIDCard(IDCard.trim());
}
String fixedTelephoneNumber=creditVo.getFixedTelephoneNumber();
if(fixedTelephoneNumber!=null && !"".trim().equals(fixedTelephoneNumber))
{
credit.setFixedTelephoneNumber(fixedTelephoneNumber.trim());
}
String telephoneNumber=creditVo.getTelephoneNumber();
if(telephoneNumber!=null && !"".trim().equals(telephoneNumber))
{
credit.setTelephoneNumber(telephoneNumber.trim());
}
String email=creditVo.getEmail();
if(email!=null && !"".trim().equals(email))
{
credit.setEmail(email.trim());
}
String address=creditVo.getAddress();
if(address!=null && !"".trim().equals(address)){
if(address.contains(",")){
String str=address.replace(",", "-");
credit.setAddress(str);
}
}else{
credit.setAddress(address);
}
credit.setStatus("0");
Date date=new Date();
SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH: mm");
credit.setCreatetime(time.format(date));
int num=creditDao.createUser(credit);
if(num>0){
return true;
}
return false;
}
/**
* 查询
*/
@Override
public List<Credit> queryUserInfo(CreditVO creditVO) {
Credit credit=new Credit();
List<Credit> creditList=null;
if(creditVO!=null){
String loginName=creditVO.getLoginName();
if(loginName!=null&&!"".equals(loginName.trim()))
{
credit.setLoginName(loginName.trim());
}
String IDCard=creditVO.getIDCard();
if(IDCard!=null && !"".trim().equals(IDCard))
{
credit.setIDCard(IDCard.trim());
}
String Status=creditVO.getStatus();
if(Status!=null&&!"".equals(Status.trim()))
{
credit.setStatus(Status.trim());
}
creditList=creditDao.queryUserInfo(creditVO);
}else{
creditList=creditDao.queryUserInfo(null);
}
return creditList;
}
/**
* 删除
*/
@Override
public void deleteUser(int id) {
int num=creditDao.deleteUser(id);
}
/**
* 导出excexl
*/
@Override
public void exprotExcel(CreditVO CreditVO,HttpServletResponse response) {
Workbook workBook = null;
ByteArrayOutputStream out = null;
ByteArrayInputStream input = null;
OutputStream os=null;
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSSS");
String time = dateFormat.format(new Date());
String fileName = "userFile" + time + ".xls";
workBook = new HSSFWorkbook();
Sheet sheet = workBook.createSheet("用户信息 ");
CellStyle style = workBook.createCellStyle();
Row rowFirst = sheet.createRow(0);
Cell headCell0 = rowFirst.createCell(0);
Cell headCell1 = rowFirst.createCell(1);
Cell headCell2 = rowFirst.createCell(2);
Cell headCell3 = rowFirst.createCell(3);
Cell headCell4 = rowFirst.createCell(4);
Cell headCell5 = rowFirst.createCell(5);
Cell headCell6 = rowFirst.createCell(6);
Cell headCell7 = rowFirst.createCell(7);
Cell headCell8 = rowFirst.createCell(8);
headCell0.setCellValue("登录名");;
headCell1.setCellValue("登录密码");
headCell2.setCellValue("确认密码");
headCell3.setCellValue("身份证号码");
headCell4.setCellValue("固定电话");
headCell5.setCellValue("手机号码");
headCell6.setCellValue("电子邮件");
headCell7.setCellValue("现居住地");
headCell8.setCellValue("创建时间");
int index = 1;
List<Credit> creditList=null;
if(CreditVO!=null){
creditList=creditDao.queryUserInfo(CreditVO);
}else{
creditDao.queryUserInfo(null);
}
for (Credit credit2 : creditList) {
Font font = workBook.createFont();
font.setColor(Font.COLOR_NORMAL);
style.setFont(font);
Row sheetRow = sheet.createRow(index);
Cell sheetCell0 = sheetRow.createCell(0);
Cell sheetCell1 = sheetRow.createCell(1);
Cell sheetCell2 = sheetRow.createCell(2);
Cell sheetCell3 = sheetRow.createCell(3);
Cell sheetCell4 = sheetRow.createCell(4);
Cell sheetCell5 = sheetRow.createCell(5);
Cell sheetCell6 = sheetRow.createCell(6);
Cell sheetCell7 = sheetRow.createCell(7);
Cell sheetCell8 = sheetRow.createCell(8);
String loginName=credit2.getLoginName();
String loginPwd=credit2.getLoginPwd();
String againPwd=credit2.getAgainPwd();
String IDCard=credit2.getIDCard();
String fixedTelephoneNumber=credit2.getFixedTelephoneNumber();
String telephoneNumber=credit2.getTelephoneNumber();
String email=credit2.getEmail();
String address=credit2.getAddress();
String createtime=credit2.getCreatetime();
// 用户名
if (StringUtils.isNotBlank(loginName)) {
sheetCell0.setCellValue(loginName);
sheetCell0.setCellType(Cell.CELL_TYPE_STRING);
}
// 密码
if (StringUtils.isNotBlank(loginPwd)) {
sheetCell1.setCellValue(loginPwd);
sheetCell1.setCellType(Cell.CELL_TYPE_STRING);
}
// 确认密码
if (StringUtils.isNotBlank(againPwd)) {
sheetCell2.setCellValue(againPwd);
sheetCell2.setCellType(Cell.CELL_TYPE_STRING);
}
//身份证号
if (StringUtils.isNotBlank(IDCard)) {
sheetCell3.setCellValue(IDCard);
sheetCell3.setCellType(Cell.CELL_TYPE_STRING);
}
// 手机号
if (StringUtils.isNotBlank(fixedTelephoneNumber)) {
sheetCell4.setCellValue(fixedTelephoneNumber);
sheetCell4.setCellType(Cell.CELL_TYPE_STRING);
}
// 固定电话
if (StringUtils.isNotBlank(telephoneNumber)) {
sheetCell5.setCellValue(telephoneNumber);
sheetCell5.setCellType(Cell.CELL_TYPE_STRING);
}
// email
if (StringUtils.isNotBlank(email)) {
sheetCell6.setCellValue(email);
sheetCell6.setCellType(Cell.CELL_TYPE_STRING);
}
// 地址
if (StringUtils.isNotBlank(address)) {
sheetCell7.setCellValue(address);
sheetCell7.setCellType(Cell.CELL_TYPE_STRING);
}
// 创建时间
if (StringUtils.isNotBlank(createtime)) {
sheetCell8.setCellValue(createtime);
sheetCell8.setCellType(Cell.CELL_TYPE_STRING);
}
++index;
}
fileName = new String(fileName.getBytes(),"UTF-8");
response
.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename="
+ fileName);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
os = response.getOutputStream();
workBook.write(os);
os.flush();
os.close();
/*out = new ByteArrayOutputStream();
workBook.write(out);
input = new ByteArrayInputStream(out.toByteArray());
out.close();*/
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (input != null) {
input.close();
}
if (os != null) {
os.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
控制层:
package ssmy.control;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import ssmy.dto.Credit;
import ssmy.service.CreditService;
import ssmy.vo.CreditVO;
@Controller
@RequestMapping("/CreditController")
public class CreditController {
/**
* 初始化页面
* @return
*/
@RequestMapping(value="/inti",method=RequestMethod.GET)
public String inti(){
return "registeruser";
}
/**
* 初始化页面
* @return
*/
@RequestMapping(value="/test",method=RequestMethod.GET)
public String test(){
return "test";
}
@RequestMapping(value="/fenye",method=RequestMethod.GET)
public String fenye(){
return "fenye";
}
/**
* 增加
* @param creditVO
* @param model
* @return
*/
@RequestMapping(value="/createUser",method=RequestMethod.POST)
public String createUser(CreditVO creditVO,Model model)
{
if (creditVO!=null) {
creditService.createUser(creditVO);
}
List<Credit> list=creditService.queryUserInfo(null);
model.addAttribute("creditVOList", list);
return "queryregister";
}
/**
* 查询
* @param creditVO
* @param model
* @return
*/
@RequestMapping(value="/intiqu",method=RequestMethod.POST)
//@ResponseBody
public String intiquery(CreditVO creditVO,Model model){
List<Credit> list=null;
if(creditVO!=null){
list=creditService.queryUserInfo(creditVO);
}else{
list=creditService.queryUserInfo(null);
}
model.addAttribute("creditVOList", list);
model.addAttribute("status", creditVO.getStatus());
return "queryregister";
}
@RequestMapping("/deleteuser")
public ModelAndView deleteUser(CreditVO creditVO,HttpServletRequest request) {
String[] creditId=request.getParameterValues("creditIdbox");
if(creditId!=null&&creditId.length>0){
for (int i = 0; i < creditId.length; i++)
{
creditService.deleteUser(Integer.parseInt(creditId[i]));
}
}
List<Credit> list=creditService.queryUserInfo(null);
ModelAndView mv = new ModelAndView();
Map<String, Object> model = new HashMap<String, Object>();
model.put("creditVOList", list);
mv.addAllObjects(model);
mv.setViewName("queryregister");
return mv;
}
@RequestMapping(value="/exprotExcel",method=RequestMethod.POST)
public void exprotExcel(CreditVO creditVO,HttpServletResponse response) {
creditService.exprotExcel(creditVO,response);
}
@RequestMapping(value="/uploadFile",method=RequestMethod.POST)
public ModelAndView uploadFile(MultipartHttpServletRequest request,HttpServletResponse response)
{
ModelAndView mode =new ModelAndView();
try {
Date date=new Date();
SimpleDateFormat sf=new SimpleDateFormat("yyyyMMddHHmm");
System.out.println("username"+request.getParameter("username"));
String username=request.getParameter("username");
MultipartFile file=request.getFile("uploadFile");
String uploadFileName=file.getOriginalFilename();
if(uploadFileName!=null&&uploadFileName.length()>0){
String [] str=uploadFileName.split("\\.");
//新的图片名称
String newFileName=str[0]+sf.format(date)+"."+str[1];
//String newFileName =uploadFileName.substring(uploadFileName.lastIndexOf("."))+sf.format(date);
if(checkFile(newFileName)){
InputStream isRef=file.getInputStream();
//获取服务器路径
String targetDir=request.getSession().getServletContext().getRealPath("/upload")+"\\";
File targerFile=new File(targetDir,newFileName);
FileOutputStream fos=new FileOutputStream(targerFile);
IOUtils.copy(isRef, fos);
mode.addObject("username",username);
mode.addObject("uploadFile",newFileName);
mode.setViewName("success_fileupload");
return mode;
}
else{
String message="你的文件格式错误!";
String uploadMessage="文件只支持持:xls,xlsx,jpg,gif,png,ico,bmp,jpeg格式";
mode.addObject("message",message);
mode.addObject("uploadMessage",uploadMessage);
mode.addObject("username",username);
mode.addObject("uploadFile",uploadFileName);
mode.setViewName("error_fileupload");
return mode;
}
}else{
String message="你没有上传文件!!";
mode.addObject("message",message);
mode.addObject("username",username);
mode.addObject("uploadFile",uploadFileName);
mode.setViewName("error_fileupload");
return mode;
}
} catch (IOException e) {
e.printStackTrace();
mode.setViewName("error_fileupload");
return mode;
}
}
/**
*
* @param model
* @return
*/
@RequestMapping(value="/downloadFile",method=RequestMethod.GET)
public void downloadFile(String fileName, HttpServletRequest request,HttpServletResponse response)
{
OutputStream os=null;
FileInputStream fileInputStreamRef=null;
try {
String fileNameEncode=new String(fileName.getBytes(),"UTF-8");
//response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition", "attachment;fileName="
+ fileName);
fileInputStreamRef=new FileInputStream
(new File(request.getSession().getServletContext().getRealPath("/downfile")+"\\"+fileName));
os=response.getOutputStream();
IOUtils.copy(fileInputStreamRef, os);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(os!=null){
os.close();
}
if(fileInputStreamRef!=null)
{
fileInputStreamRef.close();
}
} catch (IOException e)
{
e.printStackTrace();
}
}
}
/**
* 验证文件上传格式
* @param fileName
* @return
*/
private boolean checkFile(String fileName){
boolean flag=false;
String suffixList="xls,xlsx,jpg,gif,png,ico,bmp,jpeg";
//获取文件后缀
String suffix=fileName.substring(fileName.lastIndexOf(".")+1, fileName.length());
if(suffixList.contains(suffix.trim().toLowerCase())){
flag=true;
}
return flag;
}
@Autowired
private CreditService creditService;
public void setCreditService(CreditService creditService) {
this.creditService = creditService;
}
}
页面jsp:
<script type="text/javascript" src="../jquery/jquery-2.1.3.js"></script>
<script type="text/javascript" >
$(function(){
$("#button").click(function(){
var form=$("#registerform");
form.prop("action","http://localhost:8080/ssmy/CreditController/intiqu.do");
form.submit();
});
//对查询按钮定死状态
$("#status").val($("#statushidden").val());
});
function selectAll(){
if ($("#SelectAll").is(":checked")) {
$(":checkbox").prop("checked", true);//所有选择框都选中
} else {
$(":checkbox").prop("checked", false);
}
}
$(function(){
$("#deleteuser").click(function(){
//判断至少写了一项
var checkedNum = $("input[name='creditIdbox']:checked").length;
if(checkedNum==0){
alert("请至少选择一项!");
return false;
}
var form=$("#registerform");
form.prop("action","http://localhost:8080/ssmy/CreditController/deleteuser.do");
form.submit();
});
$("#exports").click(function(){
var form =$("#registerform");
form.prop("action","http://localhost:8080/ssmy/CreditController/exprotExcel.do");
form.submit();
});
$("#uploadFile").click(function(){
var form =$("#registerform");
form.prop("action","http://localhost:8080/ssmy/CreditController/uploadFile.do");
form.submit();
});
});
</script>
</head>
<body>
<div id="head">
<form id="registerform" action="" method="post" enctype="multipart/form-data">
<div class="search-box" style="width:100%;height:40px;">
<label> 登录名: </label>
<input type="text" name="loginName" id="loginName" />
<label style="margin-left:10px;">身份证:</label>
<input type="text" name="IDCard" id="IDCard" />
<label style="margin-left:10px;"> 提交状态:</label>
<select id="status" name="status" style="width:100px;height:20px;">
<option value="">全部</option>
<option value="0">已提交</option>
<option value="1">未提交</option>
</select>
<input type="button" id="button" value="查询" style="width:65px;height:22px;margin-left:20px;"/>
<input type="submit" id="exports" value="导出" style="width:65px;height:22px;margin-left:20px;"/>
</div>
<input type="hidden" name="statushidden" id="statushidden" value="${status }" />
<table border="0" cellpadding="0" cellspacing="0">
<tr style="width:100%; height: 50px;">
<td>
<input type="checkbox" id="SelectAll" name="SelectAll" οnclick="selectAll();" style="margin-right:5px;"/>全选</td>
<td>序列</td>
<td >登录名</td>
<td >登录密码</td>
<td >确认密码</td>
<td >身份证号</td>
<td >固定电话</td>
<td >手机号码</td>
<td >邮箱</td>
<td >居住地址</td>
<td id="address">提交状态</td>
<td >创建时间</td>
<td >操作</td>
</tr>
<c:forEach var="credit" items="${creditVOList}" varStatus="status">
<tr style="width: 300px;">
<td><input type="checkbox" name="creditIdbox" id="creditIdbox" value="${credit.creditId }"></td>
<td>${status.index}</td>
<td>${credit.loginName}</td>
<td>${credit.loginPwd}</td>
<td>${credit.againPwd}</td>
<td>${credit.IDCard}</td>
<td>${credit.fixedTelephoneNumber}</td>
<td>${credit.telephoneNumber}</td>
<td>${credit.email}</td>
<td id="address">${credit.address}</td>
<td>${credit.status ==0 ? '已提交':'未提交'}</td>
<td>${creditVO.createtime}</td>
<td>
<a href="http://localhost:8080/ssmy/CreditController/deleteuser.do?creditIdbox=${credit.creditId}">删除</a>
</td>
</tr>
</c:forEach>
</table>
<c:if test="${empty creditVOList }">
没有任何员工信息.
</c:if> <br/>
<input type="button" value="删除" id ="deleteuser" >
<input type="text" name="username"/>
<input type="file" name="uploadFile"/>
<input type="submit" id="uploadFile" name="开始上传文件" value="开始上传文件"/>
</form>
<a href="http://localhost:8080/ssmy/CreditController/downloadFile.do?fileName=inputik201612061447.JPG">下载文件 </a><br />
</div>
</body>
</html>