다운로드 수정

master
mjkhan21 7 months ago
parent 322655ae68
commit af9bdbc343

@ -1,145 +0,0 @@
package cokr.xit.base.file;
import java.io.File;
import java.io.InputStream;
import java.net.URLConnection;
import cokr.xit.foundation.Assert;
public class Downloadable {
private Object file;
private String
charset,
filename,
contentType,
disposition;
private Number length;
/** .
* @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");
}
/** .
* @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;
}
}

@ -10,11 +10,11 @@ import java.util.Map;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.servlet.view.AbstractView; import org.springframework.web.servlet.view.AbstractView;
import cokr.xit.base.file.FileInfo; import cokr.xit.base.file.FileInfo;
import cokr.xit.foundation.Assert; import cokr.xit.foundation.Assert;
import cokr.xit.foundation.Downloadable;
/** {@link org.springframework.web.servlet.View} /** {@link org.springframework.web.servlet.View}
* <p>DownloadView {@link org.springframework.web.servlet.ModelAndView} . * <p>DownloadView {@link org.springframework.web.servlet.ModelAndView} .
@ -30,6 +30,11 @@ import cokr.xit.foundation.Assert;
public class DownloadView extends AbstractView { public class DownloadView extends AbstractView {
@Override @Override
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 {
if (model.containsKey("download")) {
render((Downloadable)model.get("download"), hresp);
return;
}
InputStream input = getInputStream(model); InputStream input = getInputStream(model);
if (input == null) { if (input == null) {
hresp.setStatus(HttpServletResponse.SC_NOT_FOUND); hresp.setStatus(HttpServletResponse.SC_NOT_FOUND);
@ -56,7 +61,7 @@ public class DownloadView extends AbstractView {
if (length != null) if (length != null)
hresp.setContentLengthLong(length.longValue()); hresp.setContentLengthLong(length.longValue());
FileCopyUtils.copy(stream, hresp.getOutputStream()); stream.transferTo(hresp.getOutputStream());
} finally { } finally {
clear(model); clear(model);
} }
@ -148,4 +153,25 @@ public class DownloadView extends AbstractView {
if (file.exists()) if (file.exists())
file.delete(); file.delete();
} }
protected void render(Downloadable downloadable, HttpServletResponse hresp) throws Exception {
if (downloadable == null) {
hresp.setStatus(HttpServletResponse.SC_NOT_FOUND);
hresp.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
String filename = downloadable.getFilename();
if (Assert.isEmpty(filename))
throw new IllegalArgumentException("Unable to determine the filename");
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());
}
} }

@ -7,11 +7,20 @@ import java.util.List;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import cokr.xit.base.file.FileInfo; import cokr.xit.base.file.FileInfo;
import cokr.xit.foundation.Downloadable;
/** /**
* @author mjkhan * @author mjkhan
*/ */
public class FileInfoFactory extends FileInfo.Factory { public class FileInfoFactory extends FileInfo.Factory {
public static Downloadable getDownloadable(FileInfo fileInfo) {
return new Downloadable()
.setFilename(fileInfo.getName())
.setContentType(fileInfo.getMimeType())
.setLength(fileInfo.getSize())
.setWriter(fileInfo::write);
}
/** , FileInfo . /** , FileInfo .
* @param relation * @param relation
* @param multipartFiles * @param multipartFiles

Loading…
Cancel
Save