六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 151|回复: 0

上传文件

[复制链接]

升级  3.33%

13

主题

13

主题

13

主题

秀才

Rank: 2

积分
55
 楼主| 发表于 2013-1-14 23:07:36 | 显示全部楼层 |阅读模式
package com.derby.dms.neil;import com.opensymphony.xwork2.ActionSupport;import java.io.File;import java.io.IOException;public class UploadFileAction extends ActionSupport {    private File[] file;    private String[] fileName;    private static final float CONSTANT = 1024f;    private static final String[] TYPES = new String[]{"jpg,bmp,gif"};    private int allowSize = 1;    private static final String CONTANT_FILE_PATH = "d:/upload";    private boolean checkFileSize(int allowSize) {        if (fileName != null && fileName.length > 0) {            for (int i = 0; i < fileName.length; i++) {                Long size = new Long(Math.round(file[i].length() / CONSTANT));                if (size > allowSize) {                    return false;                }            }        }        return true;    }    public boolean checkFileType(String fileName) {        boolean flat = false;        for (String type : TYPES) {            if (fileName.toLowerCase().endsWith(type)) {                flat = true;                break;            }        }        if (!flat) {            StringBuffer buf = new StringBuffer("文件类型必须为以下几种类型:");            for (String type : TYPES) {                buf.append(type);                buf.append(",");            }            addActionError(buf.toString().substring(0, buf.length() - 1));        }        return flat;    }    public boolean uploadFile(File file, String fileName) {        boolean flat = false;        String[] newFileName = fileName.split("\\.");        if (!checkFileSize(allowSize)) {            return flat;        }        File newFilePath = new File(CONTANT_FILE_PATH);        if (!newFilePath.exists()) {            newFilePath.mkdir();        }        File newFile = new File(CONTANT_FILE_PATH + System.currentTimeMillis() + newFileName[fileName.length() - 1]);        try {            org.apache.commons.io.FileUtils.copyFile(file, newFile);        } catch (IOException e) {            e.printStackTrace();        }        return flat;    }    public File[] getFile() {        return file;    }    public void setFile(File[] file) {        this.file = file;    }    public String[] getFileName() {        return fileName;    }    public void setFileName(String[] fileName) {        this.fileName = fileName;    }    /*    public static void main(String[] args) {                int i = Math.round(5/2.5f);        System.out.println(i);    }    */}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表