/**
作者:Joe Vienneau
译者:Rifoo Technology(http://www.rifoo.com)
时间:2005-11-10
备注:转载请保留以上声明
**/
Struts 1.2.6中推出了新的DownloadAction,用来简化下载操作。
实现DownloadAction
我们需要扩展org.apache.struts.actions.DownloadAction并实现
getStreamInfo()方法。如果我们要更改默认的缓冲大小,我们也可以覆盖
getBufferSize()方法。
实现getStreamInfo() 方法
getStreamInfo() 方法返回一个StreamInfo对象- 它是DownloadAction类的内
部类,其实是个内部接口。DownloadAction为这个接口提供了两个具体的静态内
部实现类:
FileStreamInfo - 简化从磁盘系统下载文件。需要连同content type传入一个java.io.File对象到构造方法中。
ResourceStreamInfo - 简化从web应用资源下载文件。需要传入ServletContext,路径以及content type 到它的构造方法中。
在下面的例子中,我们还提供了一个以Byte array方法实现StreamInfo接口的代
码。
实现getBufferSize() 方法
DownloadAction默认返回4096byte的缓冲区我们可以覆盖这个方法来自定义用
来传输文件的缓冲区大小
范例
下面有三个例子:
使用文件
使用web应用资源
使用byte array
FileStreamInfo范例
DownloadAction使用文件的例子。这个范例从struts-config.xml的action
mapping的parameter属性来得到文件名。
import java.io.File;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DownloadAction;
public class ExampleFileDownload extends DownloadAction{
protected StreamInfo getStreamInfo(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
// Download a "pdf" file - gets the file name from the
// Action Mapping's parameter
String contentType = "application/pdf";
File file = new File(mapping.getParameter());
return new FileStreamInfo(contentType, file);
}
}
ResourceStreamInfo范例
DownloadAction使用web应用资源的范例。这个范例从struts-config.xml的
action mapping的parameter属性来得到web应用资源的路径。
import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DownloadAction; public class ExampleResourceDownload extends DownloadAction { protected StreamInfo getStreamInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // Download a "jpeg" file - gets the file name from the // Action Mapping's parameter String contentType = "image/jpeg"; String path = mapping.getParameter(); ServletContext application = servlet.getServletContext(); return new ResourceStreamInfo(contentType, application, path); } }
Byte Array 范例
DownloadAction使用字节数组(byte array)的范例。
这个例子创建了一个实现了StreamInfo接口的ByteArrayStreamInfo内部类。
import java.io.IOException; import java.io.InputStream; import java.io.ByteArrayInputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DownloadAction; public class ExampleByteArrayDownload extends DownloadAction { protected StreamInfo getStreamInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // Download a "pdf" file String contentType = "application/pdf"; byte[] myPdfBytes = null;// Get the bytes from somewhere return new ByteArrayStreamInfo(contentType, myPdfBytes); } protected class ByteArrayStreamInfo implements StreamInfo { protected String contentType; protected byte[] bytes; public ByteArrayStreamInfo(String contentType, byte[] bytes) { this.contentType = contentType; this.bytes = bytes; } public String getContentType() { return contentType; } public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(bytes); } } }
在WEB页面上使用DownloadAction
最大的疑惑是我么如何使用这个Action?
需要做两件事情:
和任何Struts的action一样,需要在struts-config.xml中进行配置。
在WEB页面中使用它对文件进行连接
下面是struts-config.xml配置的一个例子:
<action path="/downloadMyPdfFile" type="myPackage.ExampleFileDownload"
parameter="/foo/bar.pdf"> <action path="/downloadMyImage" type="myPackage.ExampleResourceDownload"
parameter="/images/myImage.jpeg">
那么在我们的JSP页面,可以使用类似下面的例子:
<html:img action="downloadMyImage" alt="My Image" height="400" width="400"/> <html:link action="downloadMyPdfFile">Click Here to See the PDF</html:link>
注意:我们可能要将struts配置文件中<controller>属性的nocache值设置为false。如果设置为true,可能在IE上不能成功下载文件,但是在Firefox和Safari上工作正常。
<controller contentType="text/html;charset=UTF-8" locale="true" nocache="false" />
内容部署(Content Disposition)
设置Content Disposition
DownloadAction不能处理content dispositon头部。最简单的方法是在getStreamInfo()方法中设置,比如:
public class ExampleFileDownload extends DownloadAction{ protected StreamInfo getStreamInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // File Name String fileName = mapping.getParameter(); // Set the content disposition response.setHeader("Content-disposition", "attachment; filename=" + fileName); // Download a "pdf" file - gets the file name from the // Action Mapping's parameter String contentType = "application/pdf"; File file = new File(fileName); return new FileStreamInfo(contentType, file); } }
如果需要文件名做为参数,可能需要首先把文件前面的任何路径信息先清除。
Content Disposition的值
我们可以设置content disposition来下载一个文件或者在浏览器中打开一个文件。
在浏览器中打开文件的例子写法: "inline; filename=myFile.pdf"
下载的例子写法: "attachment; filename=myFile.pdf"
显示图片的话,可以使用content disposition的"inline"选项。