java文件下载

3 篇文章 0 订阅

文件上传之后有时候需要添加下载功能,但html5提供的下载方式在web项目下不实用,当然也可能是我用的方法不对,在这里我介绍一种我的解决方式

首先是前端页面文件下载部分的代码片段,这里引用了

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

这个jstl应该都知道在哪引用就不详细介绍了

代码片段前端

 

<div class="case">
  <table>
    <c:forEach items="${list}" var="li">
      <tr>
        <td style="width: 150px;margin-right: 20px"><a href="/content/download?filePath=${li.downName}" download="${li.downName}">${li.title}</a></td>
        <td style="width: 80px;margin-right: 10px;text-align: center;">${li.downType}</td>
        <td style="width: 420px;margin-right: 10px;text-align: center;">${li.introduce}</td>
        <td style="width: 100px;margin-right: 10px;text-align: center"><fmt:formatDate value="${li.createTime}"/></td>
        <td style="width: 80px;text-align: center"><a href="/content/download?filePath=${li.downName}" download="${li.downName}">点击下载</a></td>
      </tr>
    </c:forEach>
  </table>
  <div class="space_hx">&nbsp;</div>

后台部分

API下载的接口    返回值一定要为null

 

/**
     * 文件下载
     * @param request
     * @param response
     * @param filePath
     * @return
     */
    @RequestMapping("/download1")
    public void download1( HttpServletRequest request, HttpServletResponse response, String filePath){
        try {
            //获取文件名
            String fileName = filePath.substring(filePath.lastIndexOf("/")+1);
            System.out.println(filePath);
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            //处理下载弹出框名字的编码问题
            response.setHeader("Content-Disposition", "attachment;fileName="
                    + new String( fileName.getBytes("gb2312"), "ISO8859-1" ));
            //获取文件的下载路径
            String path = request.getSession().getServletContext().getRealPath(filePath);
            System.out.println(path);
            //利用输入输出流对文件进行下载
            InputStream inputStream = new FileInputStream(new File(path));
            //文件传输大小
            OutputStream os = response.getOutputStream();
            byte[] b = new byte[2048]; 
            int length;
            while ((length = inputStream.read(b)) > 0) { 
                os.write(b, 0, length); 
            } // 关闭。
            os.close(); 
            inputStream.close();
        } catch (FileNotFoundException e) { 
            e.printStackTrace();
        } catch (IOException e) { 
            e.printStackTrace(); 
        }
    }
        
    //文件传输大小

OutputStream os = response.getOutputStream(); byte[] b = new byte[2048]; int length; while ((length = inputStream.read(b)) > 0) { os.write(b, 0, length); } // 关闭。 os.close(); inputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }

下载功能这样就实现了,

下面是我的实体类,怕看html的内容不明白downName是什么

 

package com.yfsoft.pojo;

import java.util.Date;

public class DownloadBean {
    private Integer downId;

    private String downType;

    private String downPath;

    private Date createTime;

    private String introduce;

    private String title;

    private String downName;

    public String getDownName() {
        return downName;
    }

    public void setDownName(String downName) {
        this.downName = downName;
    }

    public Integer getDownId() {
        return downId;
    }

    public void setDownId(Integer downId) {
        this.downId = downId;
    }

    public String getDownType() {
        return downType;
    }

    public void setDownType(String downType) {
        this.downType = downType;
    }

    public String getDownPath() {
        return downPath;
    }

    public void setDownPath(String downPath) {
        this.downPath = downPath;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getIntroduce() {
        return introduce;
    }

    public void setIntroduce(String introduce) {
        this.introduce = introduce;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值