簡體   English   中英

如何將Http url(圖像URL)轉換為字節緩沖區或字節?

[英]How to convert Http url (image URL) to byte buffer or in byte?

我嘗試了一些將URL圖像轉換為字節緩沖區的功能,但是它不起作用。 它顯示IOException錯誤

String imagePath = "https://www.clearias.com/up/UPSC-Civil-Services-Mains-Exam-2018-Timetable-V1.png";

嘗試1:

URL u = new URL(imagePath);
int contentLength = u.openConnection().getContentLength();
InputStream openStream = u.openStream();
byte[] binaryData = new byte[contentLength];
openStream.read(binaryData);
ByteBuffer imageBytes = ByteBuffer.wrap(openStream);

當我將openStream包裝到ByteBuffer時顯示此錯誤

“類型為ByteBuffer的方法wrap(byte [])不適用於參數(InputStream)”

嘗試2:

    URL url = new URL(imagePath);
    ByteArrayOutputStream output = new ByteArrayOutputStream();

    try (InputStream inputStream = url.openStream()) {
        int n = 0;    
        byte[] buffer = new byte[1024];    
        while (-1 != (n = inputStream.read(buffer))) {
            output.write(buffer, 0, n);
        }
    }
    byte[] img = output.toByteArray();    
    ByteBuffer imageBytes = ByteBuffer.wrap(img);    

我也嘗試過此功能,但顯示此錯誤:

java.io.IOException:服務器返回URL的HTTP響應代碼:403: https : //www.clearias.com/up/UPSC-Civil-Services-Mains-Exam-2018-Timetable-V1.png

嘗試3:另一個是

byte[] img = Base64.encodeBase64(IOUtils.toByteArray((new URL(imagePath)).openStream()), true);    

這行也給我錯誤

HTTP錯誤403表示服務器已完全理解您請求此圖像並下載它的意圖。 但是,它估計您無權這樣做,不是因為編程錯誤,而是因為您不應該這樣做。

通常,得出的結論是不可能僅從其URL獲得此圖像,並且可能需要在請求旁邊提供某種形式的身份驗證,以向服務器證明應允許您獲取該圖像。

但是很明顯,僅通過在瀏覽器中復制/粘貼URL,就可以確定服務器在“正常”條件下無條件發出圖像。 服務器僅在由Java程序發出請求時才拒絕該請求(我也沒有測試其他技術)。剩下的問題是,服務器如何分辨該請求是由Java程序發出的? 或者,更籠統地說,服務器如何決定您是否有權提出此請求?

從理論上講,我們無法猜測每個人的意圖,但是對於希望拒絕來自特定技術的請求的HTTP服務器,通常會基於請求的User-Agent HTTP標頭執行此操作。 因此,我決定修改Java默認發送的User-Agent,並假裝請求是由Firefox完成的(非常幼稚的方式)

這是代碼:

URL url = new URL(imagePath);
ByteArrayOutputStream output = new ByteArrayOutputStream();
URLConnection conn = url.openConnection();
conn.setRequestProperty("User-Agent", "Firefox");

try (InputStream inputStream = conn.getInputStream()) {
  int n = 0;
  byte[] buffer = new byte[1024];
  while (-1 != (n = inputStream.read(buffer))) {
    output.write(buffer, 0, n);
  }
}
byte[] img = output.toByteArray();
ByteBuffer imageBytes = ByteBuffer.wrap(img);

有用。

我想網址有問題。

使用Apache commons-io Apache commons-io

下面是示例代碼:

import org.apache.commons.io.IOUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;

public class DownloadImage {
    public static void main(String[] args) {

        try {
            URL url = new URL("https://en.wikipedia.org/wiki/Car#/media/File:401_Gridlock.jpg");
            System.out.println(Arrays.toString(downloadFile(url)));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    private static byte[] downloadFile(URL url) {
        try {
            URLConnection conn = url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(5000);
            conn.connect();

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            IOUtils.copy(conn.getInputStream(), baos);

            return baos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

輸出: [..., 47, 98, 111, 100, 121, 62, 10, 60, 47, 104, 116, 109, 108, 62, 10]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM