码迷,mamicode.com
首页 > 编程语言 > 详细

java web实现文件下载的注意事项

时间:2018-09-11 22:32:23      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:mes   页面   eth   前端   print   ack   exist   index   val   

如图,在浏览器中实现以下的下载方式

技术分享图片

注意点:

1,下载文件存在于web服务器上本地上;

2,前端页面用form形式请求。

html:

<div id="download_debit_dlg" >
    <form id="itemForm_payreslut_charge" method="post">
        <table cellspacing="5px;">
            <tr>
                <td><label for="debitDate">日期:</label></td>
                <td><input type="date" name="debitDate" id="debitDate"
                    required="required"></td>
            </tr>
        </table>
    </form>
</div>
<div id="dlg-buttons">
    <a href="javascript:downloadDebitPost()"iconCls="icon-ok">下载</a> 
</div>

 js:

/**
 * 
 * 开始下载
 * ctx 对应路径
 */
function downloadDebitPost(){
    var debitDate=$(‘#debitDate‘).val();
    if (debitDate==""||debitDate==null) {
        $.messager.alert("系统提示", "请完整输入日期");
        return;
    }
    $("#itemForm_payreslut_charge").attr("action", ctx + ‘/downloadDebit‘);
    $(‘#itemForm_payreslut_charge‘).submit();
}

 java:

@RequestMapping("/downloadDebit")
public void downloadDebit(HttpServletRequest request,HttpServletResponse response,@RequestParam Map<String, Object> reqParams) {
    String debitDate = (String) reqParams.get("debitDate");
    debitDate=debitDate.replaceAll("-", "");
    try {
        String fileName = "202210000000000001214_" + debitDate + ".txt";//文件名
        String path = "D:/file/opbill/" + fileName;//绝对路径
        FileUploadUtil.downLoadByFilePath(request, response,path);
    } catch (BusinessException e) {
        //LOGGER.error("导出账单出错:", e);
    } catch (IOException e) {
        //LOGGER.error("导出账单出错:", e);
        e.printStackTrace();
    }
}
public class FileUploadUtil {    
    /**
     * 按文件源路径下载文件
     * @param request
     * @param response
     * @param path
     */
    public static void downLoadByFilePath(HttpServletRequest request,
            HttpServletResponse response, String path) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            response.setContentType("text/html;charset=utf-8");
            request.setCharacterEncoding("UTF-8");
            File file = new File(path);
            if(!file.exists()){
                log.info("文件路径:"+path);
                throw new BusinessException("下载文件路径不存在["+path+"]");
            }
            long fileLength = file.length();
            // 编码处理
            String fileName = path.substring(path.lastIndexOf("/") + 1);
            fileName = encodeFileName(request, fileName);
            response.setContentType("application/x-msdownload;");

            response.setHeader("Content-disposition", "attachment; filename="
                    + fileName);
            response.setHeader("Content-Length", String.valueOf(fileLength));

            bis = new BufferedInputStream(new FileInputStream(path));
            bos = new BufferedOutputStream(response.getOutputStream());
            byte[] buff = new byte[2048];
            int bytesRead;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
        } catch (IOException e) {
            // 此异常为下载中出现的异常,不影响下载功能,可捕获但无需抛出
        } catch (Exception e) {
            log.error("下载出现异常:", e);
        } finally {
            try {
                if (bis != null) {
                    bis.close();
                }
                if (bos != null) {
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();

            }
        }
    }
}

 

java web实现文件下载的注意事项

标签:mes   页面   eth   前端   print   ack   exist   index   val   

原文地址:https://www.cnblogs.com/xumz/p/9630589.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!