一、Struts文件上传
1. JSP页面(必须采用POST方式提交,并且必须设置enctype)
<html:form action="/agent" method="POST" enctype="multipart/form-data">
<%--
1、设置不允许右键粘贴和键盘输入,必须选择
2、这里的name属性和ActionForm中的属性名称要对应
--%>
<input type="file" name="closeFile" id="closeFile" size="60" onpaste="return false;" onkeydown="return false;">
</html:form>
2. ActionForm
定义和页面对应的FormFile:private FormFile closeFile = null;
3. Action
通过ActionForm获取FormFile,再通过FormFile对象获取文件名称、大小、输入流等信息,
通过这些信息完成文件格式、大小的校验,及文件内容的保存操作。
二、Struts文件下载
1. JSP页面构造查询条件
2. Action
根据查询条件查询数据(从数据库),将查询数据内容输出到文件,并保存好文件目录及文件名称(页面需要用到)。
3. JSP页面(提供文件下载链接)
<a href="javascript:void(0)" onclick="location.replace('<app:ContextPath/>/download.do?method=downLoadFile&filePath=${filePath}&fileName=${fileName}');"><c:out value="${fileName}"></c:out></a>
4. 文件下载Action处理类
/**
* 文件下载
*
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
* @throws Exception
*/
public ActionForward downLoadFile(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
{
DownLoadForm downloadForm = (DownLoadForm) form;
String filePath = downloadForm.getFilePath();
String fileName = downloadForm.getFileName();
if (null != filePath
&& false == filePath.endsWith("/"))
{
filePath = filePath + "/";
}
try
{
fileName = new String(fileName.getBytes("ISO-8859-1"), "UTF-8");
} catch (Exception e)
{
e.printStackTrace();
}
BufferedWriter out = null;
BufferedReader reader = null;
try
{
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition","attachment;filename="
+ java.net.URLEncoder.encode(fileName, "UTF8"));
out = new BufferedWriter(response.getWriter());
reader = new BufferedReader(new InputStreamReader(
new FileInputStream(filePath + fileName), "GBK"));
String line = null;
while (null != (line = reader.readLine()))
{
out.write(line);
out.write("\r\n");
}
out.flush();
} catch (Exception e)
{
} finally
{
if (null != out) {
try {
out.close();
} catch (IOException e) {
}
}
if (null != reader) {
try {
reader.close();
} catch (IOException e) {
}
}
}
// 记录成功日志
log.info("DownLoadAction downLoadFile successful!");
return null;
}
**************************************************************************************
可能有人会提出疑问:JSP页面下载文件不就设置下载链接地址就ok了吗?为什么还要写Action处理类?
例如JSP页面:<a href="<app:ContextPath/>/download/${fileName}" target="_blank"><c:out value="${fileName}"></c:out></a>
★之所以多写一个Action处理类的原因是:如果浏览器能够识别已下载文件的扩展名,则浏览器就会激活该扩展名所关联的程序来打开所下载的文件。比如:在Windows平台上,如果用户点击的链接链接的是一个“.doc”或者“.txt”文件的话,那么,浏览器就会启动Microsoft Word或者记事本应用程序来打开它。
附录:
Web文件的ContentType类型大全
".*"="application/octet-stream"
".001"="application/x-001"
".301"="application/x-301"
".323"="text/h323"
".906"="application/x-906"
".907"="drawing/907"
".a11"="application/x-a11"
".acp"="audio/x-mei-aac"
".ai"="application/postscript"
".aif"="audio/aiff"
".aifc"="audio/aiff"
".aiff"="audio/aiff"
".anv"="application/x-anv"
".asa"="text/asa"
".asf"="video/x-ms-asf"
".asp"="text/asp"
".asx"="video/x-ms-asf"
".au"="audio/basic"
".avi"="video/avi"
".awf"="application/vnd.adobe.workflow"
".biz"="text/xml"
".bmp"="application/x-bmp"
".bot"="application/x-bot"
".c4t"="application/x-c4t"
".c90"="application/x-c90"
".cal"="application/x-cals"
".cat"="application/vnd.ms-pki.seccat"
".cdf"="application/x-netcdf"
".cdr"="application/x-cdr"
".cel"="application/x-cel"
".cer"="application/x-x509-ca-cert"
".cg4"="application/x-g4"
".cgm"="application/x-cgm"
".cit"="application/x-cit"
".class"="java/*"
".cml"="text/xml"
".cmp"="application/x-cmp"
".cmx"="application/x-cmx"
".cot"="application/x-cot"
".crl"="application/pkix-crl"
".crt"="application/x-x509-ca-cert"
".csi"="application/x-csi"
".css"="text/css"
".cut"="application/x-cut"
".dbf"="application/x-dbf"
".dbm"="application/x-dbm"
".dbx"="application/x-dbx"
".dcd"="text/xml"
".dcx"="application/x-dcx"
".der"="application/x-x509-ca-cert"
".dgn"="application/x-dgn"
".dib"="application/x-dib"
".dll"="application/x-msdownload"
".doc"="application/msword"
".dot"="application/msword"
".drw"="application/x-drw"
".dtd"="text/xml"
".dwf"="Model/vnd.dwf"
".dwf"="application/x-dwf"
".dwg"="application/x-dwg"
".dxb"="application/x-dxb"
".dxf"="application/x-dxf"
".edn"="application/vnd.adobe.edn"
".emf"="application/x-emf"
".eml"="message/rfc822"
".ent"="text/xml"
".epi"="application/x-epi"
".eps"="application/x-ps"
".eps"="application/postscript"
".etd"="application/x-ebx"
".exe"="application/x-msdownload"
".fax"="image/fax"
".fdf"="application/vnd.fdf"
".fif"="application/fractals"
".fo"="text/xml"
".frm"="application/x-frm"
".g4"="application/x-g4"
".gbr"="application/x-gbr"
".gcd"="application/x-gcd"
".gif"="image/gif"
".gl2"="application/x-gl2"
".gp4"="application/x-gp4"
".hgl"="application/x-hgl"
".hmr"="application/x-hmr"
".hpg"="application/x-hpgl"
".hpl"="application/x-hpl"
".hqx"="application/mac-binhex40"
".hrf"="application/x-hrf"
".hta"="application/hta"
".htc"="text/x-component"
".htm"="text/html"
".html"="text/html"
".htt"="text/webviewhtml"
".htx"="text/html"
".icb"="application/x-icb"
".ico"="image/x-icon"
".ico"="application/x-ico"
".iff"="application/x-iff"
".ig4"="application/x-g4"
".igs"="application/x-igs"
".iii"="application/x-iphone"
".img"="application/x-img"
".ins"="application/x-internet-signup"
".isp"="application/x-internet-signup"
".IVF"="video/x-ivf"
".java"="java/*"
".jfif"="image/jpeg"font-size: 10.
分享到:
相关推荐
struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载
struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传
在struts2基础上实现的包括单文件、多文件的上传及下载
Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码
struts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileupload
http://blog.csdn.net/johnjobs/article/details/8076832博文中附件的下载链接
struts2文件上传实例,程序员宝典......
里面包括的是实现struts2文件上传所需要的全部jar包
JavaEE Struts文件上传
struts 文件上传 Struts实现文件上传
Struts 2是通过Commons FileUpload文件上传。Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器将文件绑定到Action的实例中。从而我们就能够以本地文件方式的操作浏览器上传的...
struts2文件上传的两种方法,单文件上传与多文件上传,很经典的,是我总结的两种方法,要看struts2文件上传,大家一定要下载看看啊
struts文件上传.pdf 文件上传struts文件上传.pdf 文件上传struts文件上传.pdf 文件上传struts文件上传.pdf 文件上传
struts2文件上传极其简单struts2文件上传极其简单struts2文件上传极其简单struts2文件上传极其简单struts2文件上传极其简单
struts2文件上传与下载,eclipse与myeclipse导入即可使用的代码,简单易懂,希望对你有帮助,喜欢的给个五星评价,谢谢!!!
struts2文件上传与下载, <s:fielderror></s:fielderror> 文件"></s:file> <s:submit id="submit" value="sbumit"></s:submit> </s:form>
struts2 fileupload 文件上传
基于Struts2实现头像或文件上传,基于Struts2实现头像或文件上传,基于Struts2实现头像或文件上传