1导入struts2-blank.war所有jar包:\struts-2.3.4\apps\struts2-blank.war

单个文件上传

upload.jsp

<s:form action="upload2.action" method="post" theme="simple" enctype="multipart/form-data">
<tr>
<td id="more">
选择上传文件:<s:file name="file"></s:file><br>
<s:submit type="button" value="submit"/>
</td>
</tr>
</s:form>

struts.xml

 <package name="struts2" extends="struts-default">
<action name="upload2"class="com.hloytax.wg.upload.UploadAction1">
<result name="success">/success.jsp</result>
<interceptor-ref name="fileUpload">
<param name="maximumSize">409600</param> //上传文件大小设置
// <!--allowedTypes (可选) - 以逗号分割的contentType类型列表(例如text/html), <param name="contentType"> application/txt; </param>
<param name="allowedTypes">
</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
UploadAction1.action
public class UploadAction1 extends ActionSupport {

    /**
*
*/ private static final long serialVersionUID = 1L; private File file;
//文件名称
private String fileFileName; //文件类型
private String fileContentType;
//注意:文件名称和文件类型的名称前缀必须相同,
省略get set 方法 @Override
public String execute() throws Exception{ //获取需要上传文件的文件路径
File uploadFile=new File(ServletActionContext.getServletContext().getRealPath("uploadFile"));
//判断文件是否上传,如果上传的话将会创建该目录
if(!uploadFile.exists()){
uploadFile.mkdir(); //创建该目录
} /*//第一种文件上传的方法
//声明文件输入流,为输入流指定文件路径
FileInputStream input=new FileInputStream(file);
//获取输出流,获取文件的文件地址及名称
FileOutputStream out=new FileOutputStream(uploadFile + "\\" +fileFileName); try{
byte[] b=new byte[1024];//每次写入的大小
int i=0;
while((i=input.read(b))>0){
out.write(b,0,i);
}
}catch(Exception e){
e.printStackTrace();
}finally{
input.close();
out.close();
}
*/ //第二种文件上传的方法
//FileUtils.copyFile(file,new File(uploadFile+"\\"+fileFileName));
// FileUtils.copyFile(file,new File(uploadFile,fileFileName));
// System.out.println(uploadFile); //第三种方法
BufferedReader bReader=new BufferedReader(new InputStreamReader(new FileInputStream(file)));
BufferedWriter bWriter=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(uploadFile+"\\"+fileFileName)));
System.out.println(uploadFile);
try{
char[] str=new char[1024];
int i=0;
while((i=bReader.read(str))>0){
bWriter.write(str,0,i);
}
}catch(Exception e){
e.printStackTrace();
}finally{
bReader.close();
bWriter.close();
uploadFile.delete();
} return SUCCESS;
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

多文件上传:

private List<File> file;
private List<String> fileContentType;
private List<String> fileFileName;
private String savePath;
省略get set 方法 上传方法 参照单文件上传
@Override
    public String execute() throws Exception {
        List<File> files= getFile();
        if (files !=null) {
                for (int i = 0; i < files.size(); i++) {
                    FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getFileFileName().get(i));
                    //建立上传文件的输入流
                    System.out.println(getSavePath());
                    FileInputStream fis = new FileInputStream(files.get(i));         byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = fis.read(buffer)) > 0) {
             fos.write(buffer, 0, len);
         }
         fis.close();
         fos.close();
     }
 }
      return SUCCESS;
        
            }     /**
     * 返回上传文件保存的位置
     *
     * @return
     * @throws Exception
     */
    public String getSavePath() throws Exception {
        return ServletActionContext.getServletContext().getRealPath(savePath);
    }
    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }

Struts2 单个文件上传/多文件上传的更多相关文章

  1. struts2实现文件上传(多文件上传)及下载

    一.要实现文件上传,需在项目中添加两个jar文件 二.上传准备的页面 注:必须植入enctype="multipart/form-data"属性,以及提交方式要设置成post &l ...

  2. struts2 s:file标签使用及文件上传例子

      <s:form action="uploadaction" method="post" enctype="multipart/form-da ...

  3. Struts2文件上传--多文件上传(插件uploadify)

    公司需要把以前的Struts2自带的图片上传替换掉,因为不能一个file选择多个文件,本人直接百度搜索图片插件,  貌似就它(uploadify3.2.1)在最前面,也找过很多案例, 其中有不少问题, ...

  4. Struts2之文件上传(单文件/多文件)

    <一>简述: Struts2的文件上传其实也是通过拦截器来实现的,只是该拦截器定义为默认拦截器了,所以不用自己去手工配置,<interceptor name="fileUp ...

  5. ueditor1.3.6jsp版在struts2应用中上传图片报"未找到上传文件"解决方案

    摘要: ueditor1.3.6jsp版在struts2应用中上传图片报"未找到上传文件"解决方案 在struts2应用中使用ueditor富文本编辑器上传图片或者附件时,即使配置 ...

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

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

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

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

  8. struts2 上传下载文件,异常处理,数据类型转换

    一,web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version=" ...

  9. struts2文件上传时获取上传文件的大小

    利用struts2框架上传文件时,如果想要获取上传文件的大小可以利用下面的方式进行: FileInputStream ins = new FileInputStream(file); if (ins. ...

随机推荐

  1. 报错java.net.SocketException: Software caused connection abort: recv failed 怎么办

    产生这个异常的原因有多种方面,单就如 Software caused 所示, 是由于程序编写的问题,而不是网络的问题引起的. 已知会导致这种异常的一个场景如下: 客户端和服务端建立tcp的短连接,每次 ...

  2. 51,PIC,AVR单片机它们的优点缺点都有哪些?

    我有幸接触了几款单片机,并用它们做了一些项目.现在想做个小总结,谈一下自己用各种单片机的感受.仅是个人意见,仁者见仁智者见智. 传统51,我想我就不多说了,适合菜鸟入门,容易上手,价格一般(从性价比方 ...

  3. java.sql.SQLException: Can not issue executeUpdate() for SELECTs

    未处理的多个select语句 解决方法就是:查看有没有用了同一个连接来处理多个SQL语句!

  4. 【HDOJ】1823 Luck and Love

    二维线段树.wa了几次,不存在输出-1,而不再是一位小数. #include <cstdio> #include <cstring> #define MAXN 105 #def ...

  5. 【转】Java ConcurrentModificationException 异常分析与解决方案--还不错

    原文网址:http://www.2cto.com/kf/201403/286536.html 一.单线程 1. 异常情况举例 只要抛出出现异常,可以肯定的是代码一定有错误的地方.先来看看都有哪些情况会 ...

  6. JPA入门例子(采用JPA的hibernate实现版本) 转

    JPA入门例子(采用JPA的hibernate实现版本) jpahibernate数据库jdbcjava框架(1).JPA介绍: JPA全称为Java Persistence API ,Java持久化 ...

  7. jQuery、实例大全

    文章出处 http://www.cnblogs.com/suoning/p/5683047.html 一.简介 定义 jQuery创始人是美国John Resig,是优秀的Javascript框架: ...

  8. ACM2096_小明A+B

    #include<iostream> int main() { using namespace std; int a,b,count; cin>>count; while(co ...

  9. DES加密后get获取url参数无法解密问题

    参考:http://www.cnblogs.com/lori/archive/2011/09/08/2170979.html 问题,就是URL编码问题,如果不对URL进行编码直接加码,那么在解码时,如 ...

  10. 【JAVA - 基础】之数据加密和解密

    1.Base64工具类(可逆): import java.util.HashMap; import java.util.Map; /** * Base64加解密算法 * </p> * Ba ...