이 기사에서는 파일 다운로드를 구현하기위한 간단한 서블릿 방법에 대해 설명합니다. 다음과 같이 참조에 대해 공유하십시오.
public static void download (문자열 path, httpservletresponse 응답) {try {// path는 다운로드 할 파일의 경로를 나타냅니다. 파일 = 새 파일 (Path); // 파일 이름을 가져옵니다. 문자열 filename = file.getName (); // 파일의 접미사 이름을 가져옵니다. 문자열 ext = filename.substring (filename.lastIndexof ( ".") + 1) .toupperCase (); // 스트림 형식으로 파일을 다운로드합니다. inputStream fis = new bufferedInputStream (new FileInputStream (PATH)); 바이트 [] buffer = new Byte [fis.available ()]; fis.read (버퍼); fis.close (); // 응답 응답을 클리어. reset (); // 응답의 헤더 응답을 설정합니다 .addheader ( "content-disposition", "첨부 파일; filename =" + new String (filename.getBytes ())); response.addheader ( "Content-Length", "" + file.length ()); outputStream toclient = new bufferedOutputStream (response.getOutputStream ()); Response.setContentType ( "Application/Octet-stream"); toclient.write (버퍼); toclient.flush (); toclient.close (); } catch (ioexception ex) {ex.printstacktrace (); }}이 기사가 모든 사람의 Java 프로그래밍에 도움이되기를 바랍니다.