@RequestMapping("/exportDocument")
@ResponseBody
public void exportDocument(HttpServletRequest request,HttpServletResponse response) throws IOException {
XWPFDocument xdoc = null;
FileInputStream is = null;
OutputStream out=null;
try {
String wordName="数聚空港2.0使用手册.docx";
wordName = new String(wordName.getBytes(), "iso8859-1");
// File file = new File("/root/usersGuide.docx");
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition", "attachment; filename="+ wordName);
is = new FileInputStream("/root/usersGuide.docx");
out=response.getOutputStream();//获得一个output对象
xdoc = new XWPFDocument(is);
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
xdoc.write(out);//Write out this document to an Outputstream.
is.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/*上述是word文档的下载*/ /*下面是对各种类型的文件下载*/ /*
  
          //2.获取要下载的文件名
String fileName = "test.png";
//3.设置content-disposition响应头控制浏览器以下载的形式打开文件
response.setHeader("content-disposition", "attachment;filename="+fileName);
//4.获取要下载的文件输入流
InputStream in=null;
OutputStream out=null;
try {//获取要下载的文件的绝对路径
in=new FileInputStream("/root/运营线路图0.3.5版.png");
int len = 0;
//5.创建数据缓冲区
byte[] buffer = new byte[1024];
//6.通过response对象获取OutputStream流
out = response.getOutputStream();
//7.将FileInputStream流写入到buffer缓冲区
while ((len = in.read(buffer)) > 0) {//in.read(byte[] b)最多读入b.length个字节 在碰到流的结尾时 返回-1
//8.使用OutputStream将缓冲区的数据输出到客户端浏览器
out.write(buffer,0,len);
}
}catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
in.close();
out.close();
}*/
}

java 实现对指定目录的文件进行下载的更多相关文章

  1. Java基础---Java---IO流-----File 类、递归、删除一个带内容的目录、列出指定目录下文件夹、FilenameFilte

    File 类 用来将文件或者文件夹封装成对象 方便对文件与文件夹进行操作. File对象可以作为参数传递给流的构造函数 流只用操作数据,而封装数据的文件只能用File类 File类常见方法: 1.创建 ...

  2. java统计指定目录中文件的个数和总的大小

    转: 统计指定目录中文件的个数和总的大小 package file; import java.io.File; import java.util.ArrayList; public class Fil ...

  3. Window Linux下实现指定目录内文件变更的监控方法

    转自:http://qbaok.blog.163.com/blog/static/10129265201112302014782/ 对于监控指定目录内文件变更,window 系统提供了两个未公开API ...

  4. Git如何检出指定目录或文件

    系统版本:Window 10,Git 版本:2.7.1 对于大型 Git 仓库,每次执行 Git 命令,都需要经过漫长的等待,特别是要经常执行的 git status 命令.下面是一个例子... 从 ...

  5. IO流-获取指定目录下文件夹和文件对象【File类】

    一.运用File类实现获取指定目录下文件夹和文件对象 1.File类 2.方法: 获取文件绝对路径 :getAbsolutePath 案例: import java.io.File; /** * 获取 ...

  6. [WinAPI] API 13 [遍历指定目录 打印文件和其他属性]

    Windows API中,有一组专门的函数和结构,用于遍历目录,它们是FindFirstFile函数.FindNextFile函数和WIN32_FIND_DATA结构.使用FindFirstFile和 ...

  7. rsync 排除指定目录或文件进行同步

    很常见的情况:我想同步/myweb下的 一些php文件 ,  但是不想复制/myweb/log/里边的一些日志文件,因为这些文件太大了,备份也是没有意义的. 现在如果想避开某个路径  直接添加—exc ...

  8. C#监控指定目录的文件变化的代码

    如下的资料是关于C#监控指定目录的文件变化的代码. FileSystemWatcher watcher = new FileSystemWatcher();watcher.Path = @" ...

  9. scp从远程指定目录拷贝文件到本地指定目录

    scp从远程指定目录拷贝文件到本地指定目录 [root@picts ~]# cat /root/scp_pictures.sh #!/bin/bash # Function: copy files f ...

随机推荐

  1. python 多线程爬虫 实例

    多进程 Multiprocessing 模块 Process 类用来描述一个进程对象.创建子进程的时候,只需要传入一个执行函数和函数的参数即可完成 Process 示例的创建. star() 方法启动 ...

  2. mybatis 映射器

    1 映射器 Mapper 是由java接口和 XML 文件共同组成.它的作用如下 1)定义参数类型 2)描述缓存 3)描述 SQL 语句 4)定义查询结果和POJO的映射关系 2 SqlSession ...

  3. 用户ID的代码生成

    public class Uid { private static final String machineIdString = Integer.toHexString(new Object().ha ...

  4. Maven的个性化定制

    用Maven的小伙伴都知道,Maven的宗旨是约定优于配置(Convention Over Configuration). 在宗旨的前提下Maven也提供了个性化定制的Profile,让我们看看使用方 ...

  5. Qt 积累

    总结(-) 1> 定时器的使用 QTimer *timer = new QTimer(this);  connect(timer, SIGNAL(timeout()), this, SLOT(u ...

  6. 使用nio实现web服务器

    package com.nio; import java.io.IOException; import java.net.InetAddress; import java.net.InetSocket ...

  7. mouseover mouseenter mouseout mouseleave

    mouseover与mouseenter 不论鼠标指针穿过被选元素或其子元素,都会触发 mouseover 事件. 只有在鼠标指针穿过被选元素时,才会触发 mouseenter 事件. mouseou ...

  8. 延时NSTimer

    import Foundationimport UIKit class YijfkController:UIViewController{ override func viewDidLoad() { ...

  9. 实现在edittext中任意插入图片

    Myedittext: public class MyEditText extends EditText { public MyEditText(Context context) { super(co ...

  10. 结构体内重载小于号< 及构造函数

    struct Node { int d, e; bool operator < (const Node x) const { return x.d < d; } Node(int d, i ...