簡單實現下載功能--jsp,servlet

剛到公司,前天給個人第一個小任務,雖然只是作個簡單的下載功能。java

但一開始仍是花了很多時間去理解導師的意思,並且還要用到一些公司的庫類,花了一個下午才作好。web

 

大概有三種方法多線程

1.直接用a標籤跳轉到指定位置的目標文件。  <a href="doc/help.txt" /></a>(文件是位於doc文件夾下help.txt)(不推薦這種方法,並且這跳轉會直接打開文件)app

2.jsp跳轉而後直接在jsp裏面寫下載功能jsp

<a href="download.jsp" /></a>網站

在download.jsp中使用字節流的方法提供下載編碼

 <%@page language="java" contentType="application/x-msdownload"  pageEncoding="gb2312"%><%
    //關於文件下載時採用文件流輸出的方式處理:
    //加上response.reset(),而且全部的%>後面不要換行,包括最後一個

    response.reset();//能夠加也能夠不加
    response.setContentType("application/x-download");
    String filedownload = "想辦法找到要提供下載的文件的物理路徑+文件名";
    String filedisplay = "給用戶提供的下載文件名";
    filedisplay = URLEncoder.encode(filedisplay,"UTF-8");
    response.addHeader("Content-Disposition","attachment;filename=" + filedisplay);

    OutputStream outp = null;
    FileInputStream in = null;
    try
    {
        outp = response.getOutputStream();
        in = new FileInputStream(filenamedownload);

        byte[] b = new byte[1024];
        int i = 0;

        while((i = in.read(b)) > 0)
        {
            outp.write(b, 0, i);
        }
        outp.flush();
    }
    catch(Exception e)
    {
        System.out.println("Error!");
        e.printStackTrace();
    }
    finally
    {
        if(in != null)
        {
            in.close();
            in = null;
        }
        if(outp != null)
        {
            outp.close();
            outp = null;
        }
    }
%>url

我以爲這種方法對於網站只有少許下載要求時能夠使用線程

3使用servlet來控制下載code

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<servlet>
<servlet-name>DownloadFile</servlet-name>
<servlet-class>com.suntek.web.servlet.DownloadServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>DownloadFile</servlet-name>
<url-pattern>/downloadfile</url-pattern>
</servlet-mapping>


</web-app>

 

servlet

package com.suntek.web.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DownloadServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
OutputStream o = response.getOutputStream();
byte b[] = new byte[1024];
// the file to download.
File fileLoad = new File("F:/", "Eula.txt");
// the dialogbox of download file.
response.setHeader("Content-disposition", "attachment;filename="
+ "help.txt");
// set the MIME type.
response.setContentType("text/plain");
// get the file length.
long fileLength = fileLoad.length();
String length = String.valueOf(fileLength);
response.setHeader("Content_Length", length);
// download the file.
FileInputStream in = new FileInputStream(fileLoad);
int n = 0;
while ((n = in.read(b)) != -1) {
o.write(b, 0, n);
}
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
doGet(request, response);
}
}

 在此servlet裏面使用了硬編碼的方式把文件的路徑匹配上去,在實際中要實現動態下載,就從路徑方面下手,並且注意,文件的後綴名必定要設置成功,也就是

response.setContentType("text/plain");

來設置下載的文件默認用什麼軟件打開,contenttype網上有列表,能夠本身搜索

 

 

下一篇應該會寫多線程下載跟實現斷點下載(恰好作到這個下載功能,就把這塊的知識都順便學學吧)

相關文章
相關標籤/搜索