如果要获得上传文件的原始名称,需要定义一个String类型的属性,属性名必须为***FileName,其中***为File属性的名称;同理,如果要获取该文件的MIME类型,需要定义一个***ContentType的String属性

单个文件上传

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");
        FileOutputStream fos = null;
        FileInputStream fis = null;
        try {
            // 建立文件输出流
            System.out.println(getSavePath());
            fos = new FileOutputStream(realpath+ "\\" + getImageFileName());
            // 建立文件上传流
            fis = newFileInputStream(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;
}

    public File getImage() {
        return image;
    }

    public void setImage(File image) {
        this.image = image;
    }

    public String getImageFileName() {
        return imageFileName;
    }

    public void setImageFileName(StringimageFileName) {
        this.imageFileName = imageFileName;
    }

    public String getImageContentType() {
        return imageContentType;
    }

    public void setImageContentType(StringimageContentType) {
        this.imageContentType = imageContentType;
    }

}

多个文件上传

import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class TagUploadListAction extends ActionSupport {
    private static final long serialVersionUID= 1L;
    private String name;

    // 上传多个文件的集合文本
    private List<File> upload;
    // /多个上传文件的类型集合
    private List<String>uploadContextType;
   // 多个上传文件的文件名集合
    private List<String> uploadFileName;

    public String getName() {
            return name;
     }
    public void setName(String name) {
       this.name = name;
    }
    public List<File> getUpload() {
       return upload;
    }
    public void setUpload(List<File>upload) {
       this.upload = upload;
    }
    public List<String>getUploadContextType() {
       return uploadContextType;
    }
    public voidsetUploadContextType(List<String> uploadContextType) {
       this.uploadContextType =uploadContextType;
    }
    public List<String>getUploadFileName() {
       return uploadFileName;
    }
    public voidsetUploadFileName(List<String> uploadFileName) {
       this.uploadFileName = uploadFileName;
    }
    public String execute() {

       // 把上传的文件放到指定的路径下
       String path =ServletActionContext.getServletContext().getRealPath(

              "/WEB-INF/uploadList");

       // 写到指定的路径中
       File file = new File(path);

       // 如果指定的路径没有就创建
       if(!file.exists()) {
           file.mkdirs();
       }

       // 把得到的文件的集合通过循环的方式读取并放在指定的路径下
       for (int i = 0; i < upload.size();i++) {
           try {
              //list集合通过get(i)的方式来获取索引
              FileUtils.copyFile(upload.get(i), new File(file,uploadFileName.get(i)));
           } catch (IOException e) {
              // TODO Auto-generated catchblock
              e.printStackTrace();
           }
       }
       return SUCCESS;
    }
}

Struts 2 之文件上传的更多相关文章

  1. Struts学习之文件上传

    * 单文件上传:        * 在动作类action中声明相关属性:            * 在动作类action中,要声明与页面中表单name属性同名的属性,同名的属性的类型是File类型:  ...

  2. Struts 框架 之 文件上传下载案例

    Struts 框架 文件上传 1. 先准备 Struts 环境 (我使用的是struts 2.3.4版本) 导jar包:

  3. Struts多个文件上传

    Struts2多个文件上传 10级学员 韩晓爽课堂笔记 多个文件上传分为List集合和数组,下面我们着重介绍一下list集合的上传.都大同小异. 一 介绍 1. 在struts2文件上传的时候要先导入 ...

  4. Struts 1 之文件上传

    Struts 1 对Apache的commons-fileupload进行了再封装,把上传文件封装成FormFile对象 定义UploadForm: private FormFilefile; //上 ...

  5. Java Struts文件上传和下载详解

    Struts2文件上传 Struts 2框架提供了内置支持处理文件上传使用基于HTML表单的文件上传.上传一个文件时,它通常会被存储在一个临时目录中,他们应该由Action类进行处理或移动到一个永久的 ...

  6. 转:在Struts 2中实现文件上传

    (本文转自:http://www.blogjava.net/max/archive/2007/03/21/105124.html) 前一阵子有些朋友在电子邮件中问关于Struts 2实现文件上传的问题 ...

  7. java框架篇---struts之文件上传和下载

    Struts2文件上传 Struts 2框架提供了内置支持处理文件上传使用基于HTML表单的文件上传.上传一个文件时,它通常会被存储在一个临时目录中,他们应该由Action类进行处理或移动到一个永久的 ...

  8. Struts 2(八):文件上传

    第一节 基于Struts 2完成文件上传 Struts 2框架中没有提供文件上传,而是通过Common-FileUpload框架或COS框架来实现的,Struts 2在原有上传框架的基础上进行了进一步 ...

  9. 使用commons-fileupload包进行大文件上传注意事项

    项目中使用 commons-fileupload-1.2.1.jar 进行大文件上传. 测试了一把,效果很不错. 总结如下: 必须设置好上传文件的最大阀值 final long MAX_SIZE = ...

随机推荐

  1. [SCOI2016]背单词

    题目描述 Lweb 面对如山的英语单词,陷入了深深的沉思,”我怎么样才能快点学完,然后去玩三国杀呢?“.这时候睿智的凤老师从远处飘来,他送给了 Lweb 一本计划册和一大缸泡椒,他的计划册是长这样的: ...

  2. ●BZOJ 3926 [Zjoi2015]诸神眷顾的幻想乡

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3926题解&&代码: 后缀自动机,Trie树 如果以每个叶子为根,所有的子串一 ...

  3. bzoj 3751: [NOIP2014]解方程

    Description 已知多项式方程: a0+a1x+a2x^2+...+an*x^n=0 求这个方程在[1,m]内的整数解(n和m均为正整数). 解题报告: 这题比较诡,看到高精度做不了,就要想到 ...

  4. ●BZOJ 3545 [ONTAK2010]Peaks(离线)

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3545 http://www.lydsy.com/JudgeOnline/problem.ph ...

  5. cocos2d-x-3.0beta2创建项目遇到“UnicodeDecodeError: 'ascii' codec can't decode byte 0xd7 in position 9: ordinal not in range(128)”的问题

    在Windows平台下 用cocos2d-x-3.0beta2版本下的create_project.py工具创建项目 但是遇到如下问题:UnicodeDecodeError: 'ascii' code ...

  6. Linux学习之CentOS(八)----详解文件的搜寻、查找(转)

    which (寻找『运行档』) [root@www ~]# which [-a] command 选项或参数: -a :将所有由 PATH 目录中可以找到的命令均列出,而不止第一个被找到的命令名称 分 ...

  7. Golang学习笔记:channel

    channel channel是goroutine之间的通信机制,它可以让一个goroutine通过它给另一个goroutine发送数据,每个channel在创建的时候必须指定一个类型,指定的类型是任 ...

  8. C程序练习

    1.编程从键盘任意输入两个时间(例如4时55分和1时25分),计算并输出这两个时间之间的间隔.要求不输出时间差的负号. #include<stdio.h> int main() { int ...

  9. 听说图像识别很难,大神十行代码进行Python图像识别

      随着深度学习算法的兴起和普及,人工智能领域取得了令人瞩目的进步,特别是在计算机视觉领域.21世纪的第二个十年迅速采用卷积神经网络,发明了最先进的算法,大量训练数据的可用性以及高性能和高性价比计算的 ...

  10. Python中strip()、lstrip()、rstrip()用法详解

    Python中有三个去除头尾字符.空白符的函数,它们依次为: strip: 用来去除头尾字符.空白符(包括\n.\r.\t.' ',即:换行.回车.制表符.空格)lstrip:用来去除开头字符.空白符 ...