Search in sources :

Example 31 with IORuntimeException

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

the class Ftp method delFile.

@Override
public boolean delFile(String path) throws IORuntimeException {
    final String pwd = pwd();
    final String fileName = FileUtil.getName(path);
    final String dir = StrUtil.removeSuffix(path, fileName);
    if (false == isDir(dir)) {
        throw new FtpException("Change dir to [{}] error, maybe dir not exist!", path);
    }
    boolean isSuccess;
    try {
        isSuccess = client.deleteFile(fileName);
    } catch (IOException e) {
        throw new IORuntimeException(e);
    } finally {
        // 回到原目录
        cd(pwd);
    }
    return isSuccess;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) IOException(java.io.IOException)

Example 32 with IORuntimeException

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

the class Mail method addImage.

/**
 * 增加图片,图片的键对应到邮件模板中的占位字符串
 *
 * @param cid         图片与占位符,占位符格式为cid:${cid}
 * @param imageStream 图片流,不关闭
 * @param contentType 图片类型,null赋值默认的"image/jpeg"
 * @return this
 * @since 4.6.3
 */
public Mail addImage(String cid, InputStream imageStream, String contentType) {
    ByteArrayDataSource imgSource;
    try {
        imgSource = new ByteArrayDataSource(imageStream, ObjectUtil.defaultIfNull(contentType, "image/jpeg"));
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
    imgSource.setName(cid);
    return setAttachments(imgSource);
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) IOException(java.io.IOException) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

Example 33 with IORuntimeException

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

the class DownloadTest method downloadFileFromUrlTest2.

@Test
@Ignore
public void downloadFileFromUrlTest2() {
    File file = null;
    try {
        file = HttpUtil.downloadFileFromUrl("https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.4.0/hutool-all-5.4.0-sources.jar", FileUtil.file("d:/download/temp"), 1, new StreamProgress() {

            @Override
            public void start() {
                System.out.println("start");
            }

            @Override
            public void progress(long progressSize) {
                System.out.println("download size:" + progressSize);
            }

            @Override
            public void finish() {
                System.out.println("end");
            }
        });
        Assert.assertNotNull(file);
        Assert.assertTrue(file.exists());
        Assert.assertTrue(file.isFile());
        Assert.assertTrue(file.length() > 0);
        Assert.assertTrue(file.getName().length() > 0);
    } catch (Exception e) {
        Assert.assertTrue(e instanceof IORuntimeException);
    } finally {
        FileUtil.del(file);
    }
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) File(java.io.File) StreamProgress(cn.hutool.core.io.StreamProgress) IORuntimeException(cn.hutool.core.io.IORuntimeException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 34 with IORuntimeException

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

the class ExcelFileUtil method getFileMagic.

/**
 * {@link java.io.PushbackInputStream}
 * PushbackInputStream的markSupported()为false,并不支持mark和reset
 * 如果强转成PushbackInputStream在调用FileMagic.valueOf(inputStream)时会报错
 * {@link FileMagic}
 * 报错内容:getFileMagic() only operates on streams which support mark(int)
 * 此处修改成 final InputStream in = FileMagic.prepareToCheckMagic(in)
 *
 * @param in {@link InputStream}
 * @author kefan.qu
 */
private static FileMagic getFileMagic(InputStream in) {
    FileMagic magic;
    in = FileMagic.prepareToCheckMagic(in);
    try {
        magic = FileMagic.valueOf(in);
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
    return magic;
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) FileMagic(org.apache.poi.poifs.filesystem.FileMagic) IOException(java.io.IOException)

Example 35 with IORuntimeException

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

the class Gzip method unGzip.

/**
 * 将压缩流解压到target中
 *
 * @return Gzip
 */
public Gzip unGzip() {
    try {
        source = (source instanceof GZIPInputStream) ? (GZIPInputStream) source : new GZIPInputStream(source);
        IoUtil.copy(source, target);
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
    return this;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) IORuntimeException(cn.hutool.core.io.IORuntimeException) 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