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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations