获取图片文件

来源:互联网 发布:无约束优化问题 编辑:程序博客网 时间: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;

}

}

}

原创粉丝点击