salut j'essaye de télécharger l'image de la page Web. J'essaie de télécharger l'image à partir de la page d'accueil 'http://www.yahoo.com'. Merci de me dire comment passer "http://www.yahoo.com" en entrée. Et en ouvrant cette page Web, comment récupérer une image de cette page. Merci de me donner le code Java) pour récupérer l’image de la page Web.
(throws IOException)
Image image = null;
try {
URL url = new URL("http://www.yahoo.com/image_to_read.jpg");
image = ImageIO.read(url);
} catch (IOException e) {
}
Voir javax.imageio
package pour plus d'informations. Cela utilise l'image AWT. Sinon, vous pourriez faire:
URL url = new URL("http://www.yahoo.com/image_to_read.jpg");
InputStream in = new BufferedInputStream(url.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while (-1!=(n=in.read(buf)))
{
out.write(buf, 0, n);
}
out.close();
in.close();
byte[] response = out.toByteArray();
Et vous voudrez peut-être alors sauvegarder l'image alors faites:
FileOutputStream fos = new FileOutputStream("C://borrowed_image.jpg");
fos.write(response);
fos.close();
Si vous souhaitez enregistrer l'image et que vous connaissez son URL, procédez comme suit:
try(InputStream in = new URL("http://example.com/image.jpg").openStream()){
Files.copy(in, Paths.get("C:/File/To/Save/To/image.jpg"));
}
Vous devrez également gérer les IOException
s qui peuvent être lancés.
Cela fonctionne pour moi:
URL url = new URL("http://upload.wikimedia.org/wikipedia/commons/9/9c/Image-Porkeri_001.jpg");
InputStream in = new BufferedInputStream(url.openStream());
OutputStream out = new BufferedOutputStream(new FileOutputStream("Image-Porkeri_001.jpg"));
for ( int i; (i = in.read()) != -1; ) {
out.write(i);
}
in.close();
out.close();
// Do you want to download an image?
// But are u denied access?
// well here is the solution.
public static void DownloadImage(String search, String path) {
// This will get input data from the server
InputStream inputStream = null;
// This will read the data from the server;
OutputStream outputStream = null;
try {
// This will open a socket from client to server
URL url = new URL(search);
// This user agent is for if the server wants real humans to visit
String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36";
// This socket type will allow to set user_agent
URLConnection con = url.openConnection();
// Setting the user agent
con.setRequestProperty("User-Agent", USER_AGENT);
// Requesting input data from server
inputStream = con.getInputStream();
// Open local file writer
outputStream = new FileOutputStream(path);
// Limiting byte written to file per loop
byte[] buffer = new byte[2048];
// Increments file size
int length;
// Looping until server finishes
while ((length = inputStream.read(buffer)) != -1) {
// Writing data
outputStream.write(buffer, 0, length);
}
} catch (Exception ex) {
Logger.getLogger(WebCrawler.class.getName()).log(Level.SEVERE, null, ex);
}
// closing used resources
// The computer will not be able to use the image
// This is a must
outputStream.close();
inputStream.close();
}
Le code suivant télécharge une image d'un lien direct vers le disque dans le répertoire du projet. Notez également qu’il utilise try-with-resources
.
import Java.io.BufferedInputStream;
import Java.io.BufferedOutputStream;
import Java.io.File;
import Java.io.FileNotFoundException;
import Java.io.FileOutputStream;
import Java.io.IOException;
import Java.io.InputStream;
import Java.io.OutputStream;
import Java.net.MalformedURLException;
import Java.net.URL;
import org.Apache.commons.io.FilenameUtils;
public class ImageDownloader
{
public static void main(String[] arguments) throws IOException
{
downloadImage("https://upload.wikimedia.org/wikipedia/commons/7/73/Lion_waiting_in_Namibia.jpg",
new File("").getAbsolutePath());
}
public static void downloadImage(String sourceUrl, String targetDirectory)
throws MalformedURLException, IOException, FileNotFoundException
{
URL imageUrl = new URL(sourceUrl);
try (InputStream imageReader = new BufferedInputStream(
imageUrl.openStream());
OutputStream imageWriter = new BufferedOutputStream(
new FileOutputStream(targetDirectory + File.separator
+ FilenameUtils.getName(sourceUrl)));)
{
int readByte;
while ((readByte = imageReader.read()) != -1)
{
imageWriter.write(readByte);
}
}
}
}