Java – How to download web page from internet?

A simple Java source code to download a web page from the internet. JavaDownloadWebPage.java package com.mkyong; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; public class JavaDownloadWebPage { public static void main(String[] args) throws IOException { String result = downloadWebPage("https://mkyong.com"); System.out.println(result); } private static String downloadWebPage(String url) throws IOException { StringBuilder …

Read more

Java – How to get all links from a web page?

A jsoup HTML parser example to show you how to parse and get all HTML hyperlinks from a web page: pom.xml <dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.12.1</version> </dependency> JsoupFindLinkSample.java package com.mkyong; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.IOException; import java.util.HashSet; import java.util.Set; public class JsoupFindLinkSample { public static void main(String[] args) throws IOException { …

Read more

Java – Server returned HTTP response code: 403 for URL

A simple Java program tries to download something from the internet: try (InputStream is = new URL(url).openStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is))) { while ((line = br.readLine()) != null) { result.append(line); } } But, some web servers return the following HTTP 403 errors? Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 …

Read more

wget on Mac OS X

By default, there is no wget on Mac OS X. $ wget http://www-us.apache.org/dist/tomcat/tomcat-8/v8.5.9/bin/apache-tomcat-8.5.9.tar.gz -bash: wget: command not found On Mac OS X, the equivalent of Linux’s wget is curl -O $ curl -O http://www-us.apache.org/dist/tomcat/tomcat-8/v8.5.9/bin/apache-tomcat-8.5.9.tar.gz % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 49 9112k 49 …

Read more

Download excel file from JAX-RS

In JAX-RS, for excel file, annotate the method with @Produces(“application/vnd.ms-excel”) : Put @Produces(“application/vnd.ms-excel”) on service method. Set “Content-Disposition” in Response header to prompt a download box. 1. Download Excel file in JAX-RS Full example to download an excel file from JAX-RS. import java.io.File; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; @Path("/excel") public …

Read more

Download image file from JAX-RS

In JAX-RS, for user to download an image file, annotate the method with @Produces(“image/image-type”) : Put @Produces(“image/png”) on service method, for “png” image. Set “Content-Disposition” in Response header to prompt a download box. Note For other image types, refer to this list of the image types 1. Download Image in JAX-RS Full example to download …

Read more

Download text file from JAX-RS

In JAX-RS, for user to download a file, annotate the method with @Produces(“text/plain”) : Put @Produces(“text/plain”) on service method, with a Response return type. It means the output is a text file. Set “Content-Disposition” in Response header to tell browser pop up a download box for user to download. 1. Download File in JAX-RS See …

Read more

Download pdf file from JAX-RS

In JAX-RS, for pdf file, annotate the method with @Produces(“application/pdf”) : Put @Produces(“application/pdf”) on service method. Set “Content-Disposition” in Response header to prompt a download box. 1. Download Pdf file in JAX-RS Full example to download a pdf file from JAX-RS. import java.io.File; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; @Path("/pdf") public …

Read more