Java的Servlet调用服务器上的html文件直接在客户端打开

/**

*ThedoGetmethodoftheservlet.<br>

*

*Thismethodiscalledwhenaformhasitstagvaluemethodequalstoget.

*

*@paramrequesttherequestsendbytheclienttotheserver

*@paramresponsetheresponsesendbytheservertotheclient

*@throwsServletExceptionifanerroroccurred

*@throwsIOExceptionifanerroroccurred

*/

@SuppressWarnings("unchecked")

publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

service(request,response);

}

/**

*ThedoPostmethodoftheservlet.<br>

*

*Thismethodiscalledwhenaformhasitstagvaluemethodequalstopost.

*

*@paramrequesttherequestsendbytheclienttotheserver

*@paramresponsetheresponsesendbytheservertotheclient

*@throwsServletExceptionifanerroroccurred

*@throwsIOExceptionifanerroroccurred

*/

publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

service(request,response);

}

publicvoidservice(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

//TODOAuto-generatedmethodstub

request.setCharacterEncoding("UTF-8");

response.setCharacterEncoding("UTF-8");

Stringfile_add=request.getParameter("FileAddress");

OutputStreamoutStream=response.getOutputStream();

try{

FileInputStreamfip=newFileInputStream(file_add);

byte[]buffer=newbyte[1024];//建立缓冲区

intlen;

while((len=fip.read(buffer))!=-1){

outStream.write(buffer,0,len);

}

fip.close();

outStream.close();

//关闭输入流,释放系统资源

}catch(Exceptione){

System.out.println(e.getStackTrace());

}

}

相关推荐