DownloadView, XLSView 수정

master
mjkhan21 9 months ago
parent 1ab36fe80e
commit bd5cdeef18

@ -1,15 +1,145 @@
package cokr.xit.base.file; package cokr.xit.base.file;
import java.io.OutputStream; import java.io.File;
import java.io.InputStream;
import java.net.URLConnection;
public interface Downloadable { import cokr.xit.foundation.Assert;
String getFilename();
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;
}
/** .
* @param file
* @return Downloadable
*/
public Downloadable setFile(Object file) {
this.file = file;
return this;
}
/** .
* @return
*/
public String getCharset() {
return Assert.ifEmpty(charset, "UTF-8");
}
Number getLength(); /** .
* @param charset
* @return Downloadable
*/
public Downloadable setCharset(String charset) {
this.charset = charset;
return this;
}
void write(OutputStream out); /** .
* @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;
}
} }

@ -48,9 +48,10 @@ public class DownloadView extends AbstractView {
} }
String disposition = Assert.ifEmpty((String)model.get("disposition"), "attachment"); String disposition = Assert.ifEmpty((String)model.get("disposition"), "attachment");
hresp.setCharacterEncoding("UTF-8"); String charset = getCharset(model);
hresp.setCharacterEncoding(charset);
hresp.setContentType(contentType); 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); Number length = getLength(model);
if (length != null) if (length != null)
hresp.setContentLengthLong(length.longValue()); hresp.setContentLengthLong(length.longValue());
@ -61,7 +62,12 @@ public class DownloadView extends AbstractView {
} }
} }
private InputStream getInputStream(Map<String, Object> model) throws Exception { private static String getCharset(Map<String, Object> model) {
Object obj = model.get("charset");
return obj != null ? obj.toString() : "UTF-8";
}
private static InputStream getInputStream(Map<String, Object> model) throws Exception {
Object obj = model.get("file"); Object obj = model.get("file");
if (obj == null) return null; if (obj == null) return null;
@ -75,10 +81,11 @@ public class DownloadView extends AbstractView {
FileInfo fileInfo = (FileInfo)obj; FileInfo fileInfo = (FileInfo)obj;
return fileInfo.getInputStream(); return fileInfo.getInputStream();
} }
throw new IllegalArgumentException("'file' must be either a " + File.class.getName() + ", a " + InputStream.class.getName() + ", or a " + FileInfo.class.getName()); throw new IllegalArgumentException("'file' must be either a " + File.class.getName() + ", a " + InputStream.class.getName() + ", or a " + FileInfo.class.getName());
} }
private String getFilename(Map<String, Object> model) { public static String getFilename(Map<String, Object> model) {
String filename = (String)model.get("filename"); String filename = (String)model.get("filename");
if (!Assert.isEmpty(filename)) return filename; if (!Assert.isEmpty(filename)) return filename;
@ -94,7 +101,7 @@ public class DownloadView extends AbstractView {
return null; return null;
} }
private String getContentType(Map<String, Object> model) throws Exception { private static String getContentType(Map<String, Object> model) throws Exception {
String contentType = (String)model.get("contentType"); String contentType = (String)model.get("contentType");
if (!Assert.isEmpty(contentType)) return contentType; if (!Assert.isEmpty(contentType)) return contentType;
@ -114,7 +121,7 @@ public class DownloadView extends AbstractView {
return null; return null;
} }
private Number getLength(Map<String, Object> model) { private static Number getLength(Map<String, Object> model) {
Number length = (Number)model.get("length"); Number length = (Number)model.get("length");
if (!Assert.isEmpty(length)) return length; if (!Assert.isEmpty(length)) return length;
@ -130,7 +137,7 @@ public class DownloadView extends AbstractView {
return null; return null;
} }
private void clear(Map<String, Object> model) { private static void clear(Map<String, Object> model) {
Object obj = model.get("file"); Object obj = model.get("file");
if (!(obj instanceof File)) return; if (!(obj instanceof File)) return;

@ -31,8 +31,9 @@ public class XLSView extends AbstractView {
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest hreq, HttpServletResponse hresp) throws Exception { protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest hreq, HttpServletResponse hresp) throws Exception {
XLS xls = (XLS)model.get("xls"); XLS xls = (XLS)model.get("xls");
String charset = "UTF-8"; String
String filename = xls.getFilename(); charset = "UTF-8",
filename = xls.getFilename();
hresp.setCharacterEncoding(charset); hresp.setCharacterEncoding(charset);
hresp.setContentType(XLS.MIME_TYPE); hresp.setContentType(XLS.MIME_TYPE);
@ -40,4 +41,9 @@ public class XLSView extends AbstractView {
xls.write(hresp.getOutputStream()); xls.write(hresp.getOutputStream());
} }
public static String getFilename(Map<String, Object> model) {
XLS xls = (XLS)model.get("xls");
return xls != null ? xls.getFilename() : null;
}
} }
Loading…
Cancel
Save