Java根据URL获取文件的二进制

当需要获取URL的文件,又不想下载文件的时候,可以用这个工具类。方便快捷,使用HttpURLConnection,先根据URL获取文件流,然后把获取的二进制流存储进去数据库即可,到时候拿出来直接下载、打开也是OK的。

上代码:

package jd_test;

import java.io.BufferedReader; 
import java.io.ByteArrayOutputStream; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL;
import java.util.Base64;
import java.util.Random;
import java.util.stream.Stream;

public class UrlResource { 
	 private static Base64.Decoder decoder = Base64.getDecoder();
	 private static Base64.Encoder encoder = Base64.getEncoder();
	public static void main(String[] args){ 
		try { 
			//System.out.println(UrlResource.getUrlDetail("http://www.baidu.com",true)); 
			//saveUrlFile("你的文档的路径.gif", "D:\\1.gif");
			
			System.out.println(encoder.encode(getUrlFileData("http://www.你的文档的路径")));
		  // System.out.println(getUrlFileData("http://www.你的文档的路径"));
		   // saveUrlFile("http://www.你的文档的路径", "D:\\1.pdf");
		  //  System.out.println(UrlResource.getUrlDetail("http://www.你的文档的路径",true)); 
		
			//确定写出文件的位置
			File file = new File("D:\\1.pdf");
			//建立输出字节流
			FileOutputStream fos = new FileOutputStream(file);
			
			//fos.write(decoder.decode(encoder.encode(getUrlFileData("http://www.你的文档的路径"))));
			//fos.write(encoder.encode(getUrlFileData("http://www.你的文档的路径")));
			fos.write(getUrlFileData("http://www.你的文档的路径"));
			
			//用FileOutputStream 的write方法写入字节数组
			//fos.write(getUrlFileData("http://www.你的文档的路径"));
			System.out.println("写入成功");
			//为了节省IO流的开销,需要关闭
			fos.close();
		} catch (Exception e) {
			e.printStackTrace(); 
		} 
	} 
	
	//获取网络文件,转存到fileDes中,fileDes需要带文件后缀名 
	public static void saveUrlFile(String fileUrl,String fileDes) throws Exception 
	{ 
		File toFile = new File(fileDes); 
		if (toFile.exists()) 
		{ 
		// throw new Exception("file exist"); 
		return; 
		} 
		toFile.createNewFile(); 
		FileOutputStream outImgStream = new FileOutputStream(toFile);
		System.out.println(getUrlFileData(fileUrl));
		outImgStream.write(getUrlFileData(fileUrl)); 
		
		outImgStream.close(); 
	} 
	
	public static byte[] getBase64UrlFileData(String fileUrl)
	{
		try {
			
			//发票PDF文件二进制流base64,如果是get请求还需要进行UrlEncode,使用sdk除外。 
			return encoder.encode(getUrlFileData(new String(decoder.decode(fileUrl))));
		} catch (Exception e) {
			e.printStackTrace();
		}
			return null;
		}
	//获取链接地址文件的byte数据 
	public static byte[] getUrlFileData(String fileUrl) throws Exception 
	{ 
		URL url = new URL(fileUrl); 
		HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); 
		httpConn.connect(); 
		InputStream cin = httpConn.getInputStream(); 
		ByteArrayOutputStream outStream = new ByteArrayOutputStream(); 
		byte[] buffer = new byte[1024]; 
		int len = 0; 
		while ((len = cin.read(buffer)) != -1) { 
		outStream.write(buffer, 0, len); 
		} 
		cin.close(); 
		byte[] fileData = outStream.toByteArray(); 
		outStream.close(); 
		return fileData; 
	} 
	
	//获取链接地址的字符数据,wichSep是否换行标记 
	public static String getUrlDetail(String urlStr,boolean withSep) throws Exception 
	{ 
	URL url = new URL(urlStr); 
	HttpURLConnection httpConn = (HttpURLConnection)url.openConnection(); 
	httpConn.connect(); 
	InputStream cin = httpConn.getInputStream(); 
	BufferedReader reader = new BufferedReader(new InputStreamReader(cin,"UTF-8")); 
	StringBuffer sb = new StringBuffer(); 
	String rl = null; 
	while((rl = reader.readLine()) != null) 
	{ 
		if (withSep) 
		{ 
			sb.append(rl).append(System.getProperty("line.separator")); 
		}
		//如果是要直接显示到页面中,可以讲获得的byte数据进行base64加密,加上文件头,直接设置到img的src里面就可以了
		else 
		{ 
			sb.append(rl); 
		} 
	} 
		return sb.toString(); 
	} 



}

 

    原文作者:渴望飞的鱼
    原文地址: https://blog.csdn.net/qq_36411874/article/details/95219405
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