文件的下载

发表时间:2017-05-16 15:45:23 浏览量( 21 ) 留言数( 0 )

学习目标:

1、了解Struts2的下载操作


学习过程:

一、使用超链接实现文件的下载

使用超链接指向文件,就可以实现下载,这是实现下载最简单的方法,我们在项目的WebRoot目录下建立一个doc目录,并负责一个abc.rar的文件,然后在jsp页面中建立一个超链接指向这个文件,就可以实现文件的下载,代码如下:

<a href="doc/abc.rar">下载</a>

这种方式比较简单,但是也有不好的地方,比如文件是动态生成,又或者需要下载权限,记录下载次数等等,都不好控制。所以我们可以使用action实现文件的下载。

二、使用action实现文件的下载

1、实现action代码如下:

private InputStream mydoc;
public InputStream getMydoc() {
	// 下载
	return ServletActionContext.getServletContext().getResourceAsStream("doc/abc.txt");
}

2、配置action

<action name="down" class="com.action.UserAction">
    <result type="stream">
         <!-- 对应页面的输入流的属性 -->
        <param name="inputName">mydoc</param>
        <!-- 以附件形式下载  filename=下载名称 -->
        <param name="contentDisposition">attachment;filename="abc.rar"</param>
        <param name="bufferSize">1024</param>
    </result>
</action>

3、如果下载文件名称是动态赋值的,那么我们也可以定义一个字符串属性,并赋予get/set方法,然后修改配置文件。

	private String downName;//下载文件的名称
	public String getDownName() {
		return downName;
	}

	public void setDownName(String downName) {
		this.downName = downName;
	}

如果downName是中文,我们还得处理乱码的问题,修改action如下:

