Java下载⽂件的4种⽅式总结1.以流的⽅式下载.
public HttpServletResponse download(String path, HttpServletResponse response) {
try {
// path是指欲下载的⽂件的路径。
File file = new File(path);
// 取得⽂件名。
String filename = Name();
// 取得⽂件的后缀名。
String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
// 以流的形式下载⽂件。
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
个人简历表格模板免费fis.close();
// 清空response
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new Bytes()));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new OutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
html playtoClient.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.setContentType("bin");
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
// 循环取出流中的数据
byte[] b = new byte[100];
int len;
try {
while ((len = ad(b)) > 0)
telnet ip 端口 命令是什么意思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/logo.gif");
try {
URLConnection conn = url.openConnection();
InputStream inStream = InputStream();
FileOutputStream fs = new FileOutputStream("c:/abc.gif");
byte[] buffer = new byte[1204];
int length;
while ((byteread = ad(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;
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 = OutputStream();
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 = portAddress +Time()+".zip";
File file=new portAddress);
if(!file.isDirectory() && !ists()){
//创建单层⽬录
// f.mkdir();
// 创建多层⽬录
file.mkdirs();
}
try {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(strZipPath));
java下载过程// 需要同时下载的多个⽂件
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 = ad(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.closeEntry();
truncate例子fis.close();
}
out.close();
PublicMethod.downLoadFile(request, response, strZipPath, filename+".zip");
File temp=new File(strZipPath);
ists()){
temp.delete();
}
} catch (Exception e) {
System.out.println("⽂件下载错误");
}
}
总结
eclipse数值模拟教程到此这篇关于Java下载⽂件的4种⽅式的⽂章就介绍到这了,更多相关Java下载⽂件内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。