この記事の例は、参照用のJava実装ファイルのダウンロードの特定のコードを共有しています。特定のコンテンツは次のとおりです
public httpservletResponseダウンロード(String Path、httpservletResponse応答){try {//パスは、ダウンロードするファイルのパスを参照します。 file file = new file(path); //ファイル名を取得します。文字列filename = file.getName(); //ファイルの接尾辞名を取得します。 string ext = filename.substring(filename.lastindexof( "。") + 1).touppercase(); //ストリームの形でファイルをダウンロードします。 inputstream fis = new BufferedInputStream(new FileInputStream(PATH)); byte [] buffer = new byte [fis.abailable()]; fis.read(buffer); fis.close(); //応答応答をクリアしますResponse.reset(); //応答のヘッダーResponse.Addheader( "Content-Disposition"、 "attachment; filename =" + new String(filename.getBytes()))を設定します。 Response.Addheader( "Content-Length"、 "" + file.length()); outputStream toClient = new BufferedOutputStream(Response.GetOutputStream()); Response.setContentType( "Application/Octet-Stream"); toclient.write(buffer); toclient.flush(); toclient.close(); } catch(ioException ex){ex.printstacktrace(); }返信応答; } public void downloadlocal(httpservletResponse応答)filenotFoundExceptionをスロー{//ローカルファイル文字列filename = "operator.doc" .toString(); //ファイルのデフォルトの保存名//入力Stream instream = new FileInputStream( "c:/operator.doc"); //ファイルストレージパス//出力response.reset()の形式を設定します。 Response.setContentType( "bin"); Response.Addheader( "Content-Disposition"、 "Attachment; filename =/" " + filename +"/""); //ストリームバイトのデータをループアウトします[] b = new byte [100]; int len; try {while((len = instream.read(b))> 0)respons.getOutputStream()。書き込み(b、0、len); instream.close(); } catch(ioexception e){e.printstacktrace(); }} public void downloadNet(httpservletResponse Response)Throws Malfordurlexception {//ネットワークファイルint bytesum = 0; int byteread = 0; url url = new url( "Windine.blogdriver.com/logo.gif"); try {urlconnection conn = url.openconnection(); inputstream instream = conn.getInputStream(); fileoutputStream fs = new fileoutputStream( "c:/abc.gif"); byte [] buffer = new byte [1204]; int length; while((byteread = instream.read(buffer))!= -1){bytesum += byteread; System.out.println(bytesum); fs.write(buffer、0、byteread); }} catch(filenotfoundexception e){e.printstacktrace(); } catch(ioexception e){e.printstacktrace(); }}//オンラインでファイルを開く方法
public void download(string filepath、httpservletresponse応答、boolean isonline)exception {file f = new file(filepath); if(!f.exists()){respons.senderror(404、 "ファイルが見つかりません!");戻る; } bufferedInputStream BR = new BufferedInputStream(new FileInputStream(f)); byte [] buf = new byte [1024]; int len = 0; Response.reset(); //非常に重要なif(isonline){//オンラインオープンメソッドurl u = new url( "file:////" + filepath); Response.setContentType(U.OpenConnection()。getContentType()); Response.setheader( "content-disposition"、 "inline; filename =" + f.getName()); //ファイル名はutf-8}としてエンコードする必要があります} else {//純粋なダウンロードメソッドresponse.setContentType( "Application/X-MSDownLoad"); Response.setheader( "content-disposition"、 "attachment; filename =" + f.getName()); } outputStream out = respons.getOutputStream(); while((len = br.read(buf))> 0)out.write(buf、0、len); br.close(); out.close(); }上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。