Search in sources :

Example 56 with IORuntimeException

use of cn.hutool.core.io.IORuntimeException in project hutool by looly.

the class Ftp method download.

/**
 * 下载文件到输出流
 *
 * @param path            服务端的文件路径
 * @param fileName        服务端的文件名
 * @param out             输出流,下载的文件写出到这个流中
 * @param fileNameCharset 文件名编码,通过此编码转换文件名编码为ISO8859-1
 * @throws IORuntimeException IO异常
 * @since 5.5.7
 */
public void download(String path, String fileName, OutputStream out, Charset fileNameCharset) throws IORuntimeException {
    String pwd = null;
    if (this.backToPwd) {
        pwd = pwd();
    }
    if (false == isDir(path)) {
        throw new FtpException("Change dir to [{}] error, maybe dir not exist!", path);
    }
    if (null != fileNameCharset) {
        fileName = new String(fileName.getBytes(fileNameCharset), StandardCharsets.ISO_8859_1);
    }
    try {
        client.setFileType(FTPClient.BINARY_FILE_TYPE);
        client.retrieveFile(fileName, out);
    } catch (IOException e) {
        throw new IORuntimeException(e);
    } finally {
        if (backToPwd) {
            cd(pwd);
        }
    }
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) IOException(java.io.IOException)

Example 57 with IORuntimeException

use of cn.hutool.core.io.IORuntimeException in project hutool by looly.

the class ExcelWriter method flush.

/**
 * 将Excel Workbook刷出到输出流
 *
 * @param out        输出流
 * @param isCloseOut 是否关闭输出流
 * @return this
 * @throws IORuntimeException IO异常
 * @since 4.4.1
 */
public ExcelWriter flush(OutputStream out, boolean isCloseOut) throws IORuntimeException {
    Assert.isFalse(this.isClosed, "ExcelWriter has been closed!");
    try {
        this.workbook.write(out);
        out.flush();
    } catch (IOException e) {
        throw new IORuntimeException(e);
    } finally {
        if (isCloseOut) {
            IoUtil.close(out);
        }
    }
    return this;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) IOException(java.io.IOException)

Example 58 with IORuntimeException

use of cn.hutool.core.io.IORuntimeException in project hutool by looly.

the class ExceptionUtilTest method wrapTest.

@Test
public void wrapTest() {
    IORuntimeException e = ExceptionUtil.wrap(new IOException(), IORuntimeException.class);
    Assert.assertNotNull(e);
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) IOException(java.io.IOException) Test(org.junit.Test)

Example 59 with IORuntimeException

use of cn.hutool.core.io.IORuntimeException in project hutool by looly.

the class FileCopier method copy.

// -------------------------------------------------------------------------------------------------------- Getters and Setters end
/**
 * 执行拷贝<br>
 * 拷贝规则为:
 * <pre>
 * 1、源为文件,目标为已存在目录,则拷贝到目录下,文件名不变
 * 2、源为文件,目标为不存在路径,则目标以文件对待(自动创建父级目录)比如:/dest/aaa,如果aaa不存在,则aaa被当作文件名
 * 3、源为文件,目标是一个已存在的文件,则当{@link #setOverride(boolean)}设为true时会被覆盖,默认不覆盖
 * 4、源为目录,目标为已存在目录,当{@link #setCopyContentIfDir(boolean)}为true时,只拷贝目录中的内容到目标目录中,否则整个源目录连同其目录拷贝到目标目录中
 * 5、源为目录,目标为不存在路径,则自动创建目标为新目录,然后按照规则4复制
 * 6、源为目录,目标为文件,抛出IO异常
 * 7、源路径和目标路径相同时,抛出IO异常
 * </pre>
 *
 * @return 拷贝后目标的文件或目录
 * @throws IORuntimeException IO异常
 */
@Override
public File copy() throws IORuntimeException {
    final File src = this.src;
    final File dest = this.dest;
    // check
    Assert.notNull(src, "Source File is null !");
    if (false == src.exists()) {
        throw new IORuntimeException("File not exist: " + src);
    }
    Assert.notNull(dest, "Destination File or directiory is null !");
    if (FileUtil.equals(src, dest)) {
        throw new IORuntimeException("Files '{}' and '{}' are equal", src, dest);
    }
    if (src.isDirectory()) {
        // 复制目录
        if (dest.exists() && false == dest.isDirectory()) {
            // 源为目录,目标为文件,抛出IO异常
            throw new IORuntimeException("Src is a directory but dest is a file!");
        }
        if (FileUtil.isSub(src, dest)) {
            throw new IORuntimeException("Dest is a sub directory of src !");
        }
        final File subTarget = isCopyContentIfDir ? dest : FileUtil.mkdir(FileUtil.file(dest, src.getName()));
        internalCopyDirContent(src, subTarget);
    } else {
        // 复制文件
        internalCopyFile(src, dest);
    }
    return dest;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) File(java.io.File)

Example 60 with IORuntimeException

use of cn.hutool.core.io.IORuntimeException in project hutool by looly.

the class FileSystemUtil method createZip.

/**
 * 创建 Zip的{@link FileSystem}
 *
 * @param path 文件路径,可以是目录或Zip文件等
 * @param charset 编码
 * @return {@link FileSystem}
 */
public static FileSystem createZip(String path, Charset charset) {
    if (null == charset) {
        charset = CharsetUtil.CHARSET_UTF_8;
    }
    final HashMap<String, String> env = new HashMap<>();
    env.put("create", "true");
    env.put("encoding", charset.name());
    try {
        return FileSystems.newFileSystem(URI.create("jar:" + Paths.get(path).toUri()), env);
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) HashMap(java.util.HashMap) IOException(java.io.IOException)

Aggregations

IORuntimeException (cn.hutool.core.io.IORuntimeException)81 IOException (java.io.IOException)75 File (java.io.File)9 StreamProgress (cn.hutool.core.io.StreamProgress)4 FileOutputStream (java.io.FileOutputStream)4 InputStream (java.io.InputStream)4 OutputStream (java.io.OutputStream)4 UtilException (cn.hutool.core.exceptions.UtilException)3 POIException (cn.hutool.poi.exceptions.POIException)3 Rectangle (java.awt.Rectangle)3 BufferedImage (java.awt.image.BufferedImage)3 BufferedReader (java.io.BufferedReader)3 SocketChannel (java.nio.channels.SocketChannel)3 Path (java.nio.file.Path)3 Test (org.junit.Test)3 StreamGobbler (ch.ethz.ssh2.StreamGobbler)2 CryptoException (cn.hutool.crypto.CryptoException)2 RenderedImage (java.awt.image.RenderedImage)2 BufferedWriter (java.io.BufferedWriter)2 FileInputStream (java.io.FileInputStream)2