精华内容
下载资源
问答
  • File与byte[]转换

    万次阅读 2019-06-21 16:41:06
    File转换为byte[],下面是给出File的路径参数: public static byte[] getBytesByFile(String filePath) { try { File file=new File(filePath); //获取输入流 FileInputStream fis =...

    File转换为byte[],下面是给出File的路径参数:

        public static byte[] getBytesByFile(String filePath) {
            try {
                File file=new File(filePath);
                //获取输入流
                FileInputStream fis = new FileInputStream(file);
    
                //新的 byte 数组输出流,缓冲区容量1024byte
                ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
                //缓存
                byte[] b = new byte[1024];
                int n;
                while ((n = fis.read(b)) != -1) {
                    bos.write(b, 0, n);
                }
                fis.close();
                //改变为byte[]
                byte[] data = bos.toByteArray();
                //
                bos.close();
                return data;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

    byte[]转换为File,输入参数:文件byte[],文件转换后的路径(不含文件名.格式),文件名称(包括格式)

    public static void getFileByBytes(byte[] bytes, String filePath, String fileName) {
            BufferedOutputStream bos = null;
            FileOutputStream fos = null;
            File file = null;
            try {
                File dir = new File(filePath);
                // 判断文件目录是否存在
                if (!dir.exists() && dir.isDirectory()) {
                    dir.mkdirs();
                }
                file = new File(filePath + "\\" + fileName);
    
                //输出流
                fos = new FileOutputStream(file);
    
                //缓冲流
                bos = new BufferedOutputStream(fos);
    
                //将字节数组写出
                bos.write(bytes);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (bos != null) {
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

     

    展开全文
  • Java File与byte[]互转

    万次阅读 2019-01-23 20:19:37
    1、File 转成 byte[] public static byte[] getImageStream(String imageUrl, HttpServletRequest request) { ServletContext application = request.getSession().getServletContext(); String url = ...

    1、File 转成 byte[]

    public static byte[] getImageStream(String imageUrl, 
    									HttpServletRequest request) {
    
    	ServletContext application = request.getSession().getServletContext();
    	String url = application.getRealPath("/")+imageUrl;
    	byte[] buffer = null;
    	File file = new File(url);
    	FileInputStream fis;
    	try {
    		ByteArrayOutputStream bos = new ByteArrayOutputStream();
    		fis = new FileInputStream(file);
    	    byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
    		     bos.write(b, 0, n);
    		}
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
            if(file.exists()) {
            	 file.delete();
            }
    	} catch (FileNotFoundException e) {
    		e.printStackTrace();
    	}catch (IOException e) {
    		e.printStackTrace();
    	}
    	return buffer;
    }
    

    2、byte[] 转成 File

    public static void readBin2Image(byte[] byteArray, String targetPath) {
    	InputStream in = new ByteArrayInputStream(byteArray);
        File file = new File(targetPath);
        String path = targetPath.substring(0, targetPath.lastIndexOf("/"));
        if (!file.exists()) {
            new File(path).mkdir();
        }
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            int len = 0;
            byte[] buf = new byte[1024];
            while ((len = in.read(buf)) != -1) {
                fos.write(buf, 0, len);
            }
            fos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != fos) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    展开全文
  • byte[]与File相互转换

    千次阅读 2020-11-10 09:46:44
    public class FileUtils { public static void main(String[] args) { File file = new File("F:/... byte[] buffer = FileToByte(file); ByteToFile(buffer,"F:/opt/target",name); } public static byte[]
    public class FileUtils {
    	
    	public static void main(String[] args) {
    		File file = new File("F:/opt/temp/MyBatis.pdf");
    		String name = file.getName();
    		byte[] buffer = FileToByte(file);
    		ByteToFile(buffer,"F:/opt/target",name);
    	}
    
    	public static byte[] FileToByte(File file){
    		byte[] buffer = null;
    		FileInputStream fis = null;
    		ByteArrayOutputStream bos = null;
    		try {
    			fis = new FileInputStream(file);
    			
    			bos = new ByteArrayOutputStream();
    			
    			byte[] b = new byte[1024];
    			
    			int n;
    			
    			while ((n = fis.read(b)) != -1){
    				bos.write(b, 0, n);
    			}
    			
    			buffer = bos.toByteArray();
    		} catch (Exception e) {
    			// TODO: handle exception
    			e.printStackTrace();
    		} finally {
    			if(fis != null){
    				try {
    					fis.close();
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    			}
    			if(bos != null){
    				try {
    					bos.close();
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    			}
    		}
    		return buffer;
    	}
    
    	public static void ByteToFile(byte[] buf, String filePath, String fileName){
    		BufferedOutputStream bos = null;
    		FileOutputStream fos = null;
    		File file = null;
    		try {
    			File dir = new File(filePath);
    			if (!dir.exists() && dir.isDirectory()){
    				dir.mkdirs();
    			}
    			file = new File(filePath + File.separator + fileName);
    			fos = new FileOutputStream(file);
    			bos = new BufferedOutputStream(fos);
    			bos.write(buf);
    		} catch (Exception e) {
    			// TODO: handle exception
    		} finally {
    			if(bos != null){
    				try {
    					bos.close();
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    			}
    			if(fos != null){
    				try {
    					fos.close();
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    			}
    		}
    	}
    
    }
    
    展开全文
  • Image,File与Byte转换

    千次阅读 2012-05-03 15:02:31
    原址:... 从byte[]转Image  public static byte[] getImageBytes(Image image) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageLoader

    原址:http://superheizai.iteye.com/blog/630855
    从byte[]转Image 
    public static byte[] getImageBytes(Image image) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageLoader imageLoader = new ImageLoader();
    imageLoader.data = new ImageData[] { image.getImageData() };
    imageLoader.save(baos, image.type);
    byte[] imageByteArray = baos.toByteArray();
    try {
    baos.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    return imageByteArray;
    }
    从File转byte[] 
    public static byte[] getBytesFromFile(File f){
    if (f == null){
    return null;
    }
    try {
    FileInputStream stream = new FileInputStream(f);
    ByteArrayOutputStream out = new ByteArrayOutputStream(1000);
    byte[] b = new byte[1000];
    int n;
    while ((n = stream.read(b)) != -1)
    out.write(b, 0, n);
    stream.close();
    out.close();
    return out.toByteArray();
    } catch (IOException e){
    }
    return null;
    }
    从byte[]转file 
    public static File getFileFromBytes(byte[] b, String outputFile) ...{
    BufferedOutputStream stream = null;
    File file = null;
    try ...{
    file = new File(outputFile);
    FileOutputStream fstream = new FileOutputStream(file);
    stream = new BufferedOutputStream(fstream);
    stream.write(b);
    } catch (Exception e) ...{
    e.printStackTrace();
    } finally ...{
    if (stream != null) ...{
    try ...{
    stream.close();
    } catch (IOException e1) ...{
    e1.printStackTrace();
    }
    }
    }
    return file;
    }



    展开全文
  • Java byte转化成file

    万次阅读 2018-03-21 17:43:18
    java io 真是 我的一块心病~~~拿到文件File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/aaa.jpg");根据文件构建文件输出流...写byte到文件输出流里fos.writ...
  • Filebyte[]

    千次阅读 2019-08-14 14:55:37
    1.将File文件转为byte[] public class FileChange { public static byte[] File2byte(File tradeFile){ byte[] buffer = null; try { FileInputStream fis = new FileInputStream(tradeFile); ...
  • Filebyte[ ] byte[ ]转MultipartFile

    千次阅读 2020-10-30 15:10:49
    Filebyte[ ] File file = new File("xx"); byte[] buf = null; try { FileInputStream fis = new FileInputStream(file ); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] byte = new ...
  • Filebyte[]转换

    千次阅读 2017-12-26 09:59:32
    public static byte[] FileToByte(String filePath) { byte[] buffer = null; try { File file = new File(filePath); FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream
  • File tempFile = new File(Environment. getExternalStoragePublicDirectory (Environment. DIRECTORY_PICTURES ). toString() , "temp.jpg" ) ; if (tempFile.exists()){ try { FileOutputStream out = ...
  • byte[] 转 file

    万次阅读 2018-07-18 16:22:46
    * byte[] 转 file * @param bytes * @param path * @return * @see [类、类#方法、类#成员] */ public static void byteToFile(byte[] bytes, String path) { try { // 根据绝对路径初始化文件 File ...
  • byte[]和File转互相转换

    千次阅读 2020-12-31 17:50:24
    File --> byte[] /** * 将文件转换成byte数组 * @param file 目标文件 ... public static byte[] fileToByte(File file) { byte[] bytes = null; try { FileInputStream fis = new FileInputStream(file);
  • byte数组和File,InputStream互转

    千次阅读 2018-05-08 17:05:57
    1、将File、FileInputStream 转换为byte数组: File file = new File("file.txt"); InputStream input = new FileInputStream(file); byte[] byt = new byte[input.available()]; input.read(byt)...
  • 将文件File转换成byte数组

    千次阅读 2019-01-29 15:31:00
    /** * 将文件转换成byte数组 * @param filePath * @return */ public static byte[] File2byte(File tradeFile){ byte[] buffer = null; try { Fil...
  • 1、将File、FileInputStream 转换为byte数组:  File file = new File("file.txt");  InputStream input = new FileInputStream(file);  byte[] byt = new byte[input.available()];  input.read...
  • ByteFile、MultipartFile之间的转换

    千次阅读 2020-08-07 09:36:06
    一、byte[]转MultipartFile 需要引入jar包才能使用 pom.xml(版本可自行指定) <dependency> <groupId>org.springframework</groupId> <artifactId>spring-mock</artifactId> <...
  • byte[] to file or file to byte[]

    千次阅读 2013-04-24 08:29:18
     * byte array to file or  * file to byte array test  *  * @author jwang  * @date 2013.4.24  * @version 1.0  */ public class FileToByteArray {  private static final String ...
  • 1、将File、FileInputStream 转换为byte数组:File file = new File("test.txt");InputStream input = new FileInputStream(file);byte[] byt = new byte[input.available()];input.read(byt);2、将byte...
  • android File 转成 byte[]

    千次阅读 2017-04-11 16:39:39
    说明:android File 转成 byte[] 代码: package briefer.tutu.utils; import java.io.Closeable; import java.io.File; import java.io.RandomAccessFile; /** * Created by zst on 2017/4/11. */ public ...
  • FileUtils工具类(byte[] 与File互转 文件夹清空 获取文件名) package com.utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; import java.util.zip.CRC32; import java.util....
  • Java 中 File to byte[]

    千次阅读 2018-11-14 11:14:12
    JDK1.7中nio的Files对象可以协助完成这个功能 File file = new File(filePath); byte[] b = Files.readAllBytes(file.toPath()); 完~

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 487,930
精华内容 195,172
关键字:

bytefile与