首页>代码>ssm(spring mvc+spring+mybatis)数据增删改查简单实例,支持附件文件上传下载>/web项目/Stan/src/controller/Coreollers.java
package controller;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import dao.PagerMySQL;
import entity.Stud;
import biz.Userdaobiz;
@Controller
public class Coreollers {
private Userdaobiz biz;
@Autowired
public void setBiz(Userdaobiz biz) {
this.biz = biz;
}
@RequestMapping("/Select")
public String select(
String zhname,
Model mode,
@RequestParam(value = "pageNo", defaultValue = "1") Integer curPage,
@RequestParam(value = "pageSize", defaultValue = "4") Integer pageSize) {
PageHelper.startPage(curPage, pageSize);
List<Stud> list = biz.getmm(zhname);
PageInfo<Stud> pageInfo = new PageInfo<Stud>(list);
PagerMySQL<Stud> pager = new PagerMySQL<Stud>(
curPage, pageSize, new Long(pageInfo.getTotal()).intValue());
pager.setEntitiesList(list);
if (zhname!= null) {
mode.addAttribute("huixianName", zhname);
}
mode.addAttribute("pagerBooks", pager);
return "/index.jsp";
}
//添加
@RequestMapping("/addBook")
public String Addsert(HttpServletRequest request,Model model,String stdnum,String zhname,String version,String keyss,String releasedate,String impldate,@RequestParam(value="packagepath",required=false) MultipartFile attach)
{
String packagepath=null;
if (!attach.isEmpty()) {
String path=request.getSession().getServletContext().getRealPath("statics"+File.separator+"uploadfile");
String oldfileName=attach.getOriginalFilename();
String prefix=FilenameUtils.getExtension(oldfileName);
int filesize=500000;
if (attach.getSize()>filesize) {
request.setAttribute("uploadfileError", "上传大小不得超过500k");
}else if (prefix.equalsIgnoreCase("jpg")||
prefix.equalsIgnoreCase("jpeg")||
prefix.equalsIgnoreCase("png")||
prefix.equalsIgnoreCase("pneg")
) {
String filename=System.currentTimeMillis()+RandomUtils.nextInt(100000)+"_personal.jpg";
File tarFile=new File(path,filename);
if (!tarFile.exists()) {
tarFile.mkdirs();
}
try {
attach.transferTo(tarFile);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
request.setAttribute("uploadfileError", "上传失败");
}
packagepath=path+filename;
//+File.separator
}else {
request.setAttribute("uploadfileError", "上传格式不正确");
}
}
Stud stand=new Stud();
stand.setImpldate(impldate);
stand.setKeyss(keyss);
stand.setPackagepath(packagepath);
stand.setReleasedate(releasedate);
stand.setStdnum(stdnum);
stand.setVersion(version);
stand.setZhname(zhname);
int num=biz.addstaud(stand);
//int num=biz.getAdd(name,gender,age,email,classid,telephone);
if (num==1) {
return "Select";
}
return "Select";
}
//修改
@RequestMapping("/updatestuds")
public String getemployeesupdate(Integer id,String stdnum,String zhname,String version,String keyss,String releasedate,String impldate,String packagepath){
Stud bizz=new Stud();
bizz.setId(id);
bizz.setStdnum(stdnum);
bizz.setZhname(zhname);
bizz.setVersion(version);
bizz.setKeyss(keyss);
bizz.setReleasedate(releasedate);
bizz.setImpldate(impldate);
bizz.setPackagepath(packagepath);
int ret=biz.updatestan(bizz);
if(ret==1){
return "/Select";
}
return "/Select";
}
//修改传值
@RequestMapping("/UserUpdate")
public ModelAndView getupdates(Integer id) {
ModelAndView mav = new ModelAndView();
Stud bizz =biz.getupdate(id);
if (bizz != null) {
mav.addObject("biz", bizz);
mav.setViewName("/update.jsp");
}
return mav;
}
//删除
@RequestMapping("/delet")
public String getdelete(Model model,Integer[] id)
{
int num=biz.getdelete(id);
if(num==1){
return "/Select";
}else{
return "/Select";
}
}
@RequestMapping("download")
public ResponseEntity<byte[]> download() throws IOException {
String path="D:\\Printer\\qweer.jpg";
File file=new File(path);
HttpHeaders headers = new HttpHeaders();
String fileName=new String("你好.xlsx".getBytes("UTF-8"),"iso-8859-1");//为了解决中文名称乱码问题
headers.setContentDispositionFormData("attachment", fileName);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
headers, HttpStatus.CREATED);
}
}
最近下载更多
vincemokea LV9
5月1日
zxcvbnm1027 LV1
2024年6月21日
wanglinddad LV55
2024年6月21日
2398164158 LV3
2023年9月24日
zhumeng168 LV5
2023年7月8日
xiang123xiang LV1
2023年6月18日
神经蛙-
2023年5月17日
暂无贡献等级
2633528624 LV1
2022年12月11日
刘鹏yyds LV10
2022年12月9日
zxc131313 LV12
2022年11月28日
最近浏览更多
奋斗的小蚂蚁 LV17
11月17日
vincemokea LV9
5月1日
16693655118
4月15日
暂无贡献等级
2194192290li LV1
2024年12月2日
lun123456
2024年6月22日
暂无贡献等级
zxcvbnm1027 LV1
2024年6月21日
微信网友_6925684083642368
2024年6月21日
暂无贡献等级
wanglinddad LV55
2024年6月21日
2636804923 LV6
2024年6月17日
xin xie LV1
2024年6月13日

