- 說明: 本次的任務是將等待下載的文件放在非Web目錄下(在web.xml中配置),使客戶端無法直接訪問待下載 文件。然后通過一個servlet進行中轉,如果帶下載的文件存在,通過FileInputStream對象打開這個文件,并且通過ServletOutputStream對象將待下載的文件按字節流的方式輸出到客戶端。若果待下載的文件是jpg,則直接在瀏覽器顯示該圖片,還有一個功能就是列出web.xml目錄下指定目錄中的所有文件。只需要直接單擊文件就可下載或者顯示圖片、
- 代碼 :?
import java.io.*;
import java.text.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.*;
public class Download extends HttpServlet
{public void downLoad(File file,HttpServletResponse response)throws IOException{if(file.exists()){if(file.getName().length()-file.getName().lastIndexOf(".jpg")==4){response.setContentType("image/jpeg");response.addHeader("Content-Disposition","filename="+URLEncoder.encode(file.getName(),"UTF-8"));}else{response.setContentType("application/octet-stream");response.addHeader("Content-Disposition","attachment;filename="+URLEncoder.encode(file.getName(),"UTF-8"));}response.addHeader("Content-Length",String.valueOf(file.length()));InputStream in=new FileInputStream(file);byte[] b=new byte[8192];int count=0;ServletOutputStream out=response.getOutputStream();while((count=in.read(b))>0){out.write(b,0,count);}in.close();out.close();}}private void listDir(File dir,HttpServletResponse response)throws IOException{response.setContentType("text/html;charset=UTF-8");PrintWriter out=response.getWriter();for(File file:dir.listFiles()){if(file.isFile()){out.print("<a href='Download?filename="+URLEncoder.encode(file.getName(),"UTF-8")+"'>");out.println(file.getName()+"</a><br />");}}}public void service(HttpServletRequest request,HttpServletResponse response)throws ServletException ,IOException{response.setContentType("text/html;charset=UTF-8");String path=this.getServletConfig().getInitParameter("path");String filename=request.getParameter("filename");File dir=new File(path);if(dir.exists()){if(filename!=null){filename=dir.getPath()+File.separator+filename;System.out.println(filename);File downLoadFile=new File(filename);downLoad(downLoadFile,response);}else{listDir(dir,response);}}PrintWriter out=response.getWriter();SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");out.println(dateFormat.format(new java.util.Date()));}}//總結//在下載文件時 必須設置 Content-Type和Content-Disposition 字段 其中Content-Type字段的值是application/octet-stream表示下載二進制文件
//Content-Disposition 的值為attachment;filename= 其中attachment表示下載的是附件 瀏覽器會彈出一個下載對話框 如果不設置 要下載的文件將在
//瀏覽器打開//
- 由于是初學servlet,這個小程序是通過記事本寫的。目的是為了了解servlet的配置過程,以更好的理解servlet。
這里介紹下方法:
在<Tomcat 安裝目錄>\webapps目錄下新建一個mydemo目錄在<Tomcat 安裝目錄>\webapps\mydemo目錄下新建一個WEB-INF目錄在<Tomcat 安裝目錄>\webapps\mydemo\WEB-INF目錄下新建一個classes目錄
在<Tomcat 安裝目錄>\webapps\mydemo\WEB-INF目錄中建立一個servlet類 DownLoad.java編譯 DownLoad.java javac -classpath .; servlet-api.jar -d WEB-INF\classes DownLoad.java 其中servlet-api.jar是servlet所依賴的一個庫 在<Tomcat 安裝目錄>\lib下可以找到配置servlet 在<Tomcat 安裝目錄>\WEB-INF目錄下新建一個web.xml文件<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0"metadata-complete="true"><servlet><servlet-name>Download</servlet-name><servlet-class>Download</servlet-class><init-param><param-name>path</param-name><param-value>C:\Users\min\Documents</param-value></init-param></servlet><servlet-mapping><servlet-name>Download</servlet-name><url-pattern>/Download</url-pattern></servlet-mapping>
</web-app>
之后打開tomcat服務 就可以通過http://localhost:8080/mydemo/Download訪問啦。
總結
以上是生活随笔為你收集整理的JavaWeb之Servlet学习-----实现文件动态下载功能 手写servlet 手动构建web程序的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。