FileUtil_2

简介:

文件的复制、读取,图片的裁剪等操作。


[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. package cn.huwhy.common.utils;  
  2.   
  3. import javax.imageio.ImageIO;  
  4. import javax.imageio.ImageReadParam;  
  5. import javax.imageio.ImageReader;  
  6. import javax.imageio.stream.ImageInputStream;  
  7. import java.awt.*;  
  8. import java.awt.image.BufferedImage;  
  9. import java.io.*;  
  10. import java.util.Iterator;  
  11.   
  12. public class FileUtils {  
  13.     public static void copyFile(String source, String target) throws IOException {  
  14.         BufferedInputStream inBuff = null;  
  15.         BufferedOutputStream outBuff = null;  
  16.         try {  
  17.             // 新建文件输入流并对它进行缓冲  
  18.             inBuff = new BufferedInputStream(new FileInputStream(new File(source)));  
  19.   
  20.             // 新建文件输出流并对它进行缓冲  
  21.             outBuff = new BufferedOutputStream(new FileOutputStream(new File(target)));  
  22.   
  23.             // 缓冲数组  
  24.             byte[] b = new byte[1024 * 5];  
  25.             int len;  
  26.             while ((len = inBuff.read(b)) != -1) {  
  27.                 outBuff.write(b, 0, len);  
  28.             }  
  29.             // 刷新此缓冲的输出流  
  30.             outBuff.flush();  
  31.         } finally {  
  32.             // 关闭流  
  33.             if (inBuff != null)  
  34.                 inBuff.close();  
  35.             if (outBuff != null)  
  36.                 outBuff.close();  
  37.         }  
  38.     }  
  39.   
  40.     public static void copyFile(File sourceFile, File targetFile) throws IOException {  
  41.         BufferedInputStream inBuff = null;  
  42.         BufferedOutputStream outBuff = null;  
  43.         try {  
  44.             // 新建文件输入流并对它进行缓冲  
  45.             inBuff = new BufferedInputStream(new FileInputStream(sourceFile));  
  46.   
  47.             // 新建文件输出流并对它进行缓冲  
  48.             outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));  
  49.   
  50.             // 缓冲数组  
  51.             byte[] b = new byte[1024 * 5];  
  52.             int len;  
  53.             while ((len = inBuff.read(b)) != -1) {  
  54.                 outBuff.write(b, 0, len);  
  55.             }  
  56.             // 刷新此缓冲的输出流  
  57.             outBuff.flush();  
  58.         } finally {  
  59.             // 关闭流  
  60.             if (inBuff != null)  
  61.                 inBuff.close();  
  62.             if (outBuff != null)  
  63.                 outBuff.close();  
  64.         }  
  65.     }  
  66.   
  67.     public static void copyFile(File sourceFile, String savePath, String fileName) throws IOException {  
  68.         BufferedInputStream inBuff = null;  
  69.         BufferedOutputStream outBuff = null;  
  70.         try {  
  71.             // 新建文件输入流并对它进行缓冲  
  72.             inBuff = new BufferedInputStream(new FileInputStream(sourceFile));  
  73.   
  74.             File pathFile = new File(savePath);  
  75.             if (!pathFile.exists()) {  
  76.                 pathFile.mkdirs();  
  77.             }  
  78.             // 新建文件输出流并对它进行缓冲  
  79.             outBuff = new BufferedOutputStream(new FileOutputStream(new File(pathFile, fileName)));  
  80.   
  81.             // 缓冲数组  
  82.             byte[] b = new byte[1024 * 5];  
  83.             int len;  
  84.             while ((len = inBuff.read(b)) != -1) {  
  85.                 outBuff.write(b, 0, len);  
  86.             }  
  87.             // 刷新此缓冲的输出流  
  88.             outBuff.flush();  
  89.         } finally {  
  90.             // 关闭流  
  91.             if (inBuff != null)  
  92.                 inBuff.close();  
  93.             if (outBuff != null)  
  94.                 outBuff.close();  
  95.         }  
  96.     }  
  97.   
  98.     public File getFile(String savePath, String fileName) {  
  99.         return null;  
  100.     }  
  101.   
  102.     public static byte[] getFile(String filePath) throws IOException {  
  103.         File file = new File(filePath);  
  104.         file.exists();  
  105.         byte[] buf = new byte[1024];  
  106.         InputStream in = new FileInputStream(file);  
  107.         ByteArrayOutputStream out = new ByteArrayOutputStream();  
  108.         int n;  
  109.         while ((n = in.read(buf)) != -1) {  
  110.             out.write(buf, 0, n);  
  111.         }  
  112.         in.close();  
  113.         return out.toByteArray();  
  114.     }  
  115.   
  116.     public static String checkImgSize(byte[] img) throws IOException {  
  117.         ByteArrayInputStream in = new ByteArrayInputStream(img);  
  118.         BufferedImage buffer = ImageIO.read(in);  
  119.         int width = buffer.getWidth();  
  120.         int height = buffer.getHeight();  
  121.         return "{\"width\":" + width + ",\"height\":" + height + "}";  
  122.     }  
  123.   
  124.     /**  
  125.      * 图片裁剪  
  126.      */  
  127.     public static byte[] cut(byte[] img, String formatName, int x, int y, int width, int height) throws IOException {  
  128.         ByteArrayInputStream in = new ByteArrayInputStream(img);  
  129.         ByteArrayOutputStream out = new ByteArrayOutputStream();  
  130.         ImageInputStream iis;  
  131.         Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName(formatName);  
  132.         ImageReader reader = it.next();  
  133.         iis = ImageIO.createImageInputStream(in);  
  134.         reader.setInput(iis);  
  135.         ImageReadParam param = reader.getDefaultReadParam();  
  136.         Rectangle rect = new Rectangle(x, y, width, height);  
  137.         param.setSourceRegion(rect);  
  138.         BufferedImage bi = reader.read(0, param);  
  139.         ImageIO.write(bi, formatName, out);  
  140.         return out.toByteArray();  
  141.     }  
  142.   
  143.     public static void zoom(File srcfile, String suffix, int width, String savePath, String zoomName) throws IOException {  
  144.         BufferedImage result = null;  
  145.         if (!srcfile.exists()) {  
  146.             System.out.println("文件不存在");  
  147.         }  
  148.         BufferedImage im = ImageIO.read(srcfile);  
  149.   
  150.   
  151.             /* 新生成结果图片 */  
  152.         int height = im.getHeight(null) * width / im.getWidth(null);//按比例,将高度缩减  
  153.         result = new BufferedImage(width, height,  
  154.                 BufferedImage.TYPE_INT_RGB);  
  155.   
  156.         result.getGraphics().drawImage(  
  157.                 im.getScaledInstance(width, height,  
  158.                         java.awt.Image.SCALE_SMOOTH), 0, 0, null);  
  159.         File zoomFile = new File(savePath, zoomName);  
  160.         if(!zoomFile.exists()){  
  161.             zoomFile.getParentFile().mkdirs();  
  162.         }  
  163.         FileOutputStream out = new FileOutputStream(zoomFile);  
  164.         ImageIO.write(result, suffix, out);  
  165.         out.flush();  
  166.         out.close();  
  167.     }  
  168.   
  169.     public static String getFileExt(String filename) {  
  170.         int index = filename.lastIndexOf('.');  
  171.         if (index > 0) {  
  172.             return filename.substring(index + 1);  
  173.         }  
  174.         return "";  
  175.     }  
  176. }  
