一个文件下载的Servlet

public   class  DownloadServlet  extends  HttpServlet {
    
private  String contentType  =   " application/x-msdownload " ;
    
private  String enc  =   " utf-8 " ;
    
private  String fileRoot  =   "" ;


    
/**
     * 初始化contentType,enc,fileRoot
     
*/
    
public   void  init(ServletConfig config)  throws  ServletException {
        String tempStr 
=  config.getInitParameter( " contentType " );
        
if  (tempStr  !=   null   &&   ! tempStr.equals( "" )) {
            contentType 
=  tempStr;
        }
        tempStr 
=  config.getInitParameter( " enc " );
        
if  (tempStr  !=   null   &&   ! tempStr.equals( "" )) {
            enc 
=  tempStr;
        }
        tempStr 
=  config.getInitParameter( " fileRoot " );
        
if  (tempStr  !=   null   &&   ! tempStr.equals( "" )) {
            fileRoot 
=  tempStr;
        }
    }

    
protected   void  doGet(HttpServletRequest request, HttpServletResponse response)  throws  ServletException, IOException {
        String filepath 
=  request.getParameter( " filepath " );
        String fullFilePath 
=  fileRoot  +  filepath;
        
/* 读取文件 */
        File file 
=   new  File(fullFilePath);
        
/* 如果文件存在 */
        
if  (file.exists()) {
            String filename 
=  URLEncoder.encode(file.getName(), enc);
            response.reset();
            response.setContentType(contentType);
            response.addHeader(
" Content-Disposition " " attachment; filename=/ ""  + filename +  " / "" );
            
int  fileLength  =  ( int ) file.length();
            response.setContentLength(fileLength);
            
/* 如果文件长度大于0 */
            
if  (fileLength  !=   0 ) {
                
/* 创建输入流 */
                InputStream inStream 
=   new  FileInputStream(file);
                
byte [] buf  =   new   byte [ 4096 ];
                
/* 创建输出流 */
                ServletOutputStream servletOS 
=  response.getOutputStream();
                
int  readLength;
                
while  (((readLength  =  inStream.read(buf))  !=   - 1 )) {
                    servletOS.write(buf, 
0 , readLength);
                }
                inStream.close();
                servletOS.flush();
                servletOS.close();
            }
        }
    }

web.xml
     < servlet >
        
< servlet-name > download </ servlet-name >
        
< servlet-class > org.mstar.servlet.DownloadServlet </ servlet-class >
        
< init-param >
            
< param-name > fileRoot </ param-name >
            
< param-value > d:/temp </ param-value >
        
</ init-param >
        
< init-param >
            
< param-name > contentType </ param-name >
            
< param-value > application/x-msdownload </ param-value >
        
</ init-param >
        
< init-param >
            
< param-name > enc </ param-name >
            
< param-value > utf-8 </ param-value >
        
</ init-param >
    
</ servlet >
    
< servlet-mapping >
        
< servlet-name > download </ servlet-name >
        
< url-pattern > /down </ url-pattern >
    
</ servlet-mapping >
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值