From bd5cdeef189cf31f2b3b3fc54a1828beae8d6063 Mon Sep 17 00:00:00 2001 From: mjkhan21 Date: Fri, 23 Feb 2024 14:44:07 +0900 Subject: [PATCH] =?UTF-8?q?DownloadView,=20XLSView=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cokr/xit/base/file/Downloadable.java | 144 +++++++++++++++++- .../cokr/xit/base/file/web/DownloadView.java | 21 ++- .../java/cokr/xit/base/file/web/XLSView.java | 10 +- 3 files changed, 159 insertions(+), 16 deletions(-) diff --git a/src/main/java/cokr/xit/base/file/Downloadable.java b/src/main/java/cokr/xit/base/file/Downloadable.java index 55431b4..ed127ed 100644 --- a/src/main/java/cokr/xit/base/file/Downloadable.java +++ b/src/main/java/cokr/xit/base/file/Downloadable.java @@ -1,15 +1,145 @@ package cokr.xit.base.file; -import java.io.OutputStream; +import java.io.File; +import java.io.InputStream; +import java.net.URLConnection; -public interface Downloadable { - String getFilename(); +import cokr.xit.foundation.Assert; - String getContentType(); +public class Downloadable { + private Object file; + private String + charset, + filename, + contentType, + disposition; + private Number length; - String getDisposition(); + /**파일을 반환한다. + * @return 파일 + */ + public Object getFile() { + return file; + } - Number getLength(); + /**파일을 설정한다. + * @param file 파일 + * @return 현재 Downloadable + */ + public Downloadable setFile(Object file) { + this.file = file; + return this; + } - void write(OutputStream out); + /**문자셋을 반환한다. + * @return 문자셋 + */ + public String getCharset() { + return Assert.ifEmpty(charset, "UTF-8"); + } + + /**문자셋을 설정한다. + * @param charset 문자셋 + * @return 현재 Downloadable + */ + public Downloadable setCharset(String charset) { + this.charset = charset; + return this; + } + + /**파일이름을 반환한다. + * @return 파일이름 + */ + public String getFilename() { + if (Assert.isEmpty(filename)) { + if (file instanceof File) + filename = ((File)file).getName(); + if (file instanceof FileInfo) + filename = ((FileInfo)file).getName(); + } + return Assert.notEmpty(filename, "filename"); + } + + /**파일이름을 설정한다. + * @param filename 파일이름 + * @return 현재 Downloadable + */ + public Downloadable setFilename(String filename) { + this.filename = filename; + return this; + } + + /**contentType을 반환한다. + * @return contentType + */ + public String getContentType() { + if (Assert.isEmpty(contentType)) + try { + if (file instanceof InputStream) { + InputStream input = (InputStream)file; + contentType = URLConnection.guessContentTypeFromStream(input); + } + if (file instanceof File) { + File file = (File)this.file; + contentType = URLConnection.guessContentTypeFromName(file.getName()); + } + if (file instanceof FileInfo) { + FileInfo fileInfo = (FileInfo)file; + contentType = fileInfo.getMimeType(); + } + } catch (Exception e) { + throw Assert.runtimeException(e); + } + + return Assert.ifEmpty(contentType, "application/octet-stream"); + } + + /**contentType을 설정한다. + * @param contentType contentType + * @return 현재 Downloadable + */ + public Downloadable setContentType(String contentType) { + this.contentType = contentType; + return this; + } + + /**disposition을 반환한다. + * @return disposition + */ + public String getDisposition() { + return Assert.ifEmpty(disposition, "attachment"); + } + + /**disposition을 설정한다. + * @param disposition disposition + * @return 현재 Downloadable + */ + public Downloadable setDisposition(String disposition) { + this.disposition = disposition; + return this; + } + + /**파일길이를 반환한다. + * @return 파일길이 + */ + public Number getLength() { + if (file instanceof File) { + File file = (File)this.file; + length = file.length(); + } + if (file instanceof FileInfo) { + FileInfo fileInfo = (FileInfo)this.file; + length = fileInfo.getSize(); + } + return length; + } + + /**파일길이를 설정한다. + * @param length 파일길이 + * @return 현재 Downloadable + */ + public Downloadable setLength(Number length) { + this.length = length; + return this; + } } \ No newline at end of file diff --git a/src/main/java/cokr/xit/base/file/web/DownloadView.java b/src/main/java/cokr/xit/base/file/web/DownloadView.java index c5a0027..ba2743d 100644 --- a/src/main/java/cokr/xit/base/file/web/DownloadView.java +++ b/src/main/java/cokr/xit/base/file/web/DownloadView.java @@ -48,9 +48,10 @@ public class DownloadView extends AbstractView { } String disposition = Assert.ifEmpty((String)model.get("disposition"), "attachment"); - hresp.setCharacterEncoding("UTF-8"); + String charset = getCharset(model); + hresp.setCharacterEncoding(charset); hresp.setContentType(contentType); - hresp.setHeader("Content-Disposition", disposition + "; filename=\"" + URLEncoder.encode(filename, "UTF-8") +"\""); + hresp.setHeader("Content-Disposition", disposition + "; filename=\"" + URLEncoder.encode(filename, charset) +"\""); Number length = getLength(model); if (length != null) hresp.setContentLengthLong(length.longValue()); @@ -61,7 +62,12 @@ public class DownloadView extends AbstractView { } } - private InputStream getInputStream(Map model) throws Exception { + private static String getCharset(Map model) { + Object obj = model.get("charset"); + return obj != null ? obj.toString() : "UTF-8"; + } + + private static InputStream getInputStream(Map model) throws Exception { Object obj = model.get("file"); if (obj == null) return null; @@ -75,10 +81,11 @@ public class DownloadView extends AbstractView { FileInfo fileInfo = (FileInfo)obj; return fileInfo.getInputStream(); } + throw new IllegalArgumentException("'file' must be either a " + File.class.getName() + ", a " + InputStream.class.getName() + ", or a " + FileInfo.class.getName()); } - private String getFilename(Map model) { + public static String getFilename(Map model) { String filename = (String)model.get("filename"); if (!Assert.isEmpty(filename)) return filename; @@ -94,7 +101,7 @@ public class DownloadView extends AbstractView { return null; } - private String getContentType(Map model) throws Exception { + private static String getContentType(Map model) throws Exception { String contentType = (String)model.get("contentType"); if (!Assert.isEmpty(contentType)) return contentType; @@ -114,7 +121,7 @@ public class DownloadView extends AbstractView { return null; } - private Number getLength(Map model) { + private static Number getLength(Map model) { Number length = (Number)model.get("length"); if (!Assert.isEmpty(length)) return length; @@ -130,7 +137,7 @@ public class DownloadView extends AbstractView { return null; } - private void clear(Map model) { + private static void clear(Map model) { Object obj = model.get("file"); if (!(obj instanceof File)) return; diff --git a/src/main/java/cokr/xit/base/file/web/XLSView.java b/src/main/java/cokr/xit/base/file/web/XLSView.java index fe1dc8d..0208660 100644 --- a/src/main/java/cokr/xit/base/file/web/XLSView.java +++ b/src/main/java/cokr/xit/base/file/web/XLSView.java @@ -31,8 +31,9 @@ public class XLSView extends AbstractView { protected void renderMergedOutputModel(Map model, HttpServletRequest hreq, HttpServletResponse hresp) throws Exception { XLS xls = (XLS)model.get("xls"); - String charset = "UTF-8"; - String filename = xls.getFilename(); + String + charset = "UTF-8", + filename = xls.getFilename(); hresp.setCharacterEncoding(charset); hresp.setContentType(XLS.MIME_TYPE); @@ -40,4 +41,9 @@ public class XLSView extends AbstractView { xls.write(hresp.getOutputStream()); } + + public static String getFilename(Map model) { + XLS xls = (XLS)model.get("xls"); + return xls != null ? xls.getFilename() : null; + } } \ No newline at end of file