1  在Struts2中上传文件需要 commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar 这两个包。

2  确认页面form表单上的提交方式为POST,enctype属性的值为“multipart/form-data”。

fileupload.jsp页面:

 <body>
<form action="<%=basePath%>lixiang/file_upload.action" method="post" enctype="multipart/form-data">
选择文件:<input type="file" name="upload"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
FileUploadAction.java
 package com.cy.action;

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; import com.opensymphony.xwork2.ActionSupport; public class FileUploadAction extends ActionSupport{ private static final long serialVersionUID = 1L;
private File upload;
//获取上传文件类型
private String uploadContentType;
//获取上传文件名
private String uploadFileName;
//保存路径
private String savePath; public String upload(){ File dir = new File(savePath);
//先判断文件夹是否存在
if(!dir.exists()){
dir.mkdir();
} //再判断文件是否已经存在
String filePath = savePath + "\\"+uploadFileName;
File file = new File(filePath);//上传后的目标文件
FileOutputStream out = null;
FileInputStream in = null;
if(!file.exists()){
try {
out = new FileOutputStream(filePath);
in = new FileInputStream(upload); byte[] bytes = new byte[1024];
int lenth = 0;
while ((lenth = in.read(bytes)) > 0) {
out.write(bytes, 0, lenth);
} } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} return SUCCESS;
} // 提供set、get方法
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
} }

struts.xml

 <action name="file_*" class="com.cy.action.FileUploadAction" method="{1}">
<param name="savePath">E:\images</param> <!-- 设置上传文件存放的路径  -->
<result name="success">/layout.jsp</result>
<result name="input">/fileupload.jsp</result>
</action>

Struts2文件上传的更多相关文章

  1. 【Java EE 学习 35 下】【struts2】【struts2文件上传】【struts2自定义拦截器】【struts2手动验证】

    一.struts2文件上传 1.上传文件的时候要求必须使得表单的enctype属性设置为multipart/form-data,把它的method属性设置为post 2.上传单个文件的时候需要在Act ...

  2. springMvc 使用ajax上传文件,返回获取的文件数据 附Struts2文件上传

    总结一下 springMvc使用ajax文件上传 首先说明一下,以下代码所解决的问题 :前端通过input file 标签获取文件,通过ajax与后端交互,后端获取文件,读取excel文件内容,返回e ...

  3. Struts2文件上传下载

    Struts2文件上传 Struts2提供 FileUpload拦截器,用于解析 multipart/form-data 编码格式请求,解析上传文件的内容,fileUpload拦截器 默认在defau ...

  4. Struts2文件上传和下载(原理)

    转自:http://zhou568xiao.iteye.com/blog/220732 1.    文件上传的原理:表单元素的enctype属性指定的是表单数据的编码方式,该属性有3个值:1)     ...

  5. struts2文件上传大小限制问题小结

    一:首先看一下程序执行中出现的对应报错信息,如下所示: [WARN ] 2015-03-03 15:31:11 :Unable to parse request org.apache.commons. ...

  6. Struts2 文件上传

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

  7. JAVA Web 之 struts2文件上传下载演示(二)(转)

    JAVA Web 之 struts2文件上传下载演示(二) 一.文件上传演示 详细查看本人的另一篇博客 http://titanseason.iteye.com/blog/1489397 二.文件下载 ...

  8. JAVA Web 之 struts2文件上传下载演示(一)(转)

    JAVA Web 之 struts2文件上传下载演示(一) 一.文件上传演示 1.需要的jar包 大多数的jar包都是struts里面的,大家把jar包直接复制到WebContent/WEB-INF/ ...

  9. (八)Struts2 文件上传和下载

    所有的学习我们必须先搭建好Struts2的环境(1.导入对应的jar包,2.web.xml,3.struts.xml) 第一节:Struts2 文件上传 Struts2 文件上传基于Struts2 拦 ...

  10. struts2文件上传,文件类型 allowedTypes

    struts2文件上传,文件类型 allowedTypes 1 '.a' : 'application/octet-stream', 2 '.ai' : 'application/postscript ...

随机推荐

  1. 20150629_Andriod_06_插入_删除_弹出式操作数据

    Fr_06_view_s6 --> activity_f6_insert              --> activity_f7__delete ******************** ...

  2. Labeling Balls 分类: POJ 2015-07-28 19:47 10人阅读 评论(0) 收藏

    Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11893 Accepted: 3408 Descr ...

  3. Java多线程的三种实现方式

    java多线程的三种实现方式 一.继承Thread类 二.实现Runnable接口 三.使用ExecutorService, Callable, Future 无论是通过继承Thread类还是实现Ru ...

  4. MySQL常用操作总结

    MySQL常用操作 前提条件:已安装MySQL. 学习目标:用一条sql语句写出A和B的剩余数量 AA表 BB表 以上为一道面试题,接下来由这道面试题来回顾一些数据库的基本操作. 登录MySQL su ...

  5. Javascript中setTimeout()的用法详解

    1.SetTimeOut()       1.1 SetTimeOut()语法例子       1.2 用SetTimeOut()执行Function       1.3 SetTimeout()语法 ...

  6. ExecutorService线程池讲解

    ExecutorService 建立多线程的步骤: 1.定义线程类 class Handler implements Runnable{ } 2.建立ExecutorService线程池 Execut ...

  7. CentOS 修改线程数限制等(limits.conf)

    修改/etc/security/limits.conf,例如启动程序的用户为webadmin,则添加以下配置: webadmin - nofile 65536 webadmin - nproc 655 ...

  8. winform应用程序自动更新版本

    http://blog.csdn.net/gxxloveszj/article/details/8278187 http://www.cnblogs.com/x369/articles/105656. ...

  9. Repeater数据绑定

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs& ...

  10. 异步设备IO 《windows核心编程》第10章学习

    异步IO操作与同步操作区别: 在CreateFile里的FILE_FLAG_OVERLAPPED标志 异步操作函数LPOVERLAPPED参数 接收IO请求完成通知 触发设备内核对象 缺点:同一个设备 ...