用Servlets实现Web方式的文件下载

王朝other·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

用Servlets实现Web方式的文件下载

// Download.Java

import java.io.*;

import java.text.*;

import java.util.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.net.*;

/**

* The simplest download servlet.

*

* @author Huang Jian-chang

*/

public class Download extends HttpServlet {

public void doPost(HttpServletRequest request,

HttpServletResponse response)

throws IOException, ServletException

{

response.setContentType( "application/doc" ); // MIME type for pdf doc

response.setHeader("Content-disposition",

"attachment; filename=" +

"table.doc" );

String fileURL = "http://hjc/table.doc";

URL url=new URL(fileURL);

BufferedInputStream bis = new BufferedInputStream(url.openStream());

BufferedOutputStream bos = new BufferedOutputStream( response.getOutputStream());

byte[] buff = new byte[2048];

int bytesRead;

while (-1!=(bytesRead=(bis.read(buff,0,buff.length)))){

bos.write(buff,0,bytesRead);

}

if (bis!=null){

bis.close();

}

if (bos!=null){

bos.close();

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航