深入體驗JavaWeb開發內幕——圖片、聲音等檔案的下載原始碼及可能出現的問題
阿新 • • 發佈:2019-02-13
一個可以實現圖、聲音等檔案下載功能的servlet類:
MyServlet.java
package net.csdn.servlet; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { download1(response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } public void download1(HttpServletResponse response) throws IOException{ //獲取所要下載檔案的路徑 String path = this.getServletContext().getRealPath("/download/15.jpg"); String realPath = path.substring(path.lastIndexOf("\\")+1); //告訴瀏覽器是以下載的方法獲取到資源 response.setHeader("content-disposition","attachment; filename="+realPath); //獲取到所下載的資源 FileInputStream fis = new FileInputStream(path); int len = 0; byte [] buf = new byte[1024]; while((len=fis.read(buf))!=-1){ response.getOutputStream().write(buf,0,len); } }
download.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>download.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <a href = "MyServlet">點選下載</a> </body> </html>
這樣你就可以實現一個可以下載相關英文或字元名稱的檔案了,但是對於中文名稱的便會出現錯誤:
如:
將圖名稱改為”錯誤.jpg“時:
即:
public void download1(HttpServletResponse response) throws IOException{ //獲取所要下載檔案的路徑 String path = this.getServletContext().getRealPath("/download/錯誤.jpg"); String realPath = path.substring(path.lastIndexOf("\\")+1); //告訴瀏覽器是以下載的方法獲取到資源 response.setHeader("content-disposition","attachment; filename="+realPath); //獲取到所下載的資源 FileInputStream fis = new FileInputStream(path); int len = 0; byte [] buf = new byte[1024]; while((len=fis.read(buf))!=-1){ response.getOutputStream().write(buf,0,len); }
顯示為:
會出現不能解析中文名稱的問題:
這裡只需:
public void download1(HttpServletResponse response) throws IOException{
//獲取所要下載檔案的路徑
String path = this.getServletContext().getRealPath("/download/錯誤.jpg");
String realPath = path.substring(path.lastIndexOf("\\")+1);
//告訴瀏覽器是以下載的方法獲取到資源
//告訴瀏覽器以此種編碼來解析URLEncoder.encode(realPath, "utf-8"))
response.setHeader("content-disposition","attachment; filename="+URLEncoder.encode(realPath, "utf-8"));
//獲取到所下載的資源
FileInputStream fis = new FileInputStream(path);
int len = 0;
byte [] buf = new byte[1024];
while((len=fis.read(buf))!=-1){
response.getOutputStream().write(buf,0,len);
}
}
出現上述問題的原因是因為:
在download。html檔案中規定了顯示方式:
所以應與之保持一致故採用此方法。
好了到這裡,你已經可以自己去實現一個檔案下載功能的JavaWeb程式了,試一試吧!