SoFunction
Updated on 2025-04-09

How to download server files by JSP

This article describes the method of downloading server files by JSP. Share it for your reference, as follows:

<%@page import=""%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:///TR/xhtml1/DTD/">
<%@page language="java" contentType="text/html; charset=UTF-8"%>
<%@page import="" %>
<%@page import="" %>
<%@ include file="/"%>
<%
  boolean loadFlag = false;
  ServletOutputStream ou = null;
  FileInputStream fis = null;
  try{  
    Context context=(Context)("context");
    String accountFilePath = (String)("accountFilePath");
    //String accountFilePath ="E:/ComeBack/Acc_BOS_CM/WebContent/upload/product/";
    //String accountFileName = (String)("accountFileName");
    String displayFileName = (String)("displayFileName");
    //String displayFileName ="";
    String fullPath =accountFilePath;
    ();
      ("charset=UTF-8");
    ("text/HTML");
    //("application/-excel");    
    String filename = new String(("gb2312"), "ISO8859-1");
    ("Content-Disposition","attachment; filename=" + filename);
    //Solve the problem that HTTPS cannot be downloaded    ("Cache-Control","public");    
    ou = ();
    File file = new File(fullPath);
    fis = new FileInputStream(file);    
    try {
      if (fis != null) {
        int filelen = ();
        byte a[] = new byte[filelen];
        (a);
        (a);
        ();
      }      
      loadFlag=true;
    } catch (Exception e) {
      ("Excel is not exist!");
    }
    out = ();
  }catch(Exception e){
    ();
  }finally{
    try {
      ();
      ();
      ou = null;
      ();
    } catch (Exception e) {
      // TODO automatically generates catch block      ();
    }
  }
%>

I hope this article will be helpful to everyone's JSP programming.