最近做一個帶微信頭像的二維碼的功能,微信頭像是一個鏈接,所以先把微信頭像下載,臨時保存,生成二維碼
/**
* 文件下載工具類
*
* @author zhengxinghua
*/
public class DownFileUtils {
/**
* 下載鏈接圖片,臨時保存
* @param urlStr 鏈接圖片地址
* @param savePath 要保存的地址
* @return 文件
* @throws IOException
*/
public static File downLoadFromUrl(String urlStr, String savePath) throws IOException {
String fileName = System.currentTimeMillis() + ".jpg";
URL url = new URL(urlStr);
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 inputStream = conn.getInputStream();
//獲取自己數組
byte[] getData = readInputStream(inputStream);
//文件保存位置
File saveDir = new File(savePath);
if (!saveDir.exists()) {
saveDir.mkdir();
}
File file = new File(saveDir + File.separator + fileName);
FileOutputStream fos = new FileOutputStream(file);
fos.write(getData);
if (fos != null) {
fos.close();
}
if (inputStream != null) {
inputStream.close();
}
return file;
}
/**
* 從輸入流中獲取字節數組
*
* @param inputStream 輸入流
* @return 字節數組
* @throws IOException
*/
public static byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toByteArray();
}
}
接下來是生成二維碼,用的一個開源工具類,超級好用,還得到開源大佬的指點,特別鳴謝 http://hutool.mydoc.io/#text_319501
package com.gcyh.common.utils;
import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
/**
*二維碼生成工具類
*/
public class QrCodeUtils {
/**
* 生成文件類型的帶頭像二維碼
* @param urlPath 頭像鏈接路徑
* @return 文件類型
*/
public static MultipartFile generate(String urlPath) {
try {
URL url = new URL(urlPath);
URLConnection urlConnection = url.openConnection();
HttpURLConnection connection;
if (urlConnection instanceof HttpURLConnection) {
connection = (HttpURLConnection) urlConnection;
} else {
System.out.println("請輸入 URL 地址");
return null;
}
InputStream inputStream = connection.getInputStream();
Image read = ImageIO.read(inputStream);
Image im = read;
System.out.println(im);
QrConfig config = new QrConfig(300, 300);
config.setImg(im);
config.setErrorCorrection(ErrorCorrectionLevel.M);
BufferedImage generate = QrCodeUtil.generate("http://www.baidu.com/", config);
ByteArrayOutputStream bs = new ByteArrayOutputStream();
ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);
ImageIO.write(generate, "jpg", imOut);
InputStream is = new ByteArrayInputStream(bs.toByteArray());
MockMultipartFile file = new MockMultipartFile("123", is);
return file;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 測試
* 生成文件類型後,再反向生成圖片,看有沒有問題
* @param args
*/
public static void main(String[] args) {
try {
URL url = new URL("http://file2.youboy.com/e/2014/8/29/81/252464s.jpg");
URLConnection urlConnection = url.openConnection();
HttpURLConnection connection;
if (urlConnection instanceof HttpURLConnection) {
connection = (HttpURLConnection) urlConnection;
} else {
System.out.println("請輸入 URL 地址");
return;
}
InputStream inputStream = connection.getInputStream();
Image read = ImageIO.read(inputStream);
Image im = read;
System.out.println(im);
QrConfig config = new QrConfig(300, 300);
config.setImg(im);
config.setErrorCorrection(ErrorCorrectionLevel.M);
BufferedImage generate = QrCodeUtil.generate("http://www.baidu.com/", config);
ByteArrayOutputStream bs = new ByteArrayOutputStream();
ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);
ImageIO.write(generate, "jpg", imOut);
InputStream is = new ByteArrayInputStream(bs.toByteArray());
MockMultipartFile file = new MockMultipartFile("123", is);
InputStream fileInputStream = file.getInputStream();
FileUtil.writeFromStream(fileInputStream,FileUtil.file("d:/qrco33de.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}
}