Java
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
@WebServlet(value = "/downloadservlet")
public class DownloadServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
try{
response.setHeader("content-type","application/jar");//默认保存类型
response.addHeader("content-disposition","attachment;filename=4.jar");//默认保存名称
//文件所在路径
String savepath=request.getServletContext().getRealPath("/WEB-INF/lib");
File f=new File(savepath+"/4.jar");
FileInputStream fs=new FileInputStream(f);
int read=0;
byte[] bytes=new byte[1024];
OutputStream os=response.getOutputStream();
while((read=fs.read(bytes))!=-1){
os.write(bytes,0,read);
System.out.println(read);
}
os.flush();
os.close();
fs.close();
System.out.println("传输完成");
}catch (Exception e){
e.printStackTrace();
}
}
}