重庆幸运农场中奖金额|重庆幸运农场官网
MyException - 我的異常網
當前位置:我的異常網» Java Web開發 » 諸位大大給個上傳圖片攔截器代碼

諸位大大給個上傳圖片攔截器代碼

www.h0f1.com  網友分享于:2013-12-25  瀏覽:52次
各位大大給個上傳圖片攔截器代碼
上傳核心代碼我已經有了,現在想要一個Struts2的攔截器代碼,就是攔截圖片大小、格式的攔截器,哪位朋友有的共享給我不勝感激!!!

------解決方案--------------------
Java code
2.Action類


package com.sterning;

import java.io.File;

import javax.servlet.ServletContext;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;

public class StrutsFileUpload extends ActionSupport implements
        ServletContextAware {

    private File upload;// 實際上傳文件

    private String uploadContentType; // 文件的內容類型

    private String uploadFileName; // 上傳文件名

    private String fileCaption;// 上傳文件時的備注

    private ServletContext context;

    public String execute() throws Exception {

        try {
            
            String targetDirectory = context.getRealPath("/upload");
            String targetFileName = uploadFileName;
            File target = new File(targetDirectory, targetFileName);
            FileUtils.copyFile(upload, target);            
            
            setUploadFileName(target.getPath());//保存文件的存放路徑
        } catch (Exception e) {

            addActionError(e.getMessage());

            return INPUT;
        }

        return SUCCESS;

    }

    public String getFileCaption() {
        return fileCaption;
    }

    public void setFileCaption(String fileCaption) {
        this.fileCaption = fileCaption;
    }

    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 void setServletContext(ServletContext context) {
        this.context = context;
    }

}

文章評論

軟件開發程序錯誤異常ExceptionCopyright © 2009-2015 MyException 版權所有
重庆幸运农场中奖金额