JSP download methods: 1. Use the struts download class downloadaction 2. Direct streaming
Source: Internet
Author: User
Several ways to download jsp: 1. Use download action of struts DownloadAction 2. Direct streaming
1. The first one is easy to download using struts. Need to inherit DownloadAction. Then override the getStreamInfo method. Just put the InputStream stream into the inner class.
package cn.finefuture.common.faxserver.struts; <br />
import java.io.IOException; <br />
import java.io.InputStream; <br />
import javax.servlet.http.HttpServletRequest; <br />
import javax.servlet.http.HttpServletResponse; <br />
import org.apache.struts.action.ActionForm; <br />
import org.apache.struts.action.ActionForward; <br />
import org.apache.struts.action.ActionMapping; <br />
import org.apache.struts.actions.DownloadAction; <br />
import cn.finefuture.common.faxserver.service.FaxServerService; <br />
import cn.finefuture.common.faxserver.service.impl.FaxServerServiceImp; <br />
public class FaxDownAction extends DownloadAction {<br />
private FaxServerService faxServerService = new FaxServerServiceImp (); <br />
public void setFaxServerService (FaxServerService faxServerService) {<br />
this.faxServerService = faxServerService; <br />
} <br />
@Override <br />
protected StreamInfo getStreamInfo (ActionMapping mapping, ActionForm form, <br />
HttpServletRequest request, HttpServletResponse response) <br />
throws Exception {<br />
// Fax number <br />
String faxId = request.getParameter ("faxId"); <br />
InputStream inStream = null; <br />
if (faxId! = null &&! (faxId.equals (""))) {<br />
// Get InputStream <br />
inStream = this.faxServerService.getFaxInAtt (faxId); <br />
} <br />
final InputStream in = inStream; <br />
final String contentType = "application / file"; <br />
// Build <mce: script type = "text / javascript" src = "http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js" mce_src = "http: //hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js"></mce:script><mce:script type = "text / javascript" src = "http: / /hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js "mce_src =" http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/ langs / en.js "> </ mce: script> It is recommended to set the content-disposition response information header, otherwise the web browser will download the file <br />
// Cannot display correct file name in save file dialog <br />
response.setHeader ("content-disposition", "attachment; filename =" <br />
+ faxId); <br />
return new DownloadAction.StreamInfo () // Implemented the StreamInfo interface using an implicit method <br />
{<br />
public String getContentType () {<br />
return contentType; <br />
} <br />
public InputStream getInputStream () throws IOException {<br />
return in; <br />
} <br />
}; <br />
} <br />
// If the Struts action does not add the file request parameter, the list of files in the specified directory is output to the client through the execute method <br />
public ActionForward execute (ActionMapping mapping, ActionForm form, <br />
HttpServletRequest request, HttpServletResponse response) <br />
throws Exception {<br />
// When the file parameter exists, call the execute method in DownloadAction <br />
// In fact, the execute method in the DownloadAction class calls the getStreamInfo method <br />
// This statement is equivalent to calling the getStreamInfo method <br />
return super.execute (mapping, form, request, response); <br />
} <br />
} <br />
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.