<s:form action="uploadaction" method="post" enctype="multipart/form-data" >

<s:file label="上传" theme="simple" name="upload"/>

<s:submit value="上传"/>

</s:form>

struts.xml配置文件

<action name="uploadaction" class="com.butone.struts2.taguser.UploadAction" method="upload">

<!-- 配置fileUpload的拦截器 -->

<interceptor-ref name="fileUpload">

<!-- 配置允许上传的文件类型 -->

<param name="allowedTypes">

image/bmp,image/png,image/gif,image/jpeg,image/jpg,application/msword,text/plain

</param>

<!-- 配置允许上传的文件大小 -->

<param name="maximumSize">2000000000</param>

</interceptor-ref>

<interceptor-ref name="defaultStack" />

<result>/taguser/result_fileTag.jsp</result>

<result name="input">/taguser/fileTag.jsp</result>

</action>

UploadAction.java上传处理类

public class UploadAction extends ActionSupport {

// 封装单个上传文件域的属性

private File upload;

// 封装单个上传文件类型的属性

private String uploadContentType;

// 封装单个上传文件名的属性

private String uploadFileName;

// 动态设置上传文件保存地址

private String savePath;

public String getSavePath() {

// return ServletActionContext.getRequest().getRealPath("");

String onload = "C:\report\cached\";

HttpServletRequest request = ServletActionContext.getRequest();

request.setAttribute("onload", onload);

// return ServletActionContext.getRequest().getContextPath();

return onload;

}

public void setSavePath(String savePath) {

this.savePath = savePath;

}

// 上传单个文件的文件类型的setter和getter方法

public void setUploadContentType(String uploadContentType) {

this.uploadContentType = uploadContentType;

}

public String getUploadContentType() {

return (this.uploadContentType);

}

// 上传单个文件的文件名的setter和getter方法

public void setUploadFileName(String uploadFileName) {

this.uploadFileName = uploadFileName;

}

public String getUploadFileName() {

return (this.uploadFileName);

}

public File getUpload() {

return upload;

}

public void setUpload(File upload) {

this.upload = upload;

// savePath = ServletActionContext .getRequest().getRealPath("");

}

// 上传单个文件

public String upload() {

try {

// 以服务器的文件保存地址和原文件名建立上传文件输出流

System.out.println(ServletActionContext

.getRequest().getRealPath("")

+ File.separator

+ "images"

+ File.separator

+ getUploadFileName()+"路径");

FileOutputStream fos = new FileOutputStream(ServletActionContext

.getRequest().getRealPath("")

+ File.separator

+ "images"

+ File.separator

+ getUploadFileName());

// 以上传文件建立一个文件上传流

FileInputStream fis = new FileInputStream(getUpload());

// 将上传文件的内容写入服务器

byte[] buffer = new byte[1024];

int len = 0;

while ((len = fis.read(buffer)) > 0) {

fos.write(buffer, 0, len);

}

} catch (Exception e) {

e.printStackTrace();

}

return SUCCESS;

}

}

result_file.jsp

文件路径:<s:property value="savePath" />/images/<br>

<s:property value="#request.onload" /> <br>

<img src="<s:property value='#request.onload'/>010.jpg.gif">

<!-- 根据上传文件的文件名,在页面上显示上传的图片 -->

文件为:<s:property value="uploadFileName"/><br>

上传多个文件

fileuploads.jsp

<s:fielderror></s:fielderror>

<!--  <input type="button" onclick="addComponent();" value="在上传一个" name="button" />  -->

<br />

<s:form action="uploadactions"  method="post" enctype="multipart/form-data">

<s:file name="upload" label="路径"/>

<s:file name="upload" label="路径"/>

<s:file name="upload" label="路径"/>

<s:submit value="上传"/>

</s:form>

struts.xml

<action name="uploadactions" class="com.butone.struts2.taguser.UploadActions" method="upload">

<!-- 配置fileUpload的拦截器 -->

<interceptor-ref name="fileUpload">

<!-- 配置允许上传的文件类型 -->

<param name="allowedTypes">

image/bmp,image/png,image/gif,image/jpeg,image/pjpeg,image/jpg,application/msword,text/plain

</param>

