获取url地址文件流
public static InputStream getInputStreamFromUrl(String urlStr) {
InputStream inputStream=null;
try {
//url解码
URL url = new URL(java.net.URLDecoder.decode(urlStr, "UTF-8"));
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置超时间为3秒
conn.setConnectTimeout(3 * 1000);
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//得到输入流
inputStream = conn.getInputStream();
} catch (IOException e) {
}
return inputStream;
}
根据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();
}
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。