package cn.mydoll.standard.controller;
import cn.mydoll.standard.constant.Const;
import cn.mydoll.standard.pojo.Standard;
import cn.mydoll.standard.service.StandardService;
import cn.mydoll.standard.utility.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
@RestController
public class StandardController {
@Autowired
private StandardService standardService;
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
simpleDateFormat.setLenient(false);
binder.registerCustomEditor(Date.class,new CustomDateEditor(simpleDateFormat,true));
}
@RequestMapping(value = "/list")
public PageBean<Standard> list(String queryCondition,int pageIndex){
return standardService.getStandardList(queryCondition,pageIndex);
}
@RequestMapping(value = "/validate")
public boolean validate(String stdNum){
return standardService.validateStandard(stdNum);
}
@RequestMapping(value = "/save")
public boolean save(Standard standard,@RequestParam MultipartFile pic){
try{
boolean result=false;
if(!pic.isEmpty()){
String fileName=pic.getOriginalFilename();
String fileExName=fileName.substring(fileName.lastIndexOf("."),fileName.length());
fileName=System.currentTimeMillis()+fileExName;
File destFile=new File(Const.UPLOAD_PATH+fileName);
pic.transferTo(destFile);
standard.setPackagePath(fileName);
result=standardService.saveStandard(standard);
}
return result;
}catch (Exception e){
e.printStackTrace();
return false;
}
}
@RequestMapping(value = "/get")
public Standard get(int id){
return standardService.getStandardById(id);
}
@RequestMapping(value = "update")
public boolean update(Standard standard,@RequestParam MultipartFile pic){
try{
boolean result=false;
if(!pic.isEmpty()){
String fileName=pic.getOriginalFilename();
String fileExName=fileName.substring(fileName.lastIndexOf("."),fileName.length());
fileName=System.currentTimeMillis()+fileExName;
File destFile=new File(Const.UPLOAD_PATH+fileName);
//删除原文件
deleteFile(standard.getPackagePath());
pic.transferTo(destFile);
standard.setPackagePath(fileName);
}
result=standardService.updateStandard(standard);
return result;
}catch (Exception e){
e.printStackTrace();
return false;
}
}
private void deleteFile(String fileName){
File oldFile=new File(Const.UPLOAD_PATH+fileName);
if(oldFile.exists()){
oldFile.delete();
}
}
@RequestMapping(value = "/delete")
public boolean delete(String id){
try{
boolean result=false;
String[] ids=id.split(",");
int count=0;
for(int i=0;i<ids.length;i++){
Standard standard=standardService.getStandardById(Long.parseLong(ids[i]));
deleteFile(standard.getPackagePath());
if(standardService.deleteStandard(Long.parseLong(ids[i]))){
count+=1;
}
}
if(count==ids.length){
result=true;
}
return result;
}catch (Exception e){
e.printStackTrace();
return false;
}
}
@RequestMapping(value = "/download/{fileName}")
public void download(@PathVariable String fileName, HttpServletResponse response){
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
byte[] buffer = new byte[1024];
BufferedInputStream bis=null;
OutputStream os=null;
try{
os=response.getOutputStream();
bis=new BufferedInputStream(new FileInputStream(new File(Const.UPLOAD_PATH+fileName)));
int i=bis.read(buffer);
while(i!=-1){
os.write(buffer,0,buffer.length);
os.flush();
i=bis.read(buffer);
}
}catch(Exception e){
e.printStackTrace();
}finally {
try{
if(null!=bis){
bis.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
使用SpringBoot+MyBatis+MySQL,利用HTML和jQuery技术,实现单表增删改查、条件查询和分页、文件上传...
共39个文件
java:10个
class:10个
js:6个
需积分: 42 47 下载量 86 浏览量
2018-07-09
15:07:15
上传
评论 2
收藏 218KB ZIP 举报
温馨提示
完成使用SpringBoot+Mybatis+MySQL,实现单表数据的增删改查、条件查询和分页、文件上传和下载功能。页面使用原生HTML,利用jQuery解析SpringBoot返回的json字符串。
资源推荐
资源详情
资源评论
收起资源包目录
standard.zip (39个子文件)
standard
pom.xml 3KB
target
test-classes
cn
mydoll
standard
StandardApplicationTests.class 638B
generated-test-sources
test-annotations
classes
application.properties 693B
cn
mydoll
standard
constant
Const.class 556B
pojo
Standard.class 4KB
mapper
SqlBuilder.class 1KB
StandardMapper.class 2KB
StandardApplication.class 719B
utility
PageBean.class 3KB
controller
StandardController.class 6KB
service
StandardService.class 579B
impl
StandardServiceImpl.class 3KB
static
images
load.gif 4KB
add.html 6KB
js
jquery-1.12.4.js 287KB
ajaxfileupload.js 8KB
common.js 691B
list.html 6KB
update.html 6KB
generated-sources
annotations
src
test
java
cn
mydoll
standard
StandardApplicationTests.java 346B
main
resources
application.properties 610B
templates
static
images
load.gif 4KB
add.html 6KB
js
jquery-1.12.4.js 287KB
ajaxfileupload.js 8KB
common.js 691B
list.html 6KB
update.html 6KB
java
cn
mydoll
standard
constant
Const.java 171B
pojo
Standard.java 341B
mapper
StandardMapper.java 1KB
SqlBuilder.java 612B
StandardApplication.java 373B
utility
PageBean.java 2KB
controller
StandardController.java 5KB
service
StandardService.java 488B
impl
StandardServiceImpl.java 2KB
standard.iml 9KB
.gitignore 268B
共 39 条
- 1
资源评论
MelodyChiu
- 粉丝: 3
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功