Search in sources :

Example 6 with Wrong

use of io.github.ihongs.util.verify.Wrong in project HongsCORE by ihongs.

the class UploadHelper method upload.

/**
 * 检查文件对象并写入目标目录
 * 检查当前文件
 * @param file
 * @param subn
 * @return
 * @throws Wrong
 */
public File upload(File file, String subn) throws Wrong {
    if (file == null) {
        setResultName("", null);
        return null;
    }
    if (file.exists() == false) {
        throw new Wrong("core.file.upload.not.exists");
    }
    /**
     * 从文件名中解析类型和提取扩展名
     */
    String name = file.getName();
    String type = getTypeByName(name);
    String extn = getExtnByName(name);
    chkTypeOrExtn(type, extn);
    setResultName(subn, extn);
    /**
     * 原始文件与目标文件不同才需移动
     */
    File dist = new File(getResultPath());
    if (!dist.equals(file)) {
        File dirt = dist.getParentFile();
        if (!dirt.isDirectory()) {
            dirt.mkdirs();
        }
        file.renameTo(dist);
    }
    return dist;
}
Also used : Wrong(io.github.ihongs.util.verify.Wrong) File(java.io.File)

Aggregations

Wrong (io.github.ihongs.util.verify.Wrong)6 File (java.io.File)3 Wrongs (io.github.ihongs.util.verify.Wrongs)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 CoreLocale (io.github.ihongs.CoreLocale)1 VerifyHelper (io.github.ihongs.action.VerifyHelper)1 DB (io.github.ihongs.db.DB)1 Table (io.github.ihongs.db.Table)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileOutputStream (java.io.FileOutputStream)1