java web实现文件下载的注意事项
2021-07-02 08:05
标签:mes 页面 eth 前端 print ack exist index val 如图,在浏览器中实现以下的下载方式 注意点: 1,下载文件存在于web服务器上本地上; 2,前端页面用form形式请求。 html: js: java: java web实现文件下载的注意事项 标签:mes 页面 eth 前端 print ack exist index val 原文地址:https://www.cnblogs.com/xumz/p/9630589.htmldiv 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>
/**
*
* 开始下载
* 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();
}
@RequestMapping("/downloadDebit")
public void downloadDebit(HttpServletRequest request,HttpServletResponse response,@RequestParam Map
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实现文件下载的注意事项
文章链接:http://soscw.com/index.php/essay/100704.html