public InputStream getMydoc() {
		try {
			downName=new String("中文.txt".getBytes(),"ISO-8859-1");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// 下载
		return ServletActionContext.getServletContext().getResourceAsStream(
				"doc/abc.txt");
	}

修改配置文件如下:

<action name="down" class="com.action.UserAction">
	<result type="stream">
		<!-- 对应页面的输入流的属性 -->		 
		<param name="inputName">mydoc</param>
		 <!-- 以附件形式下载  filename=下载名称 -->
		<param name="contentDisposition">attachment;filename="${downName}"</param>
		<param name="bufferSize">1024</param>
	</result>
</action>

上传和下载完整代码下载:

strutsfile.rar

附:contentType类型如下

附:contentType类型.

'ez' => 'application/andrew-inset', 

'hqx' => 'application/mac-binhex40', 

'cpt' => 'application/mac-compactpro', 

'doc' => 'application/msword', 

'bin' => 'application/octet-stream', 

'dms' => 'application/octet-stream', 

'lha' => 'application/octet-stream', 

'lzh' => 'application/octet-stream', 

'exe' => 'application/octet-stream', 

'class' => 'application/octet-stream', 

'so' => 'application/octet-stream', 

'dll' => 'application/octet-stream', 

'oda' => 'application/oda', 

'pdf' => 'application/pdf', 

'ai' => 'application/postscript', 

'eps' => 'application/postscript', 

'ps' => 'application/postscript', 

'smi' => 'application/smil', 

'smil' => 'application/smil', 

'mif' => 'application/vnd.mif', 

'xls' => 'application/vnd.ms-excel', 

'ppt' => 'application/vnd.ms-powerpoint', 

'wbxml' => 'application/vnd.wap.wbxml', 

'wmlc' => 'application/vnd.wap.wmlc', 

'wmlsc' => 'application/vnd.wap.wmlscriptc', 

'bcpio' => 'application/x-bcpio', 

'vcd' => 'application/x-cdlink', 

'pgn' => 'application/x-chess-pgn', 

'cpio' => 'application/x-cpio', 

'csh' => 'application/x-csh', 

'dcr' => 'application/x-director', 

'dir' => 'application/x-director', 

'dxr' => 'application/x-director', 

'dvi' => 'application/x-dvi', 

'spl' => 'application/x-futuresplash', 

'gtar' => 'application/x-gtar', 

'hdf' => 'application/x-hdf', 

'js' => 'application/x-javascript', 

'skp' => 'application/x-koan', 

'skd' => 'application/x-koan', 

'skt' => 'application/x-koan', 

'skm' => 'application/x-koan', 

'latex' => 'application/x-latex', 

'nc' => 'application/x-netcdf', 

'cdf' => 'application/x-netcdf', 

'sh' => 'application/x-sh', 

'shar' => 'application/x-shar', 

'swf' => 'application/x-shockwave-flash', 

'sit' => 'application/x-stuffit', 

'sv4cpio' => 'application/x-sv4cpio', 

'sv4crc' => 'application/x-sv4crc', 

'tar' => 'application/x-tar', 

'tcl' => 'application/x-tcl', 

'tex' => 'application/x-tex', 

'texinfo' => 'application/x-texinfo', 

'texi' => 'application/x-texinfo', 

't' => 'application/x-troff', 

'tr' => 'application/x-troff', 

'roff' => 'application/x-troff', 

'man' => 'application/x-troff-man', 

'me' => 'application/x-troff-me', 

'ms' => 'application/x-troff-ms', 

'ustar' => 'application/x-ustar', 

'src' => 'application/x-wais-source', 

'xhtml' => 'application/xhtml+xml', 

'xht' => 'application/xhtml+xml', 

'zip' => 'application/zip', 

'au' => 'audio/basic', 

'snd' => 'audio/basic', 

'mid' => 'audio/midi', 

'midi' => 'audio/midi', 

'kar' => 'audio/midi', 

'mpga' => 'audio/mpeg', 

'mp2' => 'audio/mpeg', 

'mp3' => 'audio/mpeg', 

'aif' => 'audio/x-aiff', 

'aiff' => 'audio/x-aiff', 

'aifc' => 'audio/x-aiff', 

'm3u' => 'audio/x-mpegurl', 

'ram' => 'audio/x-pn-realaudio', 

'rm' => 'audio/x-pn-realaudio', 

'rpm' => 'audio/x-pn-realaudio-plugin', 

'ra' => 'audio/x-realaudio', 

'wav' => 'audio/x-wav', 

'pdb' => 'chemical/x-pdb', 

'xyz' => 'chemical/x-xyz', 

'bmp' => 'image/bmp', 

'gif' => 'image/gif', 

'ief' => 'image/ief', 

'jpeg' => 'image/jpeg', 

'jpg' => 'image/jpeg', 

'jpe' => 'image/jpeg', 

'png' => 'image/png', 

'tiff' => 'image/tiff', 

'tif' => 'image/tiff', 

'djvu' => 'image/vnd.djvu', 

'djv' => 'image/vnd.djvu', 

'wbmp' => 'image/vnd.wap.wbmp', 

'ras' => 'image/x-cmu-raster', 

'pnm' => 'image/x-portable-anymap', 

'pbm' => 'image/x-portable-bitmap', 

'pgm' => 'image/x-portable-graymap', 

'ppm' => 'image/x-portable-pixmap', 

'rgb' => 'image/x-rgb', 

'xbm' => 'image/x-xbitmap', 

'xpm' => 'image/x-xpixmap', 

'xwd' => 'image/x-xwindowdump', 

'igs' => 'model/iges', 

'iges' => 'model/iges', 

'msh' => 'model/mesh', 

'mesh' => 'model/mesh', 

'silo' => 'model/mesh', 

'wrl' => 'model/vrml', 

'vrml' => 'model/vrml', 

'css' => 'text/css', 

'html' => 'text/html', 

'htm' => 'text/html', 

'asc' => 'text/plain', 

'txt' => 'text/plain', 

'rtx' => 'text/richtext', 

'rtf' => 'text/rtf', 

'sgml' => 'text/sgml', 

'sgm' => 'text/sgml', 

'tsv' => 'text/tab-separated-values', 

'wml' => 'text/vnd.wap.wml', 

'wmls' => 'text/vnd.wap.wmlscript', 

'etx' => 'text/x-setext', 

'xsl' => 'text/xml', 

'xml' => 'text/xml', 

'mpeg' => 'video/mpeg', 

'mpg' => 'video/mpeg', 

'mpe' => 'video/mpeg', 

'qt' => 'video/quicktime', 

'mov' => 'video/quicktime', 

'mxu' => 'video/vnd.mpegurl', 

'avi' => 'video/x-msvideo', 

'movie' => 'video/x-sgi-movie', 

'ice' => 'x-conference/x-cooltalk'