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 />

2. The second kind of self-write output stream

/ ** <br />
* Download attachments <br />
* <br />
* @param mapping <br />
* @param form <br />
* @param request <br />
* @param response <br />
* @return <br />
* / <br />
public ActionForward doDownLoadAttachment (ActionMapping mapping, <br />
ActionForm form, HttpServletRequest request, <br />
HttpServletResponse response) {<br />
// Fax number <br />
String faxId = request.getParameter ("faxId"); <br />
InputStream inStream = null; <br />
if (faxId! = null &&! (faxId.equals (""))) {<br />
inStream = this.faxServerService.getFaxInAtt (faxId); <br />
} <br />
// stream is not equal to null <br />
if (inStream! = null) {<br />
try {<br />
// Format the output <br />
response.reset (); <br />
response.setContentType ("bin"); <br />
// Transcode Chinese characters <br />
faxId = new String (faxId.getBytes ("GBK"), "iso8859-1"); <br />
// Set the file name <br />
response.setHeader ("Content-Disposition", <br />
"attachment; filename =" + faxId); <br />
// Cyclically fetch the data in the stream <br />
byte [] b = new byte [1024]; <br />
int len; <br />
while ((len = inStream.read (b))> 0) {<br />
response.getOutputStream (). write (b, 0, len); <br />
} <br />
} catch (IOException e) {<br />
e.printStackTrace (); <br />
} finally {<br />
try {<br />
inStream.close (); <br />
} catch (IOException e) {<br />
e.printStackTrace (); <br />
} <br />
} <br />
} <br />
return null; <br />
}
Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.