struts2中文件上传的实现

来源:互联网 发布:北方菜谱软件下载 编辑:程序博客网 时间:2024/05/21 19:25

1.struts文件上传步骤:

1)在WEB-INF/lib下加入commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar。


2)把form表的enctype设置为:"multipart/form-data",并把method改为post。如下:
<form enctype="multipart/form-data" action="${pageContext.request.contextPath}/xxx.action" method="post">
 <input  type="file" name="uploadImage">
</form>

3)在Action类中添加以下属性

public class HelloWorldAction extends ActionSupport{

 private File uploadImage;//得到上传的文件,如果是多个文件上传,可以改为List,必须是泛型。
 private String uploadImageContentType;//得到文件的类型,uploadImage必须为file的文件名,ContentType是struts2的规范,必须这样写
 private String uploadImageFileName;//得到文件的名称,uploadImage必须为file的文件名,FileName是struts2的规范,必须这样写
//这里略省了属性的getter/setter方法

 public String upload() throws Exception{
    String realpath = ServletActionContext.getServletContext().getRealPath("/images");
    File file = new File(realpath);
    if(!file.exists()) file.mkdirs();
    FileUtils.copyFile(uploadImage, new File(file, uploadImageFileName));
    return "success";

  }}

2.如何限制上传文件大小

在struts2中默认的上传文件大小为2M,如果我们想修改可以加常量:

<!--把上传文件大小改为10M左右 -->
    
<constant name="struts.multipart.maxSize" value="10701096"/>

3.单个文件上传

ublic class Image extends ActionSupport{

    private File image;
    private String imageFileName;
    private String imageContentType;
    
    

    public String getImageContentType() {
        return imageContentType;
    }

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

    public File getImage() {
        return image;
    }

    public String getImageFileName() {
        return imageFileName;
    }

    public void setImageFileName(String imageFileName) {
        this.imageFileName = imageFileName;
    }
    public void setImage(File image) {
        this.image = image;
    }
    
    
    @Override
    public String execute() throws Exception {
        
        //1.得到要上传文件夹的绝对路径
        String realpath=ServletActionContext.getServletContext().getRealPath("/images");
        if(image!=null){
            File dir=new File(realpath);
            if(!dir.exists())  dir.mkdirs();
            File file=new File(realpath,imageFileName);
            FileUtils.copyFile(image, file);
            System.out.println(imageContentType+"=filetype");
        }
        
        return SUCCESS;
    }
    
}


2.多文件上传

public class MultiUp extends ActionSupport{

    
    private ArrayList<File> image;
    private ArrayList imageFileName;
    
    
    
    public ArrayList getImageFileName() {
        return imageFileName;
    }
    public ArrayList<File> getImage() {
        return image;
    }
    public void setImage(ArrayList<File> image) {
        this.image = image;
    }
    public void setImageFileName(ArrayList imageFileName) {
        this.imageFileName = imageFileName;
    }
    
    
    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
    
        //先得到路径的决定路径
        String realpath=ServletActionContext.getServletContext().getRealPath("/images");
        if(image!=null){
            File dir=new File(realpath);
            if(!dir.exists()){
                dir.mkdirs();
            }
            for (int i = 0; i < image.size(); i++) {
                File file=new File(dir,(String)imageFileName.get(i));
                FileUtils.copyFile(image.get(i), file);
            }
        }
        return SUCCESS;
    }
}
//实现复制文件的方法
private static void copyFile(File src, File dst) {
        try {
            InputStream in = null;
            OutputStream out = null;
            try {
                in = new BufferedInputStream(new FileInputStream(src),
                        BUFFER_SIZE);
                out = new BufferedOutputStream(new FileOutputStream(dst),
                        BUFFER_SIZE);
                byte[] buffer = new byte[BUFFER_SIZE];
                while (in.read(buffer) > 0) {
                    out.write(buffer);
                }
            } finally {
                if (null != in) {
                    in.close();
                }
                if (null != out) {
                    out.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }



原创粉丝点击