Java下载文件的四种方式详细代码
程序员文章站
2023-12-29 17:33:10
1.以流的方式下载public httpservletresponse download(string path, httpservletresponse response) { try...
1.以流的方式下载
public httpservletresponse download(string path, httpservletresponse response) { try { // path是指欲下载的文件的路径。 file file = new file(path); // 取得文件名。 string filename = file.getname(); // 取得文件的后缀名。 string ext = filename.substring(filename.lastindexof(".") + 1).touppercase(); // 以流的形式下载文件。 inputstream fis = new bufferedinputstream(new fileinputstream(path)); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); // 清空response response.reset(); // 设置response的header 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(); } return response; }
2.下载本地文件
public void downloadlocal(httpservletresponse response) throws filenotfoundexception { // 下载本地文件 string filename = "operator.doc".tostring(); // 文件的默认保存名 // 读到流中 inputstream instream = new fileinputstream("c:/operator.doc");// 文件的存放路径 // 设置输出的格式 response.reset(); response.setcontenttype("bin"); response.addheader("content-disposition", "attachment; filename=\"" + filename + "\""); // 循环取出流中的数据 byte[] b = new byte[100]; int len; try { while ((len = instream.read(b)) > 0) response.getoutputstream().write(b, 0, len); instream.close(); } catch (ioexception e) { e.printstacktrace(); } }
3.下载网络文件
public void downloadnet(httpservletresponse response) throws malformedurlexception { // 下载网络文件 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(); } }
4.支持在线打开的方式
public void download(string filepath, httpservletresponse response, boolean isonline) throws exception { file f = new file(filepath); if (!f.exists()) { response.senderror(404, "file not found!"); return; } 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 = response.getoutputstream(); while ((len = br.read(buf)) > 0) out.write(buf, 0, len); br.close(); out.close(); }
到此这篇关于java下载文件的四种方式详细代码的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持。