Downloadable 추가, download(...) 수정

master
mjkhan21 7 months ago
parent 5b16aead29
commit 0fb1d99aa6

@ -0,0 +1,157 @@
package cokr.xit.foundation;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLConnection;
import java.util.function.Consumer;
/**
* @author mjkhan
*/
public class Downloadable {
/** Downloadable .
* @param file
* @return Downloadable
*/
public static Downloadable create(File file) {
String filename = file.getName();
return new Downloadable()
.setFilename(filename)
.setContentType(URLConnection.guessContentTypeFromName(filename))
.setLength(file.length())
.setWriter(out -> {
try (FileInputStream input = new FileInputStream(file)) {
input.transferTo(out);
} catch (Exception e) {
Assert.runtimeException(e);
}
});
}
/** InputStream Downloadable .
* Downloadable .
* @param inputStream InputStream
* @return Downloadable
*/
public static Downloadable create(InputStream inputStream) {
try {
return new Downloadable()
.setContentType(URLConnection.guessContentTypeFromStream(inputStream))
.setWriter(out -> {
try (InputStream input = inputStream) {
input.transferTo(out);
} catch (Exception e) {
throw Assert.runtimeException(e);
}
});
} catch (Exception e) {
throw Assert.runtimeException(e);
}
}
private String
charset,
filename,
contentType,
disposition;
private Long length;
private Consumer<OutputStream> writer;
/** . UTF-8
* @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() {
return Assert.notEmpty(filename, "filename");
}
/** .
* @param filename
* @return Downloadable
*/
public Downloadable setFilename(String filename) {
this.filename = filename;
return this;
}
/**contentType . application/octet-stream
* @return contentType
*/
public String getContentType() {
return Assert.ifEmpty(contentType, "application/octet-stream");
}
/**contentType .
* @param contentType contentType
* @return Downloadable
*/
public Downloadable setContentType(String contentType) {
this.contentType = contentType;
return this;
}
/**disposition . attachment
* @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 Long getLength() {
return length;
}
/** .
* @param length
* @return Downloadable
*/
public Downloadable setLength(Long length) {
this.length = length;
return this;
}
/** .
* @param writer
* @return Downloadable
*/
public Downloadable setWriter(Consumer<OutputStream> writer) {
this.writer = writer;
return this;
}
/** .
* @param out OutputStream
*/
public void write(OutputStream out) {
Assert.notEmpty(writer, "writer").accept(out);
}
}

@ -1,7 +1,5 @@
package cokr.xit.foundation.web; package cokr.xit.foundation.web;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream; import java.io.InputStream;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.Collection; import java.util.Collection;
@ -10,7 +8,6 @@ import java.util.Map;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
@ -18,6 +15,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import cokr.xit.foundation.AbstractComponent; import cokr.xit.foundation.AbstractComponent;
import cokr.xit.foundation.Access; import cokr.xit.foundation.Access;
import cokr.xit.foundation.Assert;
import cokr.xit.foundation.Downloadable;
import cokr.xit.foundation.component.QueryRequest; import cokr.xit.foundation.component.QueryRequest;
import cokr.xit.foundation.data.paging.BoundedList; import cokr.xit.foundation.data.paging.BoundedList;
@ -218,44 +217,31 @@ public abstract class AbstractController extends AbstractComponent {
} }
} }
/**InputStream . /** downloadable .
* @param input InputStream * @param downloadable Downloadable
* @param contentType . application/octet-stream
* @param filename
* @param length
* @param disposition 'attachment', 'inline'. attachment
* @param hresp HttpServletResponse * @param hresp HttpServletResponse
* @throws Exception * @throws Exception
*/ */
protected void download(InputStream input, String contentType, String filename, long length, String disposition, HttpServletResponse hresp) throws Exception { protected void download(Downloadable downloadable, HttpServletResponse hresp) throws Exception {
if (input == null) { //TODO: DownloadView ->foundation으로 이동 후 render(...) 호출하도록 수정할 것
if (downloadable == null) {
hresp.setStatus(HttpServletResponse.SC_NOT_FOUND); hresp.setStatus(HttpServletResponse.SC_NOT_FOUND);
hresp.sendError(HttpServletResponse.SC_NOT_FOUND); hresp.sendError(HttpServletResponse.SC_NOT_FOUND);
return; return;
} }
hresp.setCharacterEncoding("UTF-8"); String filename = downloadable.getFilename();
hresp.setContentType(ifEmpty(contentType, "application/octet-stream")); if (Assert.isEmpty(filename))
hresp.setHeader("Content-Disposition", ifEmpty(disposition, "attachment") + "; filename=\"" + URLEncoder.encode(filename, "UTF-8") +"\""); throw new IllegalArgumentException("Unable to determine the filename");
hresp.setContentLengthLong(length);
FileCopyUtils.copy(input, hresp.getOutputStream());
}
/**file . String charset = downloadable.getCharset();
* @param file hresp.setCharacterEncoding(charset);
* @param contentType . application/octet-stream hresp.setContentType(downloadable.getContentType());
* @param filename . file . hresp.setHeader("Content-Disposition", downloadable.getDisposition() + "; filename=\"" + URLEncoder.encode(filename, charset) +"\"");
* @param disposition 'attachment', 'inline'. attachment Long length = downloadable.getLength();
* @param hresp HttpServletResponse if (length != null)
* @throws Exception hresp.setContentLengthLong(length.longValue());
*/
protected void download(File file, String contentType, String filename, String disposition, HttpServletResponse hresp) throws Exception { downloadable.write(hresp.getOutputStream());
if (file == null) {
download(null, null, null, 0, null, hresp);
return;
}
try (FileInputStream in = new FileInputStream(file)) {
download(in, contentType, ifEmpty(filename, file.getName()), file.length(), disposition, hresp);
}
} }
/** .<br /> /** .<br />

Loading…
Cancel
Save