java获取远程网络图片文件流、压缩保存到本地
1.获取远程网路的图片
/**
* 根据地址获得数据的字节流
*
* @param strurl
* 网络连接地址
* @return
*/
public static byte[] getimagefromnetbyurl(string strurl) {
try {
url url = new url(strurl);
httpurlconnection conn = (httpurlconnection) url.openconnection();
conn.setrequestmethod("get");
conn.setconnecttimeout(5 * 1000);
inputstream instream = conn.getinputstream();// 通过输入流获取图片数据
byte[] btimg = readinputstream(instream);// 得到图片的二进制数据
return btimg;
} catch (exception e) {
e.printstacktrace();
}
return null;
}
/**
* 根据地址获得数据的字节流
*
* @param strurl
* 本地连接地址
* @return
*/
public static byte[] getimagefromlocalbyurl(string strurl) {
try {
file imagefile = new file(strurl);
inputstream instream = new fileinputstream(imagefile);
byte[] btimg = readinputstream(instream);// 得到图片的二进制数据
return btimg;
} catch (exception e) {
e.printstacktrace();
}
return null;
}
/**
* 从输入流中获取数据
*
* @param instream
* 输入流
* @return
* @throws exception
*/
public static byte[] readinputstream(inputstream instream) throws exception {
bytearrayoutputstream outstream = new bytearrayoutputstream();
byte[] buffer = new byte[10240];
int len = 0;
while ((len = instream.read(buffer)) != -1) {
outstream.write(buffer, 0, len);
}
instream.close();
return outstream.tobytearray();
}
2.将网络读取的文件流转成本地文件
byte[] btimg1 = imageutil.getimagefromnetbyurl(fileurl1);
if (null != btimg1 && btimg1.length > 0) {
logger.debug("读取到:" + btimg1.length + " 字节");
imageutil.writeimagetodisk(btimg1, filezipurl1);
} else {
logger.debug("没有从该连接获得内容");
}
byte[] btimg2 = imageutil.getimagefromnetbyurl(fileurl2);
if (null != btimg2 && btimg2.length > 0) {
logger.debug("读取到:" + btimg2.length + " 字节");
imageutil.writeimagetodisk(btimg2, filezipurl2);
} else {
logger.debug("没有从该连接获得内容");
}
/**
* 将图片写入到磁盘
*
* @param img
* 图片数据流
* @param filename
* 文件保存时的名称
*/
public static void writeimagetodisk(byte[] img, string zipimageurl) {
try {
file file = new file(zipimageurl);
fileoutputstream fops = new fileoutputstream(file);
fops.write(img);
fops.flush();
fops.close();
system.out.println("图片已经写入"+zipimageurl);
} catch (exception e) {
e.printstacktrace();
}
}
3、压缩本地图片
import java.io.*;
import java.util.date;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.imageio;
import com.sun.image.codec.jpeg.*;
/**
* 图片压缩处理
*/
public class imgcompress {
private image img;
private int width;
private int height;
/**
* 构造函数
*/
public imgcompress(string filename) throws ioexception {
file file = new file(filename);// 读入文件
img = imageio.read(file); // 构造image对象
width = img.getwidth(null); // 得到源图宽
height = img.getheight(null); // 得到源图长
}
/**
* 按照宽度还是高度进行压缩
* @param w int 最大宽度
* @param h int 最大高度
*/
public void resizefix(int w, int h) throws ioexception {
if (width / height > w / h) {
resizebywidth(w);
} else {
resizebyheight(h);
}
}
/**
* 以宽度为基准,等比例放缩图片
* @param w int 新宽度
*/
public void resizebywidth(int w) throws ioexception {
int h = (int) (height * w / width);
resize(w, h);
}
/**
* 以高度为基准,等比例缩放图片
* @param h int 新高度
*/
public void resizebyheight(int h) throws ioexception {
int w = (int) (width * h / height);
resize(w, h);
}
/**
* 强制压缩/放大图片到固定的大小
* @param w int 新宽度
* @param h int 新高度
*/
public void resize(int w, int h) throws ioexception {
// scale_smooth 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢
bufferedimage image = new bufferedimage(w, h,bufferedimage.type_int_rgb );
image.getgraphics().drawimage(img, 0, 0, w, h, null); // 绘制缩小后的图
file destfile = new file("c:/users/administrator/desktop/147.jpg");
fileoutputstream out = new fileoutputstream(destfile); // 输出到文件流
// 可以正常实现bmp、png、gif转jpg
jpegimageencoder encoder = jpegcodec.createjpegencoder(out);
encoder.encode(image); // jpeg编码
out.close();
}
@suppresswarnings("deprecation")
public static void main(string[] args) throws exception {
system.out.println("开始:" + new date().tolocalestring());
imgcompress imgcom = new imgcompress("c:/users/administrator/desktop/1479209533362.jpg");
imgcom.resizefix(285, 380);
system.out.println("结束:" + new date().tolocalestring());
}
}
完~