package com.xxx.project.controller;
import com.xxx.project.common.MsgException;
import com.xxx.project.entity.Counselor;
import com.xxx.project.entity.PlanFile;
import com.xxx.project.entity.Student;
import com.xxx.project.repository.PlanFileRepository;
import com.xxx.project.service.CounselorService;
import com.xxx.project.service.StudentService;
import javassist.bytecode.ByteArray;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;
import java.util.stream.Collectors;
@RestController
//@CrossOrigin(allowCredentials = "true")
@RequestMapping(value = "/file")
public class FileController {
@Autowired
private PlanFileRepository planFileRepository;
@Autowired
private StudentService studentService;
@Autowired
private CounselorService counselorService;
public static String getCellValue(Cell cell) {
if (cell == null) return "xxx";
if (cell.getCellType() == CellType.BOOLEAN) {
return String.valueOf(cell.getBooleanCellValue());
} else if (cell.getCellType() == CellType.NUMERIC) {
Double d = cell.getNumericCellValue();
return String.valueOf(d);
}
return String.valueOf(cell.getStringCellValue());
}
// 学生信息excel文档解析
@PostMapping("/upload/student")
@Transactional
public String uploadExcel (MultipartFile file) throws IOException, MsgException {
XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());
XSSFSheet sheet = workbook.getSheetAt(0);
int total = sheet.getPhysicalNumberOfRows();
for (int i = 1; i < total; i++) {
Row row = sheet.getRow(i);
Student student = new Student();
System.err.println(getCellValue(row.getCell(0)));
System.err.println(row.getCell(0).getCellType());
student.setStudentId(getCellValue(row.getCell(0)));
student.setName(getCellValue(row.getCell(1)));
if (getCellValue(row.getCell(2)).equals("男")) {
student.setGender(true);
} else {
student.setGender(false);
}
student.setCollege(getCellValue(row.getCell(3)));
student.setProfession(getCellValue(row.getCell(4)));
student.setPhoneNumber(getCellValue(row.getCell(5)));
student.setCode(getCellValue(row.getCell(6)));
student.setClassNum(getCellValue(row.getCell(7)));
student.setGradeNum(getCellValue(row.getCell(8)));
studentService.add(student);
}
return "上传成功!";
} // 教师信息excel文档解析
@PostMapping("/upload/teacher")
@Transactional
public String uploadExcel1 (MultipartFile file) throws IOException, MsgException {
XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());
XSSFSheet sheet = workbook.getSheetAt(0);
int total = sheet.getPhysicalNumberOfRows();
for (int i = 1; i < total; i++) {
Row row = sheet.getRow(i);
Counselor counselor = new Counselor();
counselor.setCounselorId(getCellValue(row.getCell(0)));
counselor.setName(getCellValue(row.getCell(1)));
if (getCellValue(row.getCell(2)).equals("男")) {
counselor.setGender(true);
} else {
counselor.setGender(false);
}
counselor.setCollege(getCellValue(row.getCell(3)));
counselor.setPhoneNumber(getCellValue(row.getCell(4)));
counselor.setCode(getCellValue(row.getCell(5)));
counselorService.add(counselor);
}
return "上传成功!";
}
// 文件上传
@PostMapping(value = "/upload/{id}")
public PlanFile upload(@PathVariable(name = "id") String extraId, @RequestParam(name = "name") String name, @RequestParam(name = "type") String type, @RequestParam(name = "file", required = false) MultipartFile file) throws IOException {
PlanFile planFile = new PlanFile();
planFile.setFile(file.getBytes());
planFile.setExtraId(extraId);
planFile.setName(name);
planFile.setType(type);
planFileRepository.save(planFile);
planFile.setFile(null);
return planFile;
}
// 获取文件列表
@GetMapping(value = "/{id}")
public List<PlanFile> getFile(@PathVariable(value = "id") String id) {
List<PlanFile> list = planFileRepository.findAllByExtraId(id);
List<PlanFile> ret = list.stream().filter(obj -> {
obj.setFile(null);
return true;
}).collect(Collectors.toList());
return ret;
}
// 删除单个文件
@DeleteMapping(value = "/{id}")
public void delete(@PathVariable(value = "id") String id) {
planFileRepository.deleteById(id);
}
// 下载文件
@GetMapping(value = "/download/{id}")
public String onePicture(@PathVariable(value = "id") String fileId, HttpServletResponse response) throws UnsupportedEncodingException {
PlanFile planFile = planFileRepository.getOne(fileId);
if (planFile.getFile() != null) {
response.setContentType("application/force-download");// 设置强制下载不打开
response.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(planFile.getName(), "UTF-8"));// 设置文件名
OutputStream os = null;
try {
os = response.getOutputStream();
os.write(planFile.getFile(), 0, planFile.getFile().length);
return "下载成功";
} catch (IOException e) {
e.printStackTrace();
}
}
return "下载失败";
}
// 下载文件模板
@GetMapping(value = "/download/student")
public void getStudentTemplate(HttpServletResponse response) throws UnsupportedEncodingException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("学生文件模板");
Row row = sheet.createRow(0);
String[] strings = new String[]{"学号", "姓名", "性别", "学院", "专业", "电话", "身份证号码", "班级", "年级"};
for (int i = 0; i < 9; i++) {
Cell cell = row.createCell(i);
cell.setCellValue(strings[i]);
}
response.setContentType("application/vnd.ms-excel");
response.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("学生信息导入.xlsx", "UTF-8"));// 设置文件名
OutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
workbook.write(outputStream);
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 下载文件模板
@GetMapping(value = "/download/teacher")
public void getTeacherTemplate(HttpServletResponse response) throws UnsupportedEncodingException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("教师文件模板");
Row row = sheet.createRow(0);
String[] strings = new String[]{"教师号", "姓名", "性别", "学院", "电话", "身份证号码"};
for (int i = 0; i < 6; i++) {
Cell cell = row.createCell(i);
cell.setCellValue(strings[i]);
}
response.setContentType("
没有合适的资源?快使用搜索试试~ 我知道了~
基于SpringBoot+Vue实现的竞赛系统.zip
共53个文件
java:49个
xml:1个
gitignore:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 1 下载量 183 浏览量
2023-06-28
16:01:34
上传
评论
收藏 41KB ZIP 举报
温馨提示
本资源中的源码都是经过本地编译过可运行的,下载后按照文档配置好环境就可以运行。资源项目的难度比较适中,内容都是经过助教老师审定过的,应该能够满足学习、使用需求,如果有需要的话可以放心下载使用。有任何问题也可以随时私信博主,博主会第一时间给您解答!!! 本资源中的源码都是经过本地编译过可运行的,下载后按照文档配置好环境就可以运行。资源项目的难度比较适中,内容都是经过助教老师审定过的,应该能够满足学习、使用需求,如果有需要的话可以放心下载使用。有任何问题也可以随时私信博主,博主会第一时间给您解答!!! 本资源中的源码都是经过本地编译过可运行的,下载后按照文档配置好环境就可以运行。资源项目的难度比较适中,内容都是经过助教老师审定过的,应该能够满足学习、使用需求,如果有需要的话可以放心下载使用。有任何问题也可以随时私信博主,博主会第一时间给您解答!!!
资源推荐
资源详情
资源评论
收起资源包目录
基于SpringBoot+Vue实现的竞赛系统.zip (53个子文件)
java-project
pom.xml 5KB
src
test
java
com
xxx
project
SimilarApplicationTests.java 217B
main
resources
application.properties 2KB
java
com
xxx
project
repository
CounselorRepository.java 806B
CommentRepository.java 400B
UserRepository.java 952B
ReviewRepository.java 693B
PlanFileRepository.java 400B
CompetitionRepository.java 340B
StudentRepository.java 782B
MessageRepository.java 1KB
controller
FileController.java 8KB
ReviewController.java 1KB
CompetitionController.java 2KB
UserController.java 2KB
LoginController.java 2KB
CommentController.java 1KB
CounselorController.java 2KB
MessageController.java 5KB
StudentController.java 2KB
serviceImpl
UserServiceImpl.java 3KB
CompetitionServiceImpl.java 3KB
ReviewServiceImpl.java 4KB
CommentServiceImpl.java 2KB
CounselorServiceImpl.java 3KB
StudentServiceImpl.java 4KB
base
BaseEntity.java 2KB
service
CounselorService.java 502B
UserService.java 472B
StudentService.java 475B
CompetitionService.java 371B
CommentService.java 421B
ReviewService.java 668B
common
LoginInterceptor.java 1KB
MyWebMvcConfiguration.java 1KB
CommandLineRunnerImpl.java 883B
MsgException.java 491B
MyConfig.java 1KB
entity
PlanFile.java 957B
Competition.java 3KB
Message.java 1KB
Review.java 2KB
Comment.java 1KB
Student.java 2KB
User.java 1KB
Counselor.java 1KB
ProjectApplication.java 585B
util
Md5Util.java 1KB
bean
UserInfo.java 832B
Result.java 612B
PageArgs.java 1KB
.gitignore 438B
competition.sql 12KB
共 53 条
- 1
资源评论
- 泪未眠2024-03-28资源内容详细,总结地很全面,与描述的内容一致,对我启发很大,学习了。
白话机器学习
- 粉丝: 8978
- 资源: 7681
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功