Examination_system/Examination_system-1/.svn/pristine/d4/d43f83ff720536be4eea7058809...

216 lines
6.8 KiB
Plaintext
Raw Normal View History

2023-10-30 13:10:40 +08:00
package com.bonus.sys.service;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import com.bonus.core.DateTimeHelper;
import com.bonus.core.ExcelUtils;
import com.bonus.core.ReflectUtil;
import com.bonus.sys.BaseServiceImp;
import com.bonus.sys.GlobalConst;
import com.bonus.sys.PropertyHelper;
import com.bonus.sys.UserShiroHelper;
import com.bonus.sys.beans.FileUploadInfoBean;
import com.bonus.sys.beans.SysDataDictBean;
import com.bonus.sys.dao.FileUploadInfoDao;
import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;
@Service("FileUploadService")
public class FileUploadServiceImp extends BaseServiceImp<FileUploadInfoBean> implements FileUploadService{
@Autowired
FileUploadInfoDao fileDao;
@Override
public void downloadDocument(HttpServletRequest request, HttpServletResponse response,FileUploadInfoBean o) {
FileUploadInfoBean file = fileDao.findByFileId(o);
if(file != null) {
String realSavePath = PropertyHelper.getPropertyByKey("fileSavePath");
String filePath = realSavePath+file.getFolderName()+"/"+file.getSaveName()+file.getSuffix();
System.err.println("filePath="+filePath);
try {
downLoad(request,file,response);
} catch (Exception e) {
logger.error("下载失败"+e,e);
e.printStackTrace();
}
}else {
}
}
private void downLoad(HttpServletRequest request,FileUploadInfoBean file, HttpServletResponse response) {
try {
//String paths = request.getSession().getServletContext().getRealPath("");
String realSavePath = PropertyHelper.getPropertyByKey("fileSavePath");
String filePath = realSavePath+file.getFolderName()+"/"+file.getSaveName()+file.getSuffix();
File f = new File(filePath);
if (!f.exists()) {
logger.warn("文件不存在");
}
String fileName = f.getName();
fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;
response.reset(); // 非常重要
// 纯下载方式
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0){
out.write(buf, 0, len);
}
br.close();
out.close();
} catch (Exception e) {
logger.warn("download file error:", e);
}
}
@Override
public void downloadTemplate(HttpServletRequest request, HttpServletResponse response, FileUploadInfoBean o) {
String realSavePath = PropertyHelper.getPropertyByKey("fileSavePath");
String flag = o.getModelFlag();
String modelFlag = GlobalConst.map.get(flag);
String[] classParam = ReflectUtil.getTypeAnnotation(modelFlag);
o.setModelFlag(classParam[0]);
List<FileUploadInfoBean> file = fileDao.findByFileModelFlag(o);
if(file.size()>0) {
try {
downLoadTemplate(request,file.get(0),response);
} catch (Exception e) {
logger.error("下载失败"+e,e);
e.printStackTrace();
}
}else {
long newName=System.currentTimeMillis();
String[] titles = ReflectUtil.getTitleValue(modelFlag);
ExcelUtils.createExcel(titles, realSavePath+"/template/"+newName+".xlsx");
FileUploadInfoBean newfile = new FileUploadInfoBean();
newfile.setCreateTime(DateTimeHelper.currentDateTime());
newfile.setFolderName("/template");
newfile.setModelFlag(classParam[0]);
newfile.setPreName(classParam[1]);
newfile.setSaveName(newName+"");
newfile.setSuffix(".xlsx");
newfile.setType(new SysDataDictBean(57));
Integer result = fileDao.insertBean(newfile);
if(result == 1) {
downLoadTemplate(request,newfile,response);
}
}
}
private void downLoadTemplate(HttpServletRequest request,FileUploadInfoBean file,HttpServletResponse response){
String realSavePath = PropertyHelper.getPropertyByKey("fileSavePath");
try {
String filePath = realSavePath+file.getFolderName()+"/"+file.getSaveName()+file.getSuffix();
String downName = file.getPreName()+file.getSuffix();
File f = new File(filePath);
if (!f.exists()) {
logger.warn("文件不存在");
}
String fileName = f.getName();
fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
downName = new String(downName.getBytes("UTF-8"), "ISO-8859-1");
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;
response.reset(); // 非常重要
// 纯下载方式
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=" + downName);
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0){
out.write(buf, 0, len);
}
br.close();
out.close();
} catch (Exception e) {
logger.warn("download file error:", e);
}
}
@Override
public String getClassName(String shortName) {
return null;
}
@Override
public String findUrlByOwnerId(Integer wonerId) {
FileUploadInfoBean bean=new FileUploadInfoBean();
bean.setOwnerId(wonerId);
bean.setModelFlag("signature");
FileUploadInfoBean o=fileDao.findByOwnerId(bean);
String url=null;
if(o!=null) {
url=o.getFolderName()+"/"+o.getSaveName()+o.getSuffix();
}
return url;
}
@Override
public String findUrlByOwnerId2(Integer wonerId) {
FileUploadInfoBean bean=new FileUploadInfoBean();
bean.setOwnerId(wonerId);
bean.setModelFlag("scrapUpload");
FileUploadInfoBean o=fileDao.findByOwnerId(bean);
String url=null;
if(o!=null) {
url=o.getFolderName()+"/"+o.getSaveName();
}
return url;
}
@Override
public void updateOwnerId(FileUploadInfoBean fub) {
String[] ids = fub.getIds().split(",");
HashMap<String,Object> idsMap=new HashMap<String,Object>();
idsMap.put("idss",ids );
fileDao.updateOwnerId(fub,idsMap);
}
}