Search in sources :

Example 1 with Wrong

use of app.hongs.util.verify.Wrong in project HongsCORE by ihongs.

the class UploadHelper method upload.

/**
 * 检查文件流并写入目标目录
 * @param xis  上传文件输入流
 * @param type 上传文件类型
 * @param extn 上传文件扩展
 * @param subn 目标文件名称
 * @return
 * @throws Wrong
 */
public File upload(InputStream xis, String type, String extn, String subn) throws Wrong {
    if (extn.contains(".")) {
        // extn = MimeUtil.getExtension(extn);
        extn = extn.substring(extn.lastIndexOf('.') + 1);
    }
    chkTypeOrExtn(type, extn);
    setResultName(subn, extn);
    File file = new File(getResultPath());
    File fdir = file.getParentFile();
    if (!fdir.exists()) {
        fdir.mkdirs();
    }
    // 拷贝数据
    try {
        try (FileOutputStream fos = new FileOutputStream(file);
            BufferedInputStream bis = new BufferedInputStream(xis);
            BufferedOutputStream bos = new BufferedOutputStream(fos)) {
            byte[] buf = new byte[1024];
            int ovr;
            while ((ovr = bis.read(buf)) != -1) {
                bos.write(buf, 0, ovr);
            }
        }
    } catch (IOException ex) {
        throw new Wrong(ex, "fore.form.upload.failed");
    }
    return file;
}
Also used : Wrong(app.hongs.util.verify.Wrong) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 2 with Wrong

use of app.hongs.util.verify.Wrong in project HongsCORE by ihongs.

the class AuthKit method getWrong.

/**
 * 快速输出登录的错误
 * @param k 字段
 * @param w 错误
 * @return
 * @throws HongsException
 */
public static Map getWrong(String k, String w) throws HongsException {
    Map m = new HashMap();
    Map e = new HashMap();
    CoreLocale lang = CoreLocale.getInstance("master");
    if (k != null && !"".equals(k)) {
        m.put(k, new Wrong(w).setLocalizedContext("master"));
        e.put("errs", new Wrongs(m).getErrors());
        e.put("msg", lang.translate(w));
    } else {
        e.put("msg", lang.translate(w));
    }
    e.put("ok", false);
    return e;
}
Also used : CoreLocale(app.hongs.CoreLocale) Wrong(app.hongs.util.verify.Wrong) HashMap(java.util.HashMap) Wrongs(app.hongs.util.verify.Wrongs) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with Wrong

use of app.hongs.util.verify.Wrong in project HongsCORE by ihongs.

the class UploadHelper method upload.

/**
 * 检查临时文件或目标链接情况
 * 参数为临时文件名或结果链接
 * @param name
 * @return
 * @throws Wrong
 */
public File upload(String name) throws Wrong {
    if (name == null || name.length() == 0) {
        setResultName("", null);
        return null;
    }
    String subn = name;
    String extn = "";
    // 避免 Windows 异常
    name = name.replace('\\', '/');
    int i = subn.lastIndexOf('/');
    if (i != -1) {
        subn = subn.substring(i + 1);
    }
    int j = subn.lastIndexOf('.');
    if (j != -1) {
        extn = subn.substring(j + 1);
        subn = subn.substring(0, j);
    }
    /*
         * 修改时文件未重新上传则回传原路径
         * 此时传入名称和扩展应得到相同结果
         * 这种情况无需额外的操作
         */
    setResultName(subn, extn);
    String href = getResultHref();
    String path = getResultPath();
    if (name.equals(href)) {
        return new File(path);
    }
    /*
         * 不能直接当作文件路径来处理
         * 这会导致严重的安全漏洞
         * 如给的是某重要配置文件路径
         * 则可能导致敏感数据泄露
         */
    if (i != -1) {
        throw new Wrong("core.file.upload.not.allows");
    // return upload(new File(subn),Core.getUniqueId());
    }
    /**
     * 尝试移动临时目录的文件
     */
    return upload(new File(getUploadTemp(uploadTemp) + File.separator + name), Core.newIdentity());
}
Also used : Wrong(app.hongs.util.verify.Wrong) File(java.io.File)

Example 4 with Wrong

use of app.hongs.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");
    }
    /**
     * 从文件名中解析类型和提取扩展名
     */
    // type = MimeUtil.getMimeTypes (file).toString(   );
    // extn = MimeUtil.getExtension (file);
    FileNameMap nmap = URLConnection.getFileNameMap();
    String extn = file.getName();
    String type = nmap.getContentTypeFor(extn);
    extn = extn.substring(extn.lastIndexOf('.') + 1);
    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(app.hongs.util.verify.Wrong) FileNameMap(java.net.FileNameMap) File(java.io.File)

Example 5 with Wrong

use of app.hongs.util.verify.Wrong in project HongsCORE by ihongs.

the class UploadHelper method upload.

/**
 * 检查上传对象并写入目标目录
 * @param part
 * @param subn
 * @return
 * @throws Wrong
 */
public File upload(Part part, String subn) throws Wrong {
    if (part == null) {
        setResultName("", null);
        return null;
    }
    String type = part.getContentType();
    String extn = part.getSubmittedFileName();
    /**
     * 从 Mime-Type 和原始文件名中分解出类型和扩展名
     */
    int pos = type.indexOf(',');
    if (pos == -1) {
        type = "";
    } else {
        type = type.substring(0, pos);
    }
    pos = extn.lastIndexOf('.');
    if (pos == -1) {
        extn = "";
    } else {
        extn = extn.substring(1 + pos);
    }
    try {
        return upload(part.getInputStream(), type, extn, subn);
    } catch (IOException ex) {
        throw new Wrong(ex, "fore.form.upload.failed");
    }
}
Also used : Wrong(app.hongs.util.verify.Wrong) IOException(java.io.IOException)

Aggregations

Wrong (app.hongs.util.verify.Wrong)5 File (java.io.File)3 IOException (java.io.IOException)2 CoreLocale (app.hongs.CoreLocale)1 Wrongs (app.hongs.util.verify.Wrongs)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 FileNameMap (java.net.FileNameMap)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1