`
superich2008
  • 浏览: 315117 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Struts文件上传

阅读更多

一、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

    定义和页面对应的FormFileprivate FormFile closeFile = null;

3. Action

通过ActionForm获取FormFile,再通过FormFile对象获取文件名称、大小、输入流等信息,

通过这些信息完成文件格式、大小的校验,及文件内容的保存操作。

二、Struts文件下载

1. JSP页面构造查询条件

2. Action

    根据查询条件查询数据(从数据库),将查询数据内容输出到文件,并保存好文件目录及文件名称(页面需要用到)。

3. JSP页面(提供文件下载链接)

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics