Programming Tips - Java: Let user download a file (from a website)

Date: 2020oct4 Update: 2025sep30 Language: Java Q. Java: Let user download a file (from a website) A. It's the same in any programming language, just set Content-Disposition and Content-Description and the file will come to the user as a download instead of a webpage. In Java:
import org.apache.http.HttpResponse; import org.apache.http.HttpEntity; import org.apache.http.entity.EntityTemplate; static void doPage(HttpResponse response) { response.setHeader("Content-Disposition", "attachment; filename=hello.txt"); response.setHeader("Content-Description", "Text File"); HttpEntity entity = new EntityTemplate(producer); ((EntityTemplate)entity).setContentType("text/plain"); response.setEntity(entity); // ... }