<!-- 配置允许上传的文件大小 -->

<!-- <param name="maximumSize">2000000000</param>  -->

</interceptor-ref>

<interceptor-ref name="defaultStack" />

<result>/taguser/fileuploadoutput.jsp</result>

<result name="input">/taguser/fileuploads.jsp</result>

</action>

UploadActions.java

public class UploadActions extends ActionSupport {

//封装多个上传文件域的属性

private List<File> upload = new ArrayList<File>();

// 封装多个上传文件类型的属性

private List<String> uploadContentType = new ArrayList<String>();

// 封装多个上传文件名的属性

private List<String> uploadFileName = new ArrayList<String>();

//动态设置上传文件保存地址

private String savePath;

public String getSavePath() {

System.out.println("getSavePath()!!!!!");

System.out.println(savePath+"++++++++++++++++++++++++++++++");

return savePath;

}

public void setSavePath(String savePath) {

System.out.println("setSavePath()!!!!!");

this.savePath = savePath;

// savePath = "E:\butone\struts2.2\WebRoot\images\"+getUploadFileName();

}

//上传多个文件对应文件内容的setter和getter方法

public List<File> getUpload() {

return upload;

}

public void setUpload(List<File> upload) {

System.out.println("----------------    setUpload(List<File> upload)     ----------------");

this.upload = upload;

}

//  上传多个文件的文件类型setter和getter方法

public List<String> getUploadContentType() {

return uploadContentType;

}

public void setUploadContentType(List<String> uploadContentType) {

this.uploadContentType = uploadContentType;

}

// 上传多个文件的文件名的setter和getter方法

public List<String> getUploadFileName() {

return uploadFileName;

}

public void setUploadFileName(List<String> uploadFileName) {

this.uploadFileName = uploadFileName;

}

public String upload() {

//savePath = "E:\butone\struts2.2\WebRoot\images\";

savePath = ServletActionContext.getRequest().getRealPath("");

System.out.println("upload()!!!!!");

//上传多个文件

List<File> files = getUpload();

// String ext ="";

FileOutputStream fos = null;

FileInputStream fis = null;

byte[] buffer = new byte[1024];

int len = 0;

Random rd = new Random();

System.out.println(files.size()+"               ----------------");

System.out.println(getSavePath());

for (int i = 0; i < files.size(); i++) {

try {

//以服务器的文件保存地址和当前时间组合文件名建立上传文件输出流

// ext =uploadFileName.get(i).substring(uploadFileName.get(i).lastIndexOf('.'));

/* fos = new FileOutputStream(getSavePath()+ File.separator+

* DateFormatUtil.getCurrentCustomFormatDateTime(DateFormatUtil.DATE_TIME_FORMAT_14) +

* String.valueOf(rd.nextInt(1000))+ext);

*/

System.out.println(getSavePath()+"------------------------getsavepath!!!");

fos = new FileOutputStream(getSavePath() + File.separator

+ uploadFileName.get(i));

// 以上传文件建立一个文件上传流

fis = new FileInputStream(files.get(i));

// 将上传文件的内容写入服务器

len = 0;

while ((len = fis.read(buffer)) > 0) {

fos.write(buffer, 0, len);

}

} catch (Exception e) {

e.printStackTrace();

}

}

return SUCCESS;

}

}

fileuploadoutput.jsp

<!-- 输出上传的表单里的文件标题属性 -->

文件路径:<s:property value="savePath" /><br>

<s:property value="#request.onload" /> <br>

<!-- 根据上传文件的文件名,在页面上显示上传的图片 -->

文件为:<s:property value="uploadFileName"/><br>

<s:iterator value="uploadFileName">

<img src="<s:property />" />

<!-- 就是为了打印图片名称,但好像不支持中文图片的 -->

<s:property/>

</s:iterator>

struts2 s:file标签使用及文件上传例子的更多相关文章

  1. SpringMVC学习总结(六)——SpringMVC文件上传例子(2)

    基本的SpringMVC的搭建在我的上一篇文章里已经写过了,这篇文章主要说明一下使用SpringMVC进行表单上的文件上传以及多个文件同时上传的不同方法 一.配置文件: SpringMVC 用的是 的 ...

  2. 根目录97 <input file>标签,把图片上传到服务器(跟增删改查一起实现)

    首先来个简单的html页面: enctype="multipart/form-data" encoding="multipart/form-data" acti ...

  3. input标签处理多文件上传

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...

  4. DVWA(九):File Upload 全等级文件上传

    File Upload 文件上传,通常是由于对上传文件的类型没有进行严格的过滤.限制造成的,一般思路是 通过上传木马获取服务器的webshell(通过网络端口对网站服务器某种程度上的操作权限 也叫网站 ...

  5. SpringMVC学习总结(五)——SpringMVC文件上传例子

    这是用的是SpringMVC-3.1.1.commons-fileupload-1.2.2和io-2.0.1 首先是web.xml <?xml version="1.0" e ...

  6. struts2.1.6教程九、文件上传下载(了解)

    首先建立struts2UpDownLoad项目,搭建好struts2基本的开发环境. 上传实例 步骤一:upload.jsp代码如下: <s:form action="upload&q ...

  7. input标签前台实现文件上传

    值得注意的是:当一个表单里面包含这个上传元素的时候,表单的enctype必须指定为multipart/form-data,method必须指定为post,浏览器才会认识并正确执行.但是还有一点,浏览器 ...

  8. Apache HttpComponents 文件上传例子

    /* * ==================================================================== * * Licensed to the Apache ...

  9. 11、Struts2 的文件上传和下载

    文件上传 表单准备 要想使用 HTML 表单上传一个或多个文件 须把 HTML 表单的 enctype 属性设置为 multipart/form-data 须把 HTML 表单的method 属性设置 ...

随机推荐

  1. Apache 安装配置详情

    本次文章讲解Apache的安装和基本的配置 输入PHP环境搭建的一部分 PHP完整配置信息请参考 http://www.cnblogs.com/azhe-style/p/php_new_env_bui ...

  2. sqlserver如何创建镜像图文教程(转)

    由于工作中需要做SQL的镜像异地备份,以前都没有研究过,百度了一个文章记录下,方便以后查询 转载地址:http://jingyan.baidu.com/article/d5c4b52b20843fda ...

  3. Java日志框架:SLF4J,Common-Logging,Log4J,Logback说明

    Log4j  Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台.文件.GUI组件.甚至是套接口服务 器.NT的事件记录器.UNIX Syslog守护进程等 ...

  4. ELF Format 笔记(十五)—— 符号哈希表

    ilocker:关注 Android 安全(新手) QQ: 2597294287 符号哈希表用于支援符号表的访问,能够提高符号搜索速度. 下表用于解释该哈希表的组织,但该格式并不属于 ELF 规范. ...

  5. LNMP环境magento常见错误

    一.安装报404错误 git clone 下最新代码,跳转到index/install 安装时出现404错误 需要把伪静态规则加到nginx配置文件中: # # The default server ...

  6. [WPF系列] window自定义

      效果图:     源码下载 SourceCode   参考 Disabling or hiding the minimize, maximize or close button of a WPF ...

  7. 【转载、推荐】不要自称是程序员,我十多年的 IT 职场总结

    注评:一气读完后,有些和我的观点类似.这篇文章显然是外国老写的,但是不妨碍我们的跨国交流. 如果我可以给每个工程教育增加一门课,它不会涉及编译器.门电路或是时间复杂度,而是一门介绍行业现实的入门课,因 ...

  8. svm使用的一般步骤

    LIBSVM 使用的一般步骤是:1)准备数据集,转化为 LIBSVM支持的数据格式 :[label] [index1]:[value1] [index2]:[value2] ...即 [l类别标号] ...

  9. 第3章 Linux常用命令(2)_权限管理命令

    2. 权限管理命令 2.1 改变文件或目录权限:chmod (1)chmod命令 命令名称 chmod(change the permission mode of a file) 命令所在路径 /bi ...

  10. 第3章 窗口与消息_3.1Windows编程模型

    第3章窗口与消息 3.1 Windows_编程模型 (1)窗口程序的运行过程   ①设计窗口   ②注册窗口类(RegisterClassEx).在注册之前,要先填写RegisterClassEx的参 ...