직장에서 파일을 다운로드하는 기능이 발생했습니다. 나는 그것을 직접 추출했다. 코드는 간단합니다. 모든 사람을 도울 수 있기를 바랍니다. 좋아, 많이 말하지 말자, 코드를 추가 해주세요!
public void downloadfile (파일 파일, 문자열 다운 이름, httpservletrequest request, httpservletresponse 응답) {outputstream out = null; fileInputStream fin = null; bufferedInputStream bin = null; try {if (file.exists ()) {String finalfilename = null; 문자열 agent = request.getheader ( "사용자 에이전트"); 부울 ismsie = (agent! = null && agent.indexof ( "msie")! = -1); if (ismsie) {finalfilename = urlencoder.encode (downname, "utf8"); } else {finalFileName = new String (downname.getBytes ( "utf-8"), "iso-8859-1"); } response.setContentType ( "application/x-msdownload"); response.setHeader ( "내용 분할", "첨부 파일; filename =". concat (finalfilename)); out = response.getOutputStream (); fin = new FileInputStream (파일); 빈 = 새로운 bufferedInputStream (Fin); for (int data = bin.read (); data> -1; data = bin.read ()) {out.write (data); }} else {}} catch (예외 e) {e.printstacktrace (); } 마침내 {try {if (bin! = null) bin.close (); if (fin! = null) fin.close (); if (out! = null) out.close (); } catch (예외 e2) {e2.printstacktrace (); }}}위는이 기사에서 다운로드 된 코드입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.