熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Listfiles.jsp-----DownloaServlet

2022-06-13   來源: Java核心技術 

  [java]
package comhbsiservlet; 
 
import javaioFile; 
import javaioFileInputStream; 
import javaioIOException; 
import javaioOutputStream; 
import URLEncoder; 
 
import javaxservletServletException; 
import javaxservlethttpHttpServlet; 
import javaxservlethttpHttpServletRequest; 
import javaxservlethttpHttpServletResponse; 
 
public class DownloadServlet extends HttpServlet { 
 
    public void doGet(HttpServletRequest request HttpServletResponse response) 
            throws ServletException IOException { 
         
        String filename = requestgetParameter(filename);  
         
        filename = new String(filenamegetBytes(iso)UTF); 
        Systemoutprintln(filename); 
         
        //找到該文件所在的文件夾位置 
        //String savepath = thisgetFileSavePath(filename); //現在的文件名是UUID 
        String savepath = thisgetFileSavePath(thisgetRealName(filename)); 
         
        File f = new File(savepath+\\+filename); 
         
        if(!fexists()){ 
            requestsetAttribute(message下載的資源不存在); 
            requestgetRequestDispatcher(/messagejsp)forward(request response); 
             
        } 
         
        responsesetHeader(contentdispositionattachment;filename=+URLEncoderencode(thisgetRealName(filename)UTF)); 
         
        FileInputStream in = new FileInputStream(f); 
        byte[] buf = new byte[]; 
        int len=
         
        OutputStream out = responsegetOutputStream(); 
        while((len=inread(buf))>){ 
            outwrite(buflen); 
        } 
        inclose(); 
         
         
         
         
    } 
    public String getFileSavePath(String filename){ 
         
        int dir = filenamehashCode()&xf; 
        int dir = (filenamehashCode()>>)&xf; 
        String savepath = thisgetServletContext()getRealPath(/WEBINF/upload)+\\+dir+\\+dir
         
         
        return savepath; 
    } 
    public String getRealName(String filename){ 
        String realName = filenamesubstring(filenameindexOf(_)+); 
        return realName; 
    } 
     
 
    public void doPost(HttpServletRequest request HttpServletResponse response) 
            throws ServletException IOException { 
 
        doGet(request response); 
    } 
 


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26051.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.