Search in sources :

Example 46 with IORuntimeException

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

the class DownloadTest method downloadFileFromUrlTest4.

@Test
@Ignore
public void downloadFileFromUrlTest4() {
    File file = null;
    try {
        file = HttpUtil.downloadFileFromUrl("http://groovy-lang.org/changelogs/changelog-3.0.5.html", FileUtil.file("d:/download/temp"), 1);
        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) IORuntimeException(cn.hutool.core.io.IORuntimeException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 47 with IORuntimeException

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

the class ImgUtil method sliceByRowsAndCols.

/**
 * 图像切割(指定切片的行数和列数),默认RGB模式
 *
 * @param srcImage 源图像,如果非{@link BufferedImage},则默认使用RGB模式
 * @param destDir  切片目标文件夹
 * @param rows     目标切片行数。默认2,必须是范围 [1, 20] 之内
 * @param cols     目标切片列数。默认2,必须是范围 [1, 20] 之内
 */
public static void sliceByRowsAndCols(Image srcImage, File destDir, int rows, int cols) {
    if (false == destDir.exists()) {
        FileUtil.mkdir(destDir);
    } else if (false == destDir.isDirectory()) {
        throw new IllegalArgumentException("Destination Dir must be a Directory !");
    }
    try {
        if (rows <= 0 || rows > 20) {
            // 切片行数
            rows = 2;
        }
        if (cols <= 0 || cols > 20) {
            // 切片列数
            cols = 2;
        }
        // 读取源图像
        final BufferedImage bi = toBufferedImage(srcImage);
        // 源图宽度
        int srcWidth = bi.getWidth();
        // 源图高度
        int srcHeight = bi.getHeight();
        // 每张切片的宽度
        int destWidth = NumberUtil.partValue(srcWidth, cols);
        // 每张切片的高度
        int destHeight = NumberUtil.partValue(srcHeight, rows);
        // 循环建立切片
        Image tag;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                tag = cut(bi, new Rectangle(j * destWidth, i * destHeight, destWidth, destHeight));
                // 输出为文件
                ImageIO.write(toRenderedImage(tag), IMAGE_TYPE_JPEG, new File(destDir, "_r" + i + "_c" + j + ".jpg"));
            }
        }
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) Rectangle(java.awt.Rectangle) IOException(java.io.IOException) IIOImage(javax.imageio.IIOImage) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) RenderedImage(java.awt.image.RenderedImage) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) Point(java.awt.Point)

Example 48 with IORuntimeException

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

the class ZipUtilTest method zipStreamTest.

@Test
@Ignore
public void zipStreamTest() {
    // https://github.com/looly/hutool/issues/944
    String dir = "d:/test";
    String zip = "d:/test.zip";
    try (OutputStream out = new FileOutputStream(zip)) {
        // 实际应用中, out 为 HttpServletResponse.getOutputStream
        ZipUtil.zip(out, Charset.defaultCharset(), false, null, new File(dir));
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 49 with IORuntimeException

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

the class NetUtil method netCat.

/**
 * 使用普通Socket发送数据
 *
 * @param host Server主机
 * @param port Server端口
 * @param data 数据
 * @throws IORuntimeException IO异常
 * @since 3.3.0
 */
public static void netCat(String host, int port, byte[] data) throws IORuntimeException {
    OutputStream out = null;
    try (Socket socket = new Socket(host, port)) {
        out = socket.getOutputStream();
        out.write(data);
        out.flush();
    } catch (IOException e) {
        throw new IORuntimeException(e);
    } finally {
        IoUtil.close(out);
    }
}
Also used : IORuntimeException(cn.hutool.core.io.IORuntimeException) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Socket(java.net.Socket) DatagramSocket(java.net.DatagramSocket) ServerSocket(java.net.ServerSocket)

Example 50 with IORuntimeException

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

the class NetUtil method netCat.

/**
 * 简易的使用Socket发送数据
 *
 * @param host    Server主机
 * @param port    Server端口
 * @param isBlock 是否阻塞方式
 * @param data    需要发送的数据
 * @throws IORuntimeException IO异常
 * @since 3.3.0
 */
public static void netCat(String host, int port, boolean isBlock, ByteBuffer data) throws IORuntimeException {
    try (SocketChannel channel = SocketChannel.open(createAddress(host, port))) {
        channel.configureBlocking(isBlock);
        channel.write(data);
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) 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