为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

java文件下载 带有错误提示框 弹出的

2017-10-16 10页 doc 26KB 152阅读

用户头像

is_792768

暂无简介

举报
java文件下载 带有错误提示框 弹出的java文件下载 带有错误提示框 弹出的 Ideas: Using the hidden iframe to access the download address implementation, successful out.write (" file flow "), failed out.write (' div contains data ') Write the $(iframe).load () method to handle failed actions Code: Page write: $(...
java文件下载    带有错误提示框 弹出的
java文件下载 带有错误提示框 弹出的 Ideas: Using the hidden iframe to access the download address implementation, successful out.write (" file flow "), failed out.write (' div contains data ') Write the $(iframe).load () method to handle failed actions Code: Page write: < script type = "text/javascript" > $(function () { $(' # file_frame '). Load (function () {) FileDownloadError (' file_frame '); }); }); The function downloadFile (count) { Var url = CTX + '/ system/documents/downloadFile? Id = '+ count; $(' # file_frame ').attr (' SRC '); } < / script > < iframe id = "file_frame" "width =" 0 "height =" 0 "and" true "style =" border: 0px ". > < iframe > In the Java / * * * the HTML prompt for the file that does not exist * * * / Public static String getNotFileHtml (String fileName, String message) { StringBuffer sb = new StringBuffer () Append (" < HTML > "); Append (" < head > "); Append (" < meta HTTP - equiv = "Content - Type 'Content =' Content = 'text/HTML; charset = utf-8' >"); Append (" < / head > "); Append (" < body > "); Append (" < div id = 'errorInfo'); (" fileName = '" + fileName + ""); "(" message =" "+ message +"); Append (" < / div > "); Append (" < / body > "); Append (" < / HTML > "); Return sb. ToString (); } / * * * downloadFileByAbsolutePath (file download) * @param path file path (absolute path E: / / text.txt) * @param prefixFilename file name * @param suffFilename file name suffix * @ param response * @ param request * / Public void downloadFileByAbsolutePath (String path, String prefixFilename, String suffFilename, final HttpServletResponse response, HttpServletRequest request) { OutputStream out = null; String fileName = prefixFilename + "." Try { Out = new BufferedOutputStream (). String filePath = path; "String fileName_encode = urlencoder.encode (" utf-8"). Byte [] data = null Try { Data = filein.getbytes (filePath); {} the catch (FileNotFoundException e2) SetCharacterEncoding (" utf-8 "); String notFileHtml = fileunfile.getnotfilehtml (fileName, "file cannot find!") (" notfilehtml.getbytes (" utf-8 ")) Out. The flush (); } SetCharacterEncoding (" utf-8 "); The response. The reset (); (" user-agent ").tolowercase ().indexof (" msie ").indexof (" msie ") "Response. SetHeader (" content-size", "attachment; filename = \" "+ fileName_encode +"); } else { (" content, "" attachment.") 文件名* = utf-8'zh_cn”+ fileName_encode); } 响应。内容长度addHeader(“”、“”+ data.length); response.setContentType(“应用程序/八进制;charset = utf - 8”); out.write(数据); out.flush(); 捕获(异常e){ 字符串notFileHtml = FileOperate.getNotFileHtml(文件名,“文 件下载出错!”); 尝试{ response.setCharacterEncoding(“utf - 8”); out.write(notFileHtml.getBytes(" utf - 8 ")); out.flush(); 捕获(异常e1){ e1.printStackTrace(); logger.error(“文件下载出错”); } 最后} { 尝试{ 如果出去!= null){ out.close(); } 捕获(IOException){ logger.error(“关闭BufferedOutputStream出错”); e.printStackTrace(); } } } / * * * downloadFile(文件下载,相对项目下的路径) * @param路径文件的路径(相对路径/ docs / abc / test.jpg) * @param文件名文件名称 * @param响应 * @param请求 * / 公共void downloadFile(字符串路径、字符串文件名、最终HttpServletResponse响应、HttpServletRequest请求){ 字符串prefixFilename = filename.substring(0,filename.lastIndexOf(“。”); 字符串suffFilename = filename.substring(filename.lastIndexOf(“。”)+ 1,filename.length()); 下载文件 (path,prefixFilename,suffFilename,response,request); } / * * * downloadFile(文件下载,相对项目下的路径) * @param路径文件的路径(相对路径/ docs / abc / test.jpg) * @param prefixFilename文件名称 * @param suffFilename文件名称的后缀 * @param响应 * @param请求 * / 公共void downloadFile(字符串路径、字符串prefixFilename、String suffFilename、final HttpServletResponse响应、 HttpServletRequest请求){ = FileOperate路径。.getServletContext getFileRealPath(路径,request.getSession()()); downloadfileby绝对化路径(路径,前缀文件名,萨福姆斯,响应,请求); } / * * * downloadFileByAbsolutePath(文件下载) * @param路径文件的路径(绝对路径E:/ / text.txt) * @param文件名文件名称 * @param响应 * @param请求 * / 公共void downloadfileby绝对条路径(字符串路径、字符串文件名、最终HttpServletResponse响应、HttpServletRequest请求){ 字符串prefixFilename = filename.substring(0,filename.lastIndexOf(“。”); 字符串suffFilename = filename.substring(filename.lastIndexOf(“。”)+ 1,filename.length()); downloadfileby绝对化路径(路径,前缀文件名,萨福姆斯,响应,请求); } / * * *获得指定文件的字节数组 * @throws FileNotFoundException * / 公共静态字节[]getBytes(字符串filePath)抛出 FileNotFoundException { 缓冲区byte[]=零; 文件文件=新文件(filePath); FileInputStream fis = new FileInputStream(文件); ByteArrayOutputStream bos = new ByteArrayOutputStream(1000); 字节[]b = new byte[1000]; int n; 尝试{ 同时(n = fis . read(b))!= 1){ bos。写(b 0 n); } }捕捉(异常e){ logger.error(“文件转换成字节数组出错”); 最后} { 尝试{ fis.close(); bos.close(); 捕获(IOException){ e.printStackTrace(); } } 缓冲= bos.toByteArray(); 返回缓冲区; } / * * *获取文件的真实路径 * * * / 公共静态字符串getFileRealPath(字符串路径,ServletContext ctx){ 如果(getSystemName().equals(“linux”)){ / / linux 尝试{ = URLDecoder.decode路径(路径,“utf - 8”); 捕获(UnsupportedEncodingException e3){ logger.error(“路径的编码不支持解码转码”); e3.printStackTrace(); } } 字符串savePath =路径; int指数= savePath.lastIndexOf(“/”); 文件名= savePath字符串。substring(指数+ 1); 文件名= fileName.replaceAll(" % 20 "," "); 文件名=文件名。replaceAll(" "," "); 字符串rpath = getRealPath(ctx)+ savePath;/ /服务器上的文件 真实路径 返回的rpath; } / /获取不同系统下的文件真正路径 公共静态字符串getRealPath(ServletContext cxt){ 字符串rpath = " "; 如果(getSystemName().equals(“linux”)){ / / linux rpath =文件。分离器+ cxt.getRealPath(File.separator); } { / /其他窗口 rpath = cxt.getRealPath(File.separator); } 返回的rpath; } / /获取系统名称 公共静态字符串getSystemName(){ 如果(System.getProperty(file.separator).equals(“/”)){ / / linux 返回“linux”; 其他} { 返回“windows”; } }
/
本文档为【java文件下载 带有错误提示框 弹出的】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索