package com.ld.igds.basic.controller;
|
|
import com.ld.igds.basic.manager.FileManager;
|
import com.ld.igds.data.PageResponse;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.io.FileUtils;
|
import org.apache.commons.lang.time.DateUtils;
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import javax.servlet.ServletOutputStream;
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
|
import java.io.*;
|
import java.net.URLEncoder;
|
import java.util.Date;
|
|
/**
|
* 文件上传接口
|
*
|
* @author
|
*/
|
@Slf4j
|
@RestController
|
@RequestMapping("basic/file")
|
public class FileController {
|
|
@Autowired
|
private FileManager fileManager;
|
|
/**
|
* 长传 出入库图片
|
*
|
* @param file
|
* @param request
|
* @return
|
* @throws IOException
|
*/
|
@RequestMapping("/update-file")
|
public PageResponse<String> updateAreationPos(
|
@RequestParam(value = "file", required = true) MultipartFile file,
|
HttpServletRequest request) throws IOException {
|
|
String plateNum = request.getParameter("plateNum");
|
|
return fileManager.upLoadInoutImg(file, plateNum);
|
}
|
|
/**
|
* 下载从临时目录下载,需要项目名称和当前组织
|
*
|
* @param request
|
* @return
|
* @throws IOException
|
*/
|
@RequestMapping("/download-temp")
|
public int downloadTemp(
|
@RequestParam(value = "fileName", required = false) String fileName,
|
@RequestParam(value = "companyId", required = false) String companyId,
|
HttpServletRequest request, HttpServletResponse response)
|
throws IOException {
|
|
if (StringUtils.isEmpty(fileName) || StringUtils.isEmpty(companyId)) {
|
response.sendError(404, "缺少下载参数条件,无法执行下载。");
|
return 404;
|
}
|
File file = fileManager.getTempFilePath(fileName, companyId);
|
|
if (!file.exists()) {
|
response.sendError(404, "没有获取到需要下载的文件,可能已被删除");
|
return 404;
|
}
|
|
response.setContentType("application/octet-stream");
|
response.setHeader("content-type", "application/octet-stream");
|
response.setHeader("Content-Length", String.valueOf(file.length()));
|
response.setHeader("Content-Disposition", "attachment;fileName="
|
+ URLEncoder.encode(fileName, "utf8"));
|
byte[] buffer = new byte[1024];
|
|
OutputStream os;
|
try (FileInputStream fis = new FileInputStream(file);
|
BufferedInputStream bis = new BufferedInputStream(fis)) {
|
os = response.getOutputStream();
|
int i = bis.read(buffer);
|
while (i != -1) {
|
os.write(buffer);
|
i = bis.read(buffer);
|
}
|
|
fis.close();
|
bis.close();
|
os.close();
|
os.flush();
|
} catch (Exception e) {
|
response.sendError(500, "文件下载出错:" + e.getMessage());
|
|
log.error("------------文件下载失败--{}", e);
|
return 500;
|
}
|
|
return 200;
|
}
|
|
/**
|
* 文件流获取图片显示到页面--针对仓内抓拍
|
*
|
* @param fileName
|
* @param timeStr
|
* 抓拍的年月日时分秒 yyyyMMdd
|
* @param response
|
* @return
|
* @throws IOException
|
*/
|
@RequestMapping(value = "/get-snap-depot", method = RequestMethod.GET, produces = { "application/vnd.ms-excel;charset=UTF-8" })
|
public String getDepotSnap(String fileName, String timeStr,
|
HttpServletResponse response) throws IOException {
|
|
// 设置返回内容格式
|
response.setContentType("image/jpeg/jpg/png/gif/bmp/tiff/svg");
|
|
String filePath;
|
// 创建一个输入流
|
InputStream in = null;
|
// 创建输出流
|
OutputStream os = null;
|
try {
|
if (null == fileName || null == timeStr) {
|
filePath = fileManager.getFailImg();
|
}else{
|
filePath = fileManager.getSnapFilePath(fileName, timeStr);
|
|
File file = new File(filePath);
|
|
if(file.exists()){
|
log.debug("----抓拍图片地址-------{}", filePath);
|
}else{
|
filePath = fileManager.getFailImg();
|
log.debug("----未获取到图纸,默认地址-------{}", filePath);
|
}
|
}
|
|
// 用该文件创建一个输入流
|
in = new FileInputStream(filePath);
|
// 创建输出流
|
os = response.getOutputStream();
|
byte[] b = new byte[1024];
|
while (in.read(b) != -1) {
|
os.write(b);
|
}
|
in.close();
|
os.close();
|
|
return null;
|
|
} catch (Exception e) {
|
e.printStackTrace();
|
return null;
|
} finally {
|
if (null != in) {
|
in.close();
|
}
|
if (null != os) {
|
os.close();
|
}
|
}
|
}
|
|
/**
|
* PDF文件预览
|
* @param response
|
*/
|
@RequestMapping("/show-pdf")
|
public void showFile(String fileTime, String fileName, String filePathType, HttpServletResponse response) throws Exception {
|
|
if (!fileName.endsWith(".pdf")) {
|
return;
|
}
|
Date time = DateUtils.parseDate(fileTime, new String[]{"yy-MM-dd HH:mm:ss"});
|
|
String filePath = fileManager.getPathByType(filePathType, time);
|
if(StringUtils.isEmpty(filePath)){
|
return;
|
}
|
|
File file = new File(filePath, fileName);
|
try {
|
response.setHeader("Content-Disposition", "inline; filename*=UTF-8''" + URLEncoder.encode(file.getName(), "UTF-8"));
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
|
ServletOutputStream os = null;
|
try {
|
os = response.getOutputStream();
|
os.write(FileUtils.readFileToByteArray(file));
|
os.flush();
|
} catch (IOException e) {
|
e.printStackTrace();
|
} finally {
|
if (os != null) {
|
try {
|
os.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
}
|
|
}
|