DownloadView 이동
parent
71831c1023
commit
5dcaea783e
@ -0,0 +1,52 @@
|
||||
package cokr.xit.foundation.web;
|
||||
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.web.servlet.view.AbstractView;
|
||||
|
||||
import cokr.xit.foundation.Assert;
|
||||
import cokr.xit.foundation.Downloadable;
|
||||
|
||||
/**파일의 다운로드를 처리하기 위한 {@link org.springframework.web.servlet.View}
|
||||
* <p>DownloadView가 파일을 다운로드하기 위해서는 {@link org.springframework.web.servlet.ModelAndView}로 다음과 같은 속성의 값을 지정해야 한다.
|
||||
* <ul><li>"download" - {@link Downloadable}</li>
|
||||
* </ul>
|
||||
* @author mjkhan
|
||||
*/
|
||||
public class DownloadView extends AbstractView {
|
||||
public static String getFilename(Map<String, Object> model) {
|
||||
Object obj = model.get("download");
|
||||
if (obj instanceof Downloadable) {
|
||||
Downloadable downloadable = (Downloadable)obj;
|
||||
return downloadable.getFilename();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest hreq, HttpServletResponse hresp) throws Exception {
|
||||
Downloadable downloadable = (Downloadable)model.get("download");
|
||||
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());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue