ssm文件导出____IE、chrom、Firefox文件中文乱码问题
2023-09-14 09:14:53 时间
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;
}
}
相关文章
- Python 中文文件统计词频 + 中文词云
- Unity3D脚本中文系列教程(七)
- 如何解决GBK的编码的文件中的中文转换成为UTF-8编码的文件而且不乱码
- Zabbix5.0中文乱码
- 【问题解决方案】本地代码文件上传到GitHub里中文乱码问题
- 91. SAP 系统里的中文数据,用 ABAP 导出成 Excel 文件时遇到乱码的分析和解决办法
- SAP UI5 应用的中文乱码问题
- paip.enhes efis 自动获取文件的中文编码
- 成功解决利用pandas的read_csv函数读取csv文件的时候出现中文乱码问题
- 中文单个字语音识别
- 大话中文文本分类之TextRCNN
- Windows Mobile 6.0 SDK和中文模拟器下载
- HTMLTestRunner生成报告中有中文乱码解决方案
- 论文投稿指南——中国(中文EI)期刊推荐(第3期)
- 异想天开 | 假如用中文写代码,是一种什么体验?