package net.sourceforge.fenixedu.presentationTier.Action; import java.io.IOException; import java.io.InputStream; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sourceforge.fenixedu.presentationTier.Action.base.FenixDispatchAction; import net.sourceforge.fenixedu.util.StreamUtils; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import pt.ist.fenixWebFramework.struts.annotations.Mapping; import pt.ist.fenixframework.plugins.fileSupport.domain.GenericFile; @Mapping(path = "/fileDownload") public class FileDownloadAction extends FenixDispatchAction { public ActionForward download(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws IOException { ServletOutputStream outputStream = null; GenericFile file = getDomainObject(request, "fileId"); InputStream stream = file.getStream(); try { outputStream = response.getOutputStream(); response.setContentType(file.getContentType()); response.setHeader("Content-disposition", "attachment; filename=\"" + file.getFilename() + "\""); int byteCount = StreamUtils.copyStream(stream, outputStream); response.setContentLength(byteCount); outputStream.flush(); } finally { outputStream.close(); stream.close(); } return null; } }