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

Loading…
Cancel
Save