package com.nq.controller;
|
|
import com.nq.utils.PropertiesUtil;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.stereotype.Controller;
|
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.IOException;
|
import java.io.OutputStream;
|
|
@Controller
|
public class ImageController {
|
private static final Logger log = LoggerFactory.getLogger(ImageController.class);
|
|
@RequestMapping({"/imgs/{filename:.+}"})
|
public void getImage(@PathVariable String filename, HttpServletResponse response) {
|
FileInputStream fis = null;
|
OutputStream os = null;
|
try {
|
String pdfDir = PropertiesUtil.getProperty("loca.pdf.dir");
|
File imageFile = new File(pdfDir, filename);
|
|
if (!imageFile.exists()) {
|
log.warn("图片文件不存在: {}", imageFile.getAbsolutePath());
|
response.sendError(HttpServletResponse.SC_NOT_FOUND, "图片文件不存在");
|
return;
|
}
|
|
// 设置响应头
|
response.setContentType("image/png");
|
response.setHeader("Content-Disposition", "inline; filename=\"" + filename + "\"");
|
response.setContentLengthLong(imageFile.length());
|
response.setHeader("Cache-Control", "public, max-age=3600");
|
|
fis = new FileInputStream(imageFile);
|
os = response.getOutputStream();
|
|
byte[] buffer = new byte[8192];
|
int bytesRead;
|
while ((bytesRead = fis.read(buffer)) != -1) {
|
os.write(buffer, 0, bytesRead);
|
}
|
os.flush();
|
} catch (Exception e) {
|
log.error("读取图片文件失败: {}", filename, e);
|
try {
|
if (!response.isCommitted()) {
|
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "读取图片失败");
|
}
|
} catch (IOException ex) {
|
log.error("发送错误响应失败", ex);
|
}
|
} finally {
|
if (fis != null) {
|
try {
|
fis.close();
|
} catch (IOException e) {
|
log.error("关闭文件流失败", e);
|
}
|
}
|
if (os != null) {
|
try {
|
os.close();
|
} catch (IOException e) {
|
log.error("关闭输出流失败", e);
|
}
|
}
|
}
|
}
|
|
@RequestMapping({"imgs.do"})
|
public void getImageByParam(@RequestParam("file") String filename, HttpServletResponse response) {
|
FileInputStream fis = null;
|
OutputStream os = null;
|
try {
|
String pdfDir = PropertiesUtil.getProperty("loca.pdf.dir");
|
File imageFile = new File(pdfDir, filename);
|
|
if (!imageFile.exists()) {
|
log.warn("图片文件不存在: {}", imageFile.getAbsolutePath());
|
response.sendError(HttpServletResponse.SC_NOT_FOUND, "图片文件不存在");
|
return;
|
}
|
|
// 设置响应头
|
response.setContentType("image/png");
|
response.setHeader("Content-Disposition", "inline; filename=\"" + filename + "\"");
|
response.setContentLengthLong(imageFile.length());
|
response.setHeader("Cache-Control", "public, max-age=3600");
|
|
fis = new FileInputStream(imageFile);
|
os = response.getOutputStream();
|
|
byte[] buffer = new byte[8192];
|
int bytesRead;
|
while ((bytesRead = fis.read(buffer)) != -1) {
|
os.write(buffer, 0, bytesRead);
|
}
|
os.flush();
|
} catch (Exception e) {
|
log.error("读取图片文件失败: {}", filename, e);
|
try {
|
if (!response.isCommitted()) {
|
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "读取图片失败");
|
}
|
} catch (IOException ex) {
|
log.error("发送错误响应失败", ex);
|
}
|
} finally {
|
if (fis != null) {
|
try {
|
fis.close();
|
} catch (IOException e) {
|
log.error("关闭文件流失败", e);
|
}
|
}
|
if (os != null) {
|
try {
|
os.close();
|
} catch (IOException e) {
|
log.error("关闭输出流失败", e);
|
}
|
}
|
}
|
}
|
}
|