packageservlet; 。IOException; 。PrintWriter; 。BufferedInputStream; 。BufferedOutputStream; 。OutputStream; 。File; 。FileInputStream; 。InputStream; rvlet。
ServletException; rvlet。ServletOutputStream; tp。HttpServlet; tp。HttpServletRequest; tp。HttpServletResponse; 。URLEncoder;publicclassDownLoadServletextendsHttpServlet{privatestaticfinalStringCONTENT_TYPE="text/html;charset=GBK";publicvoidinit()throwsServletException{}publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{StringfileName= tParameter("fileName");Filefile=newFile("E:/temp/"+fileName);//if(file。
exists()){//设置响应类型和响应头 tContentType("application/x-msdownload"); tContentLength((int)file。length()); tHeader("Content-Disposition","attachment;filename=\""+fileName+"\"");//读取文件InputStreaminputStream=newFileInputStream(file);BufferedInputStreambis=newBufferedInputStream(inputStream);byte[]bytes=newbyte[1024];ServletOutputStreamoutStream= tOutputStream();intreadLength=0;while((readLength= ad(bytes))!=-1){outStream。
write(bytes,0,readLength);}//释放资源 ose(); ose();outStream。flush(); ose();//}}jsp文件显示所有文件请选择你要下载的文件:fileList=newArrayList();Filefolder=newFile(path);if(folder。
exists()&& Directory()){File[]files= stFiles();for(File){if( File()){ d( tName());}}} tAttribute("fileList",fileList); tAttribute("fileList");%>">fileName%>}-->${fileName}我现在能连上servlet,但有错,会显示 。
FileNotFound :\temp\null(系统找不到指定的文件。)求高手指教,我文件的目录是E盘temp下,急急急。