首页>代码>SSM开发网站考试平台管理系统,SSM+MAVEN初学者必备>/ExaSystem/src/main/java/com/team/controller/ProExa_controller.java
package com.team.controller; import com.team.code.PageJsonResult; import com.team.pojo.ProFile; import com.team.pojo.UploadFile; import com.team.service.ProExa_service; import com.team.until.SearchInfo; import org.apache.commons.io.FileUtils; 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.ModelMap; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.io.File; import java.io.IOException; import java.net.URLDecoder; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; @Controller @RequestMapping("proExa") public class ProExa_controller { @Resource ProExa_service proExa_service; @RequestMapping("toExa") public String toExa(){ return "Examine/exaMain"; } @RequestMapping("toFront") public String toFront(){ return "Test/Analogfront"; } @RequestMapping("pExa") @ResponseBody public PageJsonResult getProExa(Integer pageSize, Integer pageNumber, String searchText, SearchInfo info){ if (searchText!=null&&searchText.length()>=0) info.setWhere("where proName like '%"+searchText+"%'"); else info.setWhere(""); info.setPageSize(pageSize); info.setPageNumber(pageNumber); List<ProFile> list = proExa_service.getAllTeam(info); //获取ProFile数据库中的总条数 int fileCount = proExa_service.getCountForFile(); PageJsonResult pjr = new PageJsonResult(); pjr.setTotal(fileCount); pjr.setRows(list); System.out.println(pageSize+":"+pageNumber); return pjr; } @RequestMapping("dl") public ResponseEntity<byte[]> downloadFile(HttpServletRequest request, String teamName, String fileName, ModelMap model) throws Exception { teamName = URLDecoder.decode(teamName,"UTF-8"); fileName = URLDecoder.decode(fileName,"UTF-8"); System.out.println(teamName+fileName); // 下载文件路径 // String path = request.getServletContext().getRealPath("/images/"); String path = "F:/java ee/apache-tomcat-upload/file/"+teamName; File file = new File(path + File.separator + fileName); HttpHeaders headers = new HttpHeaders(); String downloadFielName = new String(fileName.getBytes("UTF-8"), "iso-8859-1"); headers.setContentDispositionFormData("attachment", downloadFielName); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED); } @RequestMapping("test") public void get(String teamName,String fileName) throws Exception { teamName = URLDecoder.decode(teamName,"UTF-8"); fileName = URLDecoder.decode(fileName,"UTF-8"); System.out.println(teamName+":"+fileName); } // 上传题目 @RequestMapping("re") public String register(HttpServletRequest request, @ModelAttribute UploadFile uploadFile, ModelMap m) throws IllegalStateException, IOException { String fn ="AnalogFrontEndUpload"; // 如果文件不为空,写入上传路径 if (!uploadFile.getFile().isEmpty()) { // 上传文件路径 String path = "F:/java ee/apache-tomcat-upload/file/"+fn; // 上传文件名 String filename = uploadFile.getFile().getOriginalFilename(); File filepath = new File(path, filename); // 判断路径是否存在,如果不存在就创建一个 if (!filepath.getParentFile().exists()) { filepath.getParentFile().mkdirs(); } // 将上传文件保存到一个目标文件当中 uploadFile.getFile().transferTo(new File(path + File.separator + filename)); //Mysql数据库操作: toMySqlRe(filename,path, request,fn); return toExa(); } else { return ""; } } public String toMySqlRe(String filename,String path,HttpServletRequest request,String fn){ String time = new SimpleDateFormat("yyyy-MM-dd").format(new Date()); ProFile proFile = new ProFile(); proFile.setTeamName(fn); proFile.setFileName(filename); proFile.setProName(fn); proFile.setPath(path); proFile.setTime(time); proExa_service.UploadInsert(proFile); return toExa(); } }
最近下载更多
lilong007 LV22
2023年12月30日
gecongkai LV8
2023年6月22日
lbsers LV5
2023年5月6日
朱俪的邮件及存储 LV8
2023年4月19日
LITIANYU084414 LV11
2023年1月1日
云龙123456 LV7
2022年12月2日
zhuwang LV5
2022年11月17日
qwer123546 LV13
2022年9月28日
mdz123456mao LV3
2022年6月19日
chenhan413 LV10
2022年5月20日