package com.hrm.document.handler;
import com.hrm.commons.benas.Document;
import com.hrm.commons.benas.User;
import com.hrm.document.service.IDocumentService;
import com.hrm.utils.PageModel;
import lombok.var;
import org.apache.commons.io.FileUtils;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.List;
import java.util.UUID;
@Controller
@RequestMapping("/document")
public class DocumentHandler {
@Autowired
private IDocumentService documentService;
String path = "D:/upload";
@RequestMapping("/findDocument")
public String findDocument(Document document, Model model, @RequestParam(defaultValue = "1") int pageIndex) {
PageModel pageModel = new PageModel();
int count = documentService.findDcoumentCount(document);
pageModel.setRecordCount(count);
pageModel.setPageIndex(pageIndex);
List<Document> documents = documentService.findDocument(document, pageModel);
// System.out.println(document);
model.addAttribute("pageModel", pageModel);
model.addAttribute("document", document);
model.addAttribute("documents", documents);
return "/jsp/document/document.jsp";
}
@RequestMapping("/addDocument")
public String addDocument(Document document, HttpSession session) throws IOException {
String path = "D:/upload";
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
String filename = UUID.randomUUID() + "_" + document.getFile().getOriginalFilename();
document.getFile().transferTo(new File(path, filename));
document.setFilename(filename);
User loginUser = (User) session.getAttribute("loginUser");
document.setUser(loginUser);
documentService.addDocument(document);
PageModel pageModel = new PageModel();
int count = documentService.findDcoumentCount(null);
pageModel.setRecordCount(count);
return "redirect:/document/findDocument?pageIndex=" + pageModel.getTotalSize();
}
@RequestMapping("/modifyDocument")
public String modifyDocument(int pageIndex, Model model, int flag, Document document) throws IOException {
if (flag == 1) {
Document document1 = documentService.findDocumentById(document.getId());
// System.out.println(document1);
model.addAttribute("document", document1);
model.addAttribute("pageIndex", pageIndex);
return "/jsp/document/showUpdateDocument.jsp";
} else {
String path = "D:/upload";
//选择了文件新文件
if (!document.getFile().isEmpty()) {
Document document1 = documentService.findDocumentById(document.getId());
File targetFile = new File(path, document1.getFilename());
//用文件名查找是否存在,存在删除该文件
if (targetFile.exists()) {
targetFile.delete();
}
//把新文件保存
String filename = UUID.randomUUID() + "_" + document.getFile().getOriginalFilename();
document.getFile().transferTo(new File(path, filename));
document.setFilename(filename);
}
//修改数据库信息
int rows = documentService.modifyDocument(document);
if (rows > 0) {
return "redirect:/document/findDocument?pageIndex=" + pageIndex;
} else {
model.addAttribute("fail", "修改文件失败!!");
return "/jsp/fail.jsp";
}
}
}
@RequestMapping("/removeDocument")
public String removeDocument(int[] ids, Model model) {
int count = 0;
for (int id : ids) {
Document document = documentService.findDocumentById(id);
File targetFile = new File(path, document.getFilename());
if (targetFile.exists()) {
targetFile.delete();
}
int rows = documentService.removeDocument(id);
if (rows > 0) {
count++;
}
}
if (count == ids.length) {
return "/document/findDocument";
} else {
model.addAttribute("fail", "删除文件失败!!!");
return "/jsp/fail.jsp";
}
}
@RequestMapping("/downLoad")
public ResponseEntity<Object> downLoad(Integer id,HttpServletRequest request) throws IOException {
Document target = documentService.findDocumentById(id);
String filename = target.getFilename();
File file = new File(path, filename);
if (file.exists()) {
filename = processFileName(request,filename);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", filename);
return new ResponseEntity<Object>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK);
} else {
String error = "下载的文件丢失!!";
HttpHeaders headers = new HttpHeaders();
MediaType mediaType = new MediaType("text", "html", Charset.forName("utf-8"));
headers.setContentType(mediaType);
return new ResponseEntity<Object>(error, headers, HttpStatus.OK);
}
}
//IE、chrom、Firefox文件中文乱码问题
public String processFileName(HttpServletRequest request, String fileNames) {
String codedfilename = null;
try {
String agent = request.getHeader("USER-AGENT");
if (null != agent && -1 != agent.indexOf("MSIE") || null != agent &&
-1 != agent.indexOf("Trident")) {//ie
String name = java.net.URLEncoder.encode(fileNames, "UTF8");
codedfilename = name;
} else if (null != agent&&-1!=agent.indexOf("Mozilla")) {//火狐谷歌等
codedfilename=new String(fileNames.getBytes("UTF-8"),"iso8859-1");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return codedfilename;
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/qq_43842093/article/details/122014422
内容来源于网络,如有侵权,请联系作者删除!