`
lxq_xsyu
  • 浏览: 66524 次
  • 性别: Icon_minigender_1
  • 来自: 西安
文章分类
社区版块
存档分类
最新评论

Struts2——(8)struts2中文件的上传

 
阅读更多

通过2种方式模拟单个文件上传,效果如下所示

开发步骤如下:

1、新建一个web工程,导入struts2上传文件所需jar,如下图

目录结构

2、新建Action

第一种方式

复制代码
package com.ljq.action; import java.io.File; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings("serial") public class UploadAction extends ActionSupport{ private File image; //上传的文件 private String imageFileName; //文件名称 private String imageContentType; //文件类型 public String execute() throws Exception { String realpath = ServletActionContext.getServletContext().getRealPath("/images"); //D:\apache-tomcat-6.0.18\webapps\struts2_upload\images System.out.println("realpath: "+realpath); if (image != null) { File savefile = new File(new File(realpath), imageFileName); if (!savefile.getParentFile().exists()) savefile.getParentFile().mkdirs(); FileUtils.copyFile(image, savefile); ActionContext.getContext().put("message", "文件上传成功"); } return "success"; } public File getImage() { return image; } public void setImage(File image) { this.image = image; } public String getImageFileName() { return imageFileName; } public void setImageFileName(String imageFileName) { this.imageFileName = imageFileName; } public String getImageContentType() { return imageContentType; } public void setImageContentType(String imageContentType) { this.imageContentType = imageContentType; } }
复制代码

第二种方式

复制代码
package com.ljq.action; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings("serial") public class UploadAction2 extends ActionSupport { // 封装上传文件域的属性 private File image; // 封装上传文件类型的属性 private String imageContentType; // 封装上传文件名的属性 private String imageFileName; // 接受依赖注入的属性 private String savePath; @Override public String execute() { FileOutputStream fos = null; FileInputStream fis = null; try { // 建立文件输出流 System.out.println(getSavePath()); fos = new FileOutputStream(getSavePath() + "\\" + getImageFileName()); // 建立文件上传流 fis = new FileInputStream(getImage()); byte[] buffer = new byte[1024]; int len = 0; while ((len = fis.read(buffer)) > 0) { fos.write(buffer, 0, len); } } catch (Exception e) { System.out.println("文件上传失败"); e.printStackTrace(); } finally { close(fos, fis); } return SUCCESS; } /** * 返回上传文件的保存位置 * * @return */ public String getSavePath() throws Exception{ return ServletActionContext.getServletContext().getRealPath(savePath); } public void setSavePath(String savePath) { this.savePath = savePath; } public File getImage() { return image; } public void setImage(File image) { this.image = image; } public String getImageContentType() { return imageContentType; } public void setImageContentType(String imageContentType) { this.imageContentType = imageContentType; } public String getImageFileName() { return imageFileName; } public void setImageFileName(String imageFileName) { this.imageFileName = imageFileName; } private void close(FileOutputStream fos, FileInputStream fis) { if (fis != null) { try { fis.close(); } catch (IOException e) { System.out.println("FileInputStream关闭失败"); e.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (IOException e) { System.out.println("FileOutputStream关闭失败"); e.printStackTrace(); } } } }
复制代码

struts.xml配置文件

复制代码
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 该属性指定需要Struts2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。 如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 --> <constant name="struts.action.extension" value="do" /> <!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 --> <constant name="struts.serve.static.browserCache" value="false" /> <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 --> <constant name="struts.configuration.xml.reload" value="true" /> <!-- 开发模式下使用,这样可以打印出更详细的错误信息 --> <constant name="struts.devMode" value="true" /> <!-- 默认的视图主题 --> <constant name="struts.ui.theme" value="simple" /> <!--<constant name="struts.objectFactory" value="spring" />--> <!--解决乱码 --> <constant name="struts.i18n.encoding" value="UTF-8" /> <!-- 指定允许上传的文件最大字节数。默认值是2097152(2M) --> <constant name="struts.multipart.maxSize" value="10701096"/> <!-- 设置上传文件的临时文件夹,默认使用javax.servlet.context.tempdir --> <constant name="struts.multipart.saveDir " value="d:/tmp" /> <package name="upload" namespace="/upload" extends="struts-default"> <action name="*_upload" class="com.ljq.action.UploadAction" method="{1}"> <result name="success">/WEB-INF/page/message.jsp</result> </action> </package> <package name="upload2" extends="struts-default"> <action name="upload2" class="com.ljq.action.UploadAction2" method="execute"> <!-- 动态设置savePath的属性值 --> <param name="savePath">/images</param> <result name="success">/WEB-INF/page/message.jsp</result> <result name="input">/upload/upload.jsp</result> <interceptor-ref name="fileUpload"> <!-- 文件过滤 --> <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param> <!-- 文件大小, 以字节为单位 --> <param name="maximumSize">1025956</param> </interceptor-ref> <!-- 默认拦截器必须放在fileUpload之后,否则无效 --> <interceptor-ref name="defaultStack" /> </action> </package> </struts>
复制代码

上传表单页面

复制代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>文件上传</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <!-- ${pageContext.request.contextPath}/upload/execute_upload.do --> <!-- ${pageContext.request.contextPath}/upload2/upload2.do --> <form action="${pageContext.request.contextPath}/upload2/upload2.do" enctype="multipart/form-data" method="post"> 文件:<input type="file" name="image"> <input type="submit" value="上传" /> </form> <br/> <s:fielderror /> </body> </html>
复制代码

显示结果页面

复制代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>上传成功</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> 上传成功! <br/><br/> <!-- ${pageContext.request.contextPath} tomcat部署路径, 如:D:\apache-tomcat-6.0.18\webapps\struts2_upload\ --> <img src="${pageContext.request.contextPath}/<s:property value="'images/'+imageFileName"/>"> <s:debug></s:debug> </body> </html>
复制代码
分享到:
评论

相关推荐

    myeclipse+struts实例——3

    myeclipse+struts+tomcat 文件上传,可以实现多文件上传

    struts2.0中文教程

    01 为Struts 2.0做好准备 02 常用的Struts 2.0的标志(Tag) 03 Struts 2.0的Action讲解...09 在Struts 2中实现文件上传 10 在Struts 2中实现CRUD 11 Struts 2中的OGNL 12 trus 2的新表单标志的使用 13 Struts 2与AJAX

    个人认为目前最完备的Struts2教程

    01 为Struts 2.0做好准备 02 常用的Struts 2.0的标志(Tag) 03 Struts 2.0的Action...09 在Struts 2中实现文件上传 10 在Struts 2中实现CRUD 11 Struts 2中的OGNL 12 Struts 2的新表单标志的使用 13 Struts 2与AJAX

    Struts2自学笔记——Struts2的文件上传

    NULL 博文链接:https://changluo.iteye.com/blog/1842213

    Struts2.0中文教程权威版

    09 在Struts 2中实现文件上传 10 在Struts 2中实现CRUD 11 Struts 2中的OGNL 12 trus 2的新表单标志的使用 13 Struts 2与AJAX Struts2中用Spring实现IoC.doc Struts2中的零配置与CoC(Convention over Configration...

    Struts 2.0系列(MAX)

    Struts 2.0系列(MAX),pdf格式,全方位介绍struts2: 常用的Struts 2.0的标志(Tag)介绍 ...在Struts 2中实现文件上传 在Struts 2中实现CRUD Struts 2中的OGNL Strus 2的新表单标志的使用 Struts 2与AJAX

    《Struts2权威指南——基于WebWork核心的MVC开发》源代码 part06

    《Struts2权威指南——基于WebWork核心的MVC开发》的源代码。请完全放心,绝对保证质量。由于源码中包含jar文件,打出的包过大,只能分多次上传,请谅解。原文件大于415M,压缩后377M,分压成16个包。此为第68个包。

    Struts2 in action中文版

    3.5 案例研究:文件上传 56 3.5.1 通过struts-default包获得内建的支持 56 3.5.2 fileUpload拦截器做什么 57 3.5.3 Struts 2公文包示例代码研究 58 3.6 小结 60 第4章 使用拦截器追加工作流 61 4.1 为什么要拦截...

    Struts2.1权威指南——基于WebWork核心的MV...part2

    由于文件比较大,因受上传文件大小的现在,共分成5个部分,要下载全部的五个压缩包才能解压,5个只要一分,不贵吧,哈哈。 本书是《Struts 2权威指南》的第二版,本书介绍的Struts 2是Struts 2.1。本书第二版保留了...

    《Struts2权威指南——基于WebWork核心的MVC开发》源代码 part08

    《Struts2权威指南——基于WebWork核心的MVC开发》的源代码。请完全放心,绝对保证质量。由于源码中包含jar文件,打出的包过大,只能分多次上传,请谅解。原文件大于415M,压缩后377M,分压成16个包。此为第8个包。

    《Struts2权威指南——基于WebWork核心的MVC开发》源代码 part02

    《Struts2权威指南——基于WebWork核心的MVC开发》的源代码。请完全放心,绝对保证质量。由于源码中包含jar文件,打出的包过大,只能分多次上传,请谅解。原文件大于415M,压缩后377M,分压成16个包。此为第2个包。

    《Struts2权威指南——基于WebWork核心的MVC开发》源代码 part13

    《Struts2权威指南——基于WebWork核心的MVC开发》的源代码。请完全放心,绝对保证质量。由于源码中包含jar文件,打出的包过大,只能分多次上传,请谅解。原文件大于415M,压缩后377M,分压成16个包。此为第13个包。

    《Struts2权威指南——基于WebWork核心的MVC开发》源代码 part11

    《Struts2权威指南——基于WebWork核心的MVC开发》的源代码。请完全放心,绝对保证质量。由于源码中包含jar文件,打出的包过大,只能分多次上传,请谅解。原文件大于415M,压缩后377M,分压成16个包。此为第11个包。

    《Struts2权威指南——基于WebWork核心的MVC开发》源代码 part01

    《Struts2权威指南——基于WebWork核心的MVC开发》的源代码。请完全放心,绝对保证质量。由于源码中包含jar文件,打出的包过大,只能分多次上传,请谅解。原文件大于415M,压缩后377M,分压成16个包。此为第1个包。

    《Struts2权威指南——基于WebWork核心的MVC开发》源代码 part10

    《Struts2权威指南——基于WebWork核心的MVC开发》的源代码。请完全放心,绝对保证质量。由于源码中包含jar文件,打出的包过大,只能分多次上传,请谅解。原文件大于415M,压缩后377M,分压成16个包。此为第10个包。

    《Struts2权威指南——基于WebWork核心的MVC开发》源代码 part12

    《Struts2权威指南——基于WebWork核心的MVC开发》的源代码。请完全放心,绝对保证质量。由于源码中包含jar文件,打出的包过大,只能分多次上传,请谅解。原文件大于415M,压缩后377M,分压成16个包。此为第12个包。

    《Struts2权威指南——基于WebWork核心的MVC开发》源代码 part04

    《Struts2权威指南——基于WebWork核心的MVC开发》的源代码。请完全放心,绝对保证质量。由于源码中包含jar文件,打出的包过大,只能分多次上传,请谅解。原文件大于415M,压缩后377M,分压成16个包。此为第4个包。

    《Struts2权威指南——基于WebWork核心的MVC开发》源代码 part15

    《Struts2权威指南——基于WebWork核心的MVC开发》的源代码。请完全放心,绝对保证质量。由于源码中包含jar文件,打出的包过大,只能分多次上传,请谅解。原文件大于415M,压缩后377M,分压成16个包。此为第15个包。

Global site tag (gtag.js) - Google Analytics