DownloadView 이동

master
mjkhan21 7 months ago
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…
Cancel
Save