Files
wace_plm/WebContent/WEB-INF/view/viewImageByObjId.jsp
2025-12-09 11:53:57 +09:00

69 lines
2.0 KiB
Plaintext

<%@ page import="java.io.*, java.util.*" %>
<%@ page import="java.net.URLEncoder"%>
<%!
public void viewImage(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
// request attribute에서 파일 정보 가져오기 (컨트롤러에서 설정)
String attDir = (String) req.getAttribute("attDir");
String savedFileName = (String) req.getAttribute("savedFileName");
String realFileName = (String) req.getAttribute("realFileName");
if(attDir == null || savedFileName == null) {
System.out.println("viewImageByObjId: 파일 정보 없음");
return;
}
FileInputStream fis = null;
// 헤더 컨텐츠 타입 설정
res.reset();
// 파일 확장자로 컨텐츠 타입 결정
String ext = "";
if(savedFileName.lastIndexOf(".") > -1) {
ext = savedFileName.substring(savedFileName.lastIndexOf(".") + 1).toLowerCase();
}
if("png".equals(ext)) {
res.setContentType("image/png");
} else if("gif".equals(ext)) {
res.setContentType("image/gif");
} else if("bmp".equals(ext)) {
res.setContentType("image/bmp");
} else if("webp".equals(ext)) {
res.setContentType("image/webp");
} else {
res.setContentType("image/jpeg");
}
ServletOutputStream out = res.getOutputStream();
try {
File file = new File(attDir, savedFileName);
System.out.println("viewImageByObjId: " + file.getAbsolutePath());
fis = new FileInputStream(file);
byte[] buf = new byte[4 * 1024];
int bytesRead;
while((bytesRead = fis.read(buf)) != -1) {
out.write(buf, 0, bytesRead);
}
} catch(FileNotFoundException e) {
out.println("File Not Found");
System.out.println("viewImageByObjId File Not Found: attDir[" + attDir + "] savedFileName[" + savedFileName + "]");
} catch(IOException e) {
out.println("Problem sending file: " + e.getMessage());
System.out.println("viewImageByObjId Problem sending file: " + e.getMessage());
} finally {
if(fis != null) {
fis.close();
}
}
}
%>
<%
out.clear();
out = pageContext.pushBody();
viewImage(request, response);
%>