使用HttpConnection从安卓下载图片得到410,但邮递员得到200



我在Android中下载图像时遇到了一个问题。

有问题的图像链接:https://via.placeholder.com/150/92c952

我可以使用邮递员成功下载图片(200(。但当我在Android中使用HttpConnection进行编码时,它会用错误状态代码410来响应我。(然后触发FileNotFountedException。(

下面是我的代码,


URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
int responseCode = httpConn.getResponseCode();
// always check HTTP response code first
if (responseCode == HttpURLConnection.HTTP_OK) {
String fileName = "";
String disposition = httpConn.getHeaderField("Content-Disposition");
String contentType = httpConn.getContentType();
int contentLength = httpConn.getContentLength();
if (disposition != null) {
// extracts file name from header field
int index = disposition.indexOf("filename=");
if (index > 0) {
fileName = disposition.substring(index + 10,
disposition.length() - 1);
}
} else {
// extracts file name from URL
fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1,
fileURL.length());
}
System.out.println("Content-Type = " + contentType);
System.out.println("Content-Disposition = " + disposition);
System.out.println("Content-Length = " + contentLength);
System.out.println("fileName = " + fileName);
// opens input stream from the HTTP connection
InputStream inputStream = httpConn.getInputStream();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int bytesRead = -1;
byte[] buffer = new byte[1024];
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
inputStream.close();
System.out.println("File downloaded");
httpConn.disconnect();
return outputStream.toByteArray();
} else {
httpConn.disconnect();
throw new RuntimeException("Error code: " + responseCode);
}

我的代码可以成功地从Imgur下载图像,但对于这个链接,它失败了。

请分享任何想法,我非常感谢!

原因很简单。

原因不在Java程序端,而在服务器端。

服务器通过响应HTTP 410 Gone拒绝您的连接。

为了避免服务器的行为,只需在执行实际请求之前在请求中设置任何流行的User-Agent字符串即可。

HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpCon.setRequestProperty(
"User-Agent",
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X; ja-JP-mac; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"
);
int responseCode = httpConn.getResponseCode();
...

如果您想下载图像并在ImageView上显示,您可以使用glide或picasso。它很容易实现,在滑翔时你可以只使用

Glide.with(context).load("https://via.placeholder.com/150/92c952").into(myImageView);

使用此方法从URL 下载图像

// DownloadImage AsyncTask
private class DownloadImage extends AsyncTask<String, Void, Bitmap> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Bitmap doInBackground(String... URL) {
String imageURL = URL[0];
Bitmap bitmap = null;
try {
// Download Image from URL
InputStream input = new java.net.URL(imageURL).openStream();
// Decode Bitmap
bitmap = BitmapFactory.decodeStream(input);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {

//将此位图设置为图像视图//现在使用这个位图将图像保存在内存中,我保存在内部内存中

if (result != null) {
File destination = new File(getActivity().getCacheDir(),
"profile" + ".jpg");
try {
destination.createNewFile();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
result.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();
FileOutputStream fos = new FileOutputStream(destination);
fos.write(bitmapdata);
fos.flush();
fos.close();
selectedFile = destination;
} catch (IOException e) {
e.printStackTrace();
}
}

}
}

现在像一样调用这个方法

new DownloadImage().execute("https://via.placeholder.com/150/92c952");

相关内容

  • 没有找到相关文章

最新更新