Java下载文件的4种方式总结
程序员文章站
2022-06-28 11:37:32
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中批量下载文件,将下载多个文件打包成zip文件下载。
//批量文件下载(将多个文件打包成zip包下载) public static void batchdownloadfile(httpservletrequest request,httpservletresponse response,string filename,string[] filepath,string[] documentname,string loginname){ byte[] buffer = new byte[1024]; date date=new date(); //生成zip文件存放位置 string strzippath = constant.exportaddress +loginname+date.gettime()+".zip"; file file=new file(constant.exportaddress); if(!file.isdirectory() && !file.exists()){ //创建单层目录 // f.mkdir(); // 创建多层目录 file.mkdirs(); } try { zipoutputstream out = new zipoutputstream(new fileoutputstream(strzippath)); // 需要同时下载的多个文件 for (int i = 0; i < filepath.length; i++) { file f=new file(filepath[i]); fileinputstream fis = new fileinputstream(f); system.out.println(documentname[i]); out.putnextentry(new zipentry(documentname[i])); //设置压缩文件内的字符编码,不然会变成乱码 out.setencoding("gbk"); int len; // 读入需要下载的文件的内容,打包到zip文件 while ((len = fis.read(buffer)) > 0) { out.write(buffer, 0, len); } out.closeentry(); fis.close(); } out.close(); publicmethod.downloadfile(request, response, strzippath, filename+".zip"); file temp=new file(strzippath); if(temp.exists()){ temp.delete(); } } catch (exception e) { system.out.println("文件下载错误"); } }
总结
到此这篇关于java下载文件的4种方式的文章就介绍到这了,更多相关java下载文件内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!