获取图片文件
来源:互联网 发布:无约束优化问题 编辑:程序博客网 时间:2024/06/13 21:36
package com.test;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.channels.FileChannel;
publicclass FileFromUrl {
publicstaticvoid main(String[]args) {
String imageUrl="https://mmbiz.qpic.cn/mmbiz_jpg/RcTuibWiazWLNSZCHdqxOoxnDKrK3dWkvACt97p9Tof3ggEty3nPTaBviaV3QRdoN9nUcavjAOmqxicav5vNFZFuFg/0";
try {
File file = File.createTempFile("temp",".jpg");
URL url =new URL(imageUrl);
HttpURLConnection httpUrlConn = (HttpURLConnection)url.openConnection();
// set key word
httpUrlConn.setConnectTimeout(5000);
httpUrlConn.setReadTimeout(30000);
httpUrlConn.setDoOutput(true);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
// set request head
httpUrlConn.setRequestProperty("Connection","Keep-Alive");
httpUrlConn.setRequestProperty("Charset","UTF-8");
// set request method
httpUrlConn.setRequestMethod("GET");
// set content type
httpUrlConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
//connectwechat
httpUrlConn.connect();
//get fileinputstream
InputStream inputStream=httpUrlConn.getInputStream();
// gettemp file
transfer(inputStream,new FileOutputStream(file),BUFFER_SIZE,true,0);
httpUrlConn.disconnect();
}catch (Exceptione) {
//TODO: handle exception
}
}
privatestaticintBUFFER_SIZE=1024;
publicstaticvoid transfer(
InputStream inputStream, OutputStreamoutputStream,intbufferSize,
booleancleanUp,longlength)
throws IOException {
if (inputStream == null) {
thrownew IllegalArgumentException("Input stream is null");
}
if (outputStream == null) {
thrownew IllegalArgumentException("Output stream is null");
}
if (bufferSize <= 0) {
bufferSize =BUFFER_SIZE;
}
try {
if ( (inputStreaminstanceof FileInputStream) &&
(outputStreaminstanceof FileOutputStream)) {
FileInputStream fileInputStream = (FileInputStream)inputStream;
FileOutputStream fileOutputStream =
(FileOutputStream)outputStream;
transferFileChannel(
fileInputStream.getChannel(),fileOutputStream.getChannel(),
length);
}
else {
transferByteArray(
inputStream,outputStream,bufferSize,length);
}
}
finally {
if (cleanUp) {
cleanUp(false,inputStream,outputStream);
}
}
}
protectedstaticvoid transferFileChannel(
FileChannel inputFileChannel, FileChanneloutputFileChannel,
longlength)
throws IOException {
if (length <= 0) {
length =inputFileChannel.size() -inputFileChannel.position();
}
longcount = 0;
while (count < length) {
count +=inputFileChannel.transferTo(
inputFileChannel.position() +count,length -count,
outputFileChannel);
}
}
protectedstaticvoid transferByteArray(
InputStream inputStream, OutputStreamoutputStream,intbufferSize,
longlength)
throws IOException {
byte[]bytes =newbyte[bufferSize];
if (length > 0) {
longremainingLength =length;
while (remainingLength > 0) {
intreadBytes =inputStream.read(
bytes, 0, (int)Math.min(remainingLength,bufferSize));
if (readBytes == -1) {
break;
}
outputStream.write(bytes, 0, readBytes);
remainingLength -=readBytes;
}
}
else {
intvalue = -1;
while ((value = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, value);
}
}
}
publicstaticvoid cleanUp(booleanquite, Closeable...closeables) {
IOException ioException =null;
for (Closeablecloseable :closeables) {
if (closeable != null) {
try {
closeable.close();
}
catch (IOExceptionioe) {
if (ioException == null) {
ioException =ioe;
}
else {
ioException.addSuppressed(ioe);
}
}
}
}
if (ioException == null) {
return;
}
}
}
- 获取Drawable文件图片
- 图片 获取 文件
- 获取图片文件
- 获取图片文件的属性
- 根据uri获取图片文件
- Winform动态获取资源文件中的图片
- 获取图片文件的二进制数组
- android获取图片文件头信息
- 获取页面中的图片文件地址
- 获取图片文件长度和高度
- WinFrom中如何获取文件图片路径
- wpf 获取MP3文件中的专辑图片!
- 获取图片文件尺寸效率比较
- MFC从资源文件获取png图片
- C++ 获取URL图片、html文件,CInternetSession
- 动态获取文件路径或图片路径
- 获取文件和图片的方法
- input标签获取图片文件尺寸
- UIPageControl更换成图片
- iOS----点赞动画
- C# PDF转为图片的方法
- leetcode 98. Validate Binary Search Tree
- 新建Maven工程webapp项目index.jsp报错解决方法
- 获取图片文件
- 开源实用小巧的Android引导蒙版(浮层)库
- sass学习三--控制指令和表达式
- 读书笔记∣元数据:用数据的数据管理你的世界 Ch.1-2
- mysql 数据字典
- Java虚拟机的内存结构
- Jesus Is Here HDU
- 看IMEI和IMSI
- Hadoop 2.6.0 FileSplit和InputSplit和FileInputFormat