文件的上传和下载
来源:互联网 发布:淘宝客买家 编辑:程序博客网 时间:2024/05/29 04:07
一、文件的上传
// io流String realPath = ServletActionContext.getServletContext().getRealPath("\\upload"); // 输入流读取文件 FileInputStream inputStream = new FileInputStream(file); // 创建一个文件对象 File targetr = new File(realPath, fileFileName); // 创建一个输出流 FileOutputStream outputStream = new FileOutputStream(targetr); byte[] bb = new byte[1024]; int len; while ((len = inputStream.read(bb)) != -1) { outputStream.write(bb, 0, len); } outputStream.close(); inputStream.close();
Struts2中的文件上传
public String upload() throws IOException { // io流 String realPath = ServletActionContext.getServletContext().getRealPath("\\upload"); // 创建一个文件对象 File targetr = new File(realPath, fileFileName); FileUtils.copyFile(file, targetr); return SUCCESS; }
文件上传过滤器的设置
<!-- 添加文件上传拦截器 --> <interceptor-ref name="fileUpload"> <!-- 设置允许上传的文件类型 --> <param name="allowedTypes"> image/jpeg,image/bmp </param> <!-- 设置允许上传的文件大小 --> <param name="maximumSize">81920</param> </interceptor-ref> <interceptor-ref name="defaultStack"/>
二、文件的下载
// 获取到要下载资源 String realPath = ServletActionContext.getServletContext().getRealPath("\\download\\abc.txt"); String filename = realPath.substring(realPath.lastIndexOf("\\") + 1); // 设置响应头 HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("text/html;charset=utf-8"); response.setHeader("Content-disposition", "attachment;fileName=" + filename); // 输入流 FileInputStream inputStream = new FileInputStream(realPath); // 输出流 ServletOutputStream outputStream = response.getOutputStream(); byte[] bb = new byte[1024]; int len; while ((len = inputStream.read(bb)) != -1) { outputStream.write(bb, 0, len); } outputStream.close(); inputStream.close();
struts2中的文件下载
public String streamDownLoad() throws IOException{ // 获取到要下载的资源 的路径 String realPath = ServletActionContext.getServletContext().getRealPath("\\download\\abc.txt"); setInputStream(new FileInputStream(realPath)); fileName = realPath.substring(realPath.lastIndexOf("\\")+1); return Action.SUCCESS; }
阅读全文
0 0
- 文件的上传和下载
- 文件的上传和下载
- 文件的上传和下载
- 文件的上传和下载
- 文件的上传和下载
- 文件的上传和下载
- 文件的上传和下载
- 文件的上传和下载
- .文件的上传和下载:
- 文件的上传和下载
- 文件的上传和下载
- 文件的上传和下载
- 文件的上传和下载
- 文件的上传和下载
- 文件的上传和下载
- 文件的上传和下载
- 文件的上传和下载
- 文件的上传和下载
- SPRING 事务控制 (PROPAGATION_NESTED)
- java基础习题50道(一)
- bzoj3196 Tyvj 1730 二逼平衡树 线段树套treap
- 数学: HDUCo-prime
- java中的包装类
- 文件的上传和下载
- Dubbo入门---搭建一个最简单的Demo框架
- springmvc的RedirectAttributes使用方式
- Ubuntu上搭建Git服务器
- Elasticsearch5.2.0离线安装bigdesk
- hpuoj 【1149】 倒读文章 [【字符串】
- 【CSDN 日报】| 6.21-7.21 上榜作者排行出炉
- 2017 杭电多校联赛第二场 1001 Is Derek lying? HDU 6045
- Java设计模式——观察者模式(事件监听)