package com.sldl.service.app;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import com.sldl.dao.app.FileSaveDao;
import com.sldl.model.app.FileSave;
import com.sldl.model.jsp.User;
import com.sldl.utils.Configer;
import com.sldl.utils.UUIDUtil;
/**
-
@功能说明:Service实现
-
@author TANGJH
-
@date 2017-3-12
*/
@Service
public class FileService {@Resource
Configer config;@Autowired
private UUIDUtil uuidUtil;@Autowired
private Configer Config;
@Autowired
private FileSaveDao dao;/**
- 保存文件
/
public Map<String,Object> saveFile(FileSave fileSave, MultipartFile image) {
Map<String,Object> dataMap =new HashMap<>();
if (!image.isEmpty()) {
String oldName = image.getOriginalFilename();
List fileByTaskId = dao.getFileByTaskId(fileSave);
if(fileByTaskId.size()>0){
dataMap.put(“code”, 1);
dataMap.put(“msg”, “该文件已存在,请重新选择文件!”);
return dataMap;
}
//处理后缀
fileSave.setTitle(oldName);
String suffix = oldName.substring(oldName.indexOf("."), oldName.length());
String url = config.getBannerUrl();
fileSave.setDir(url);
File fileToSave=new File(url);
if(!fileToSave.isDirectory()){
fileToSave.mkdirs();
}
if (url.length() > 4) {
url = url.substring(0, 4);
}
String newFileName = uuidUtil.createUUIDByString(url);
fileSave.setRealName(newFileName+suffix);
String filePath = config.getBannerUrl() + newFileName+suffix;
fileSave.setFilePath(filePath);
File newfile = new File(filePath);
try {
/*
* 绑定默认任务116
*/
// fileSave.setTaskId(“116”);
dao.addFile(fileSave);
image.transferTo(newfile);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
dataMap.put(“code”, 0);
dataMap.put(“msg”, “文件保存成功!”);
}else{
dataMap.put(“code”, 1);
dataMap.put(“msg”, “文件为空!”);
}
return dataMap;
}
/**
- 删除文件
*/
@Transactional
public Map deleteFile(FileSave fileSave) {
Map<String,Object> map=new HashMap<>();
if (fileSave.getTitle() != null && !fileSave.getTitle().equals("")) {// 只有这个文件是非空
/// 删除文件
List fileByTaskId = dao.getFileByTaskId(fileSave);
for(int i=0;i<fileByTaskId.size();i++){
new File(fileByTaskId.get(i).getFilePath()).delete();// 删除文件
dao.delById(fileByTaskId.get(i));//删除记录
}
map.put(“code”, 0);
map.put(“msg”, “操作成功!”);
}else{
map.put(“code”, 1);
map.put(“msg”, “文件为空!”);
}
return map;
}
/**
- 下载文件
*/
public ResponseEntity<byte[]> fileDownLoad(FileSave fileSave) throws Exception{
List fileByTaskId = dao.getFileByTaskId(fileSave);
String fileName=fileByTaskId.get(0).getTitle();
// ServletContext servletContext = request.getServletContext();
//String fileName=“开发计划安排V1.1.xlsx”;
// String realPath = servletContext.getRealPath("/WEB-INF/"+fileName);//得到文件所在位置
//String realPath=“d:/wtts_file/3036414-760f0551f9b24978bfaddddc395c81a2.1.xlsx”;
String realPath=fileByTaskId.get(0).getFilePath();
InputStream in=new FileInputStream(new File(realPath));//将该文件加入到输入流之中
byte[] body=null;
body=new byte[in.available()];// 返回下一次对此输入流调用的方法可以不受阻塞地从此输入流读取(或跳过)的估计剩余字节数
in.read(body);//读入到输入流里面
fileName=new String(fileName.getBytes(“gbk”),“iso8859-1”);//防止中文乱码
HttpHeaders headers=new HttpHeaders();//设置响应头
headers.add(“Content-Disposition”, “attachment;filename=”+fileName);
HttpStatus statusCode = HttpStatus.OK;//设置响应吗
ResponseEntity<byte[]> response=new ResponseEntity<byte[]>(body, headers, statusCode);
return response;}
/**
-
获得附件列表
*/
public Map getFile(FileSave fileSave) {
Map<String,Object> map=new HashMap<>();
List fileByTaskId = dao.getFileByTaskId(fileSave);map.put(“code”, 0);
map.put(“data”, fileByTaskId);
map.put(“msg”, “操作成功!”);return map;
}
- 保存文件
}