在 Java 中使用 Base64 编码来自 URL 的图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24417699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Encode image from URL in Base64 in Java
提问by skunk a
I want to encode and save from an URL images in Base64. I found several example doing the encoding from a local file but not from an URL. Is there a possibility to do that?
我想对 Base64 中的 URL 图像进行编码和保存。我发现了几个从本地文件而不是从 URL 进行编码的示例。有没有可能做到这一点?
I tried something like that, but unsuccessfully. Any clue,help? Thanks for your answer.
我尝试过类似的事情,但没有成功。任何线索,帮助?感谢您的回答。
public static void main(String[] args) {
String imageUrl = "http://www.avajava.com/images/avajavalogo.jpg";
String destinationFile = "image.jpg";
try {
// Reading a Image file from file system
URL url = new URL(imageUrl);
InputStream is = url.openStream();
FileInputStream imageInFile = new FileInputStream(is.toString());
byte imageData[] = new byte[2048];
imageInFile.read(imageData);
// Converting Image byte array into Base64 String
String imageDataString = encodeImage(imageData);
System.out.println("imageDataString : " + imageDataString);
System.out.println("Image Successfully Manipulated!");
} catch (FileNotFoundException e) {
System.out.println("Image not found" + e);
} catch (IOException ioe) {
System.out.println("Exception while reading the Image " + ioe);
}
}
/**
* Encodes the byte array into base64 string
*
* @param imageByteArray - byte array
* @return String a {@link java.lang.String}
*/
public static String encodeImage(byte[] imageByteArray) {
return Base64.encodeBase64URLSafeString(imageByteArray);
}
采纳答案by milanboruvka
You should not use FileInputStream.
您不应该使用 FileInputStream。
Use something like:
使用类似的东西:
URL url = new URL(imageUrl);
BufferedInputStream bis = new BufferedInputStream(url.openConnection().getInputStream());
Also you need to read data in loop until you read all bytes of image.
您还需要循环读取数据,直到读取图像的所有字节。
回答by Jesper
Do you understand what this line does?
你明白这条线的作用吗?
FileInputStream imageInFile = new FileInputStream(is.toString());
First it calls toString
on an InputStream
object. That will result in a string that looks something like: InputStream@23e5aa
. Then it tries to open a file with that name. You don't want to read a file named InputStream@23e5aa
, so this is totally wrong.
首先它调用toString
一个InputStream
对象。这将导致看起来像一个字符串:InputStream@23e5aa
。然后它尝试打开具有该名称的文件。您不想读取名为 的文件InputStream@23e5aa
,因此这是完全错误的。
What you want to do instead is read all the bytes in the original InputStream is
into a byte array. How to do that is explained in the answers to this question:
您想要做的是将原始文件中的所有字节读InputStream is
入一个字节数组。在这个问题的答案中解释了如何做到这一点:
回答by Ketan Ramani
Try this function by passing image url in parameter.
通过在参数中传递图像 url 来尝试此功能。
private String getByteArrayFromImageURL(String url) {
try {
URL imageUrl = new URL(url);
URLConnection ucon = imageUrl.openConnection();
InputStream is = ucon.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int read = 0;
while ((read = is.read(buffer, 0, buffer.length)) != -1) {
baos.write(buffer, 0, read);
}
baos.flush();
return Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
} catch (Exception e) {
Log.d("Error", e.toString());
}
return null;
}
回答by shriram
Following code converts image into the base64 string:
以下代码将图像转换为 base64 字符串:
public String getBase64EncodedImage(String imageURL) throws IOException {
java.net.URL url = new java.net.URL(imageURL);
InputStream is = url.openStream();
byte[] bytes = org.apache.commons.io.IOUtils.toByteArray(is);
return Base64.encodeBase64String(bytes);
}
P.S. Above code uses commons-io
as a dependency.
PS 以上代码commons-io
用作依赖项。
回答by HARI HARAN
/**
*
* @param url - web url
* @return - Base64 String
* Method used to Convert URL to Base64 String
*/
public String convertUrlToBase64(String url) {
URL newurl;
Bitmap bitmap;
String base64 = "";
try {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
newurl = new URL(url);
bitmap = BitmapFactory.decodeStream(newurl.openConnection().getInputStream());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
base64 = Base64.encodeToString(outputStream.toByteArray(), Base64.DEFAULT);
} catch (Exception e) {
e.printStackTrace();
}
return base64;
}