目录
相关文章
|
5月前
|
存储 Java 调度
FileInputStream,FileOutputStream 和 FileReader ,FileWriter 类的基本使用【 File类+IO流知识回顾②】
这篇文章回顾了Java中FileInputStream、FileOutputStream、FileReader和FileWriter类的基本使用方法,包括读取和写入文件的操作,以及字符流和字节流的区别和应用场景。
FileInputStream,FileOutputStream 和 FileReader ,FileWriter 类的基本使用【 File类+IO流知识回顾②】
|
存储
人迹罕至的FileReader
FileReader 对象允许Web应用程序异步读取存储在用户计算机上的文件(或原始数据缓冲区)的内容,使用 File 或 Blob 对象指定要读取的文件或数据。 这边文章主要分4 部分介绍FileReader。
70 0
|
9月前
|
Java
FileInputStream和FileOutputStream
FileInputStream和FileOutputStream
62 0
File操作-FileReader(FileWriter)/BufferedReader(Writer)
File操作-FileReader(FileWriter)/BufferedReader(Writer)
66 0
|
存储 Java
FileInputStream 你了解多少
FileInputStream 你了解多少
|
存储 索引
RandomAccessFile详解
此类的实例支持对随机访问文件的读取和写入。随机访问文件的行为类似存储在文件系统中的一个大型 byte 数组。存在指向该隐含数组的光标或索引,称为文件指针;输入操作从文件指针开始读取字节,并随着对字节的读取而前移此文件指针。
1585 0
|
存储 安全 Java
DataOutputStream和DataInputStream(八)
在Java开发中,如果没有使用数据库时,常常会将内容保存在文件上面。 如用户的个人信息。 在保存的时候,需要有一定的格式进行保存,如用 \t 分开员工的每一个信息,用\n 分开每一个员工。 读取的时候,就必须按照相应的格式进行读取。
229 0
DataOutputStream和DataInputStream(八)
|
Java 开发者
RandomAccessFile|学习笔记
快速学习 RandomAccessFile
128 0
RandomAccessFile|学习笔记

热门文章

最新文章