Java 获取到第三方的二维码链接如何下载二维码图片
时间: 2024-06-13 17:06:42 浏览: 57
链接生成二维码并直接下载
根据提供的引用内容,无法确定具体的二维码链接获取方式。但是,一旦获取到了第三方的二维码链接,可以使用Java中的网络编程相关类库,例如HttpURLConnection或者HttpClient,来下载二维码图片。以下是一个使用HttpURLConnection下载图片的示例代码:
```java
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class DownloadQRCode {
public static void main(String[] args) {
String qrCodeUrl = "https://example.com/qrcode.png"; // 二维码图片链接
String savePath = "qrcode.png"; // 保存路径和文件名
try {
URL url = new URL(qrCodeUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
InputStream inputStream = conn.getInputStream(); byte[] getData = readInputStream(inputStream);
FileOutputStream fos = new FileOutputStream(savePath);
fos.write(getData);
if (fos != null) {
fos.close();
}
if (inputStream != null) {
inputStream.close();
}
System.out.println("二维码图片下载成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
public static byte[] readInputStream(InputStream inputStream) throws Exception {
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();
}
}
```
阅读全文