首页>代码>Struts2文件上传配置教程Demo代码下载>/FileUpLoad/src/main/java/com/zuidaima/fileupload/controller/FileUploadAction.java
001package com.zuidaima.fileupload.controller;
002 
003import java.io.File;
004import java.io.FileInputStream;
005import java.io.FileOutputStream;
006import java.io.InputStream;
007import java.io.OutputStream;
008import org.apache.struts2.ServletActionContext;
009 
010import com.opensymphony.xwork2.ActionSupport;
011 
012/**
013 *
014 * @author dyl
015 *
016 */
017public class FileUploadAction extends ActionSupport {
018 
019    private static final long serialVersionUID = 1L;
020 
021    private File fileUpload;
022    private String fileUploadContentType;
023    private String fileUploadFileName;
024    private String uploadDir;
025 
026    public String getFileUploadContentType() {
027        return fileUploadContentType;
028    }
029 
030    public void setFileUploadContentType(String fileUploadContentType) {
031        this.fileUploadContentType = fileUploadContentType;
032    }
033 
034    public String getFileUploadFileName() {
035        return fileUploadFileName;
036    }
037 
038    public void setFileUploadFileName(String fileUploadFileName) {
039        this.fileUploadFileName = fileUploadFileName;
040    }
041 
042    public File getFileUpload() {
043        return fileUpload;
044    }
045 
046    public void setFileUpload(File file) {
047        this.fileUpload = file;
048    }
049 
050    public String getUploadDir() {
051        return uploadDir;
052    }
053 
054    public void setUploadDir(String uploadDir) {
055        this.uploadDir = uploadDir;
056    }
057 
058    public String execute() throws Exception {
059        String newFileName = null;
060        long now = System.currentTimeMillis();
061        // 得到保存上传文件的目录的真实路径
062        File dir = new File(ServletActionContext.getServletContext()
063                .getRealPath(uploadDir));
064        // 如果该目录不存在,就创建
065        if (!dir.exists()) {
066            dir.mkdirs();
067        }
068 
069        // 为避免重名文件覆盖,判断上传文件是否有扩展名,以时间戳作为新的文件名
070        int index = fileUpload.getName().lastIndexOf(".");
071 
072        if (index != -1) {
073            newFileName = now + fileUpload.getName().substring(0,index);
074        } else {
075            newFileName = Long.toString(now);
076        }
077 
078        InputStream is = null;
079        OutputStream os = null;
080        try {
081            // 读取保存在临时目录下的上传文件,写入到新的文件中
082            is = new FileInputStream(fileUpload);
083            os = new FileOutputStream(new File(dir, newFileName));
084 
085            byte[] buf = new byte[1024];
086 
087            int len = -1;
088 
089            while ((len = is.read(buf)) != -1) {
090                os.write(buf, 0, len);
091            }
092            //删除临时文件
093            fileUpload.delete();
094        } finally {
095            is.close();
096            os.close();
097        }
098        return "SUCCESS";
099    }
100 
101    public String display() {
102        return "NONE";
103    }
104}
最近下载更多
张青峰  LV10 2020年4月14日
hehehe998  LV10 2018年12月24日
wenxin2018  LV1 2018年11月20日
poli33  LV1 2018年10月25日
老刘啊  LV2 2018年4月21日
wxsuperwx  LV17 2018年2月1日
qq251106680  LV3 2017年11月9日
dl659257617  LV1 2017年9月15日
n3189543565  LV18 2017年3月29日
果汁听  LV2 2016年10月11日
最近浏览更多
uni-code_0123  LV1 2023年11月29日
nb3121  LV1 2022年12月19日
六六六六六六  LV2 2022年4月18日
书虫冲冲冲冲 2021年1月12日
暂无贡献等级
byteyxr  LV6 2020年7月23日
HJThhh  LV1 2020年7月8日
lyd19931203  LV21 2020年6月16日
莉莉安  LV2 2020年6月12日
mzayy001 2020年5月12日
暂无贡献等级
pzy12345  LV8 2020年4月21日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友