思路:
1.用隐藏的iframe去访问下载地址实现,成功out.write("文件流"),失败out.write('div 里面有数据')
2.编写$(iframe).load()方法,处理失败的动作
代码:
页面里写:
<script type="text/javascript">
$(function(){
$('#file_frame').load(function(){
fileDownloadError('file_frame');
});
});
function downloadFile(fileId){
var url= ctx+'/system/documents/downloadFile?id='+fileId;
$('#file_frame').attr('src',url);
}
</script>
<iframe id="file_frame" width="0" height="0" hidden="true" ></iframe>
java里
/**
* 获取文件不存在时,的html提示 代码
* **/
public static String getNotFileHtml(String fileName,String message){
StringBuffer sb = new StringBuffer();
sb.append("<html>");
sb.append("<head>");
sb.append("<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>");
sb.append("</head>");
sb.append("<body>");
sb.append("<div id='errorInfo' ");
sb.append(" fileName='"+fileName+"'");
sb.append(" message='"+message+"'");
sb.append("</div>");
sb.append("</body>");
sb.append("</html>");
String();
}
/**
* downloadFileByAbsolutePath(文件下载)
* @param path 文件的路径(绝对路径E://)
* @param prefixFilename 文件名称
* @param suffFilename 文件名称的后缀
* @param response
* @param request
*/
public void downloadFileByAbsolutePath(String path ,String prefixFilename ,String suffFilename,final HttpServletResponse response ,HttpServletRequest request) {
OutputStream out = null;
String fileName = prefixFilename+"."+suffFilename;
try {
out = new OutputStream());
String filePath =path;
String fileName_encode = de(fileName, "UTF-8").replaceAll("[+]","%20");
byte[] data = null;
try{
data = Bytes(filePath);
}catch(FileNotFoundException e2){
response.setCharacterEncoding("UTF-8");
String notFileHtml = NotFileHtml(fileName,"文件不到!");
out.Bytes("UTF-8"));
out.flush();
}
request.setCharacterEncoding("UTF-8");
if (Header("User-Agent").toLowerCase().indexOf("msie") > -1){
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName_encode + "\"");
}else{
response.setHeader("content-disposition","attachment;filename*=utf-8'zh_cn'"+fileName_encode);
}
response.addHeader("Content-Length", "" + data.length);
response.setContentType("application/octet-stream;charset=UTF-8");
out.write(data);
out.flush();
} catch (Exception e) {
String notFileHtml = NotFileHtml(fileName,"文件下载出错!");
try {
response.setCharacterEncoding("UTF-8");
out.Bytes("UTF
-8"));
out.flush();
} catch (Exception e1) {
e1.printStackTrace();
<("文件下载出错");
}
} finally {
try {
if(out != null){
out.close();
}
} catch (IOException e) {
<("关闭BufferedOutputStream出错");
e.printStackTrace();
}
}
}
/**
* downloadFile(文件下载 ,相对项目下的路径)
* @param path 文件的路径(相对路径 /docs/abc/test.jpg)
* @param filename 文件名称
* @param response
* @param request
*/
public void downloadFile(String path ,String filename,final HttpServletResponse response ,HttpServletRequest request) {
String prefixFilename = filename.substring(0,filename.lastIndexOf("."));
String suffFilename = filename.substring(filename.lastIndexOf(".")+1,filename.length());
downloadFile(path, prefixFilename, suffFilename, response, request);
}
/**
* downloadFile(文件下载 ,相对项目下的路径)
* @param path 文件的路径(相对路径 /docs/abc/test.jpg)
* @param prefixFilename 文件名称
* @param suffFilename 文件名称的后缀
* @param response
* @param request
*/
public void downloadFile(String path ,String prefixFilename ,String suffFilename,final HttpServletResponse response ,HttpServletRequest request) {
path = FileRealPath(path, Session().getServletContext());
downloadFileByAbsolutePath(path, prefixFilename, suffFilename, response, request);
}
/**
* downloadFileByAbsolutePath(文件下载)
* @param path 文件的路径(绝对路径E://)
* @param filename 文件名称
* @param response
* @param request
*/
public void downloadFileByAbsolutePath(String path ,String filename,final HttpServletResponse response ,HttpServletRequest request) {
String prefixFilename = filename.substring(0,filename.lastIndexOf("."));
String suffFilename = filename.substring(filename.lastIndexOf(".")+1,filename.length());
downloadFileByAbsolutePath(path, prefixFilename, suffFilename, response, request);
}
/**
* 获得指定文件的byte数组
* @throws FileNotFoundException
*/
getsavefilenamepublic static byte[] getBytes(String filePath) throws FileNotFoundException {
byte[] buffer = null;
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
try{
while ((n = ad(b)) != -1) {
bos.write(b, 0, n);
}
}catch(Exception e){
<("文件转换成byte数组出错");
}finally{
try {
fis.close();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
buffer = bos
.toByteArray();
return buffer;
}
/**
* 获取 文件的 真实路径
* **/
public static String getFileRealPath(String path,ServletContext ctx){
if(getSystemName().equals("linux")){//linux
try {
path = URLDecoder.decode(path,"UTF-8");
} catch (UnsupportedEncodingException e3) {
<("path的编码不支持decode转码");
e3.printStackTrace();
}
}
String savePath = path;
int index = savePath.lastIndexOf("/");
String fileName = savePath.substring(index + 1);
fileName = placeAll("%20","");
fileName = placeAll(" ","");
String rpath = getRealPath(ctx)+ savePath;//服务器上的文件真实路径
return rpath;
}
//获取不 同系统下的文件真正路径
public static String getRealPath(ServletContext cxt){
String rpath = "";
if(getSystemName().equals("linux")){//linux
rpath = File.separator + RealPath(File.separator);
}else{//windows
rpath = RealPath(File.separator);
}
return rpath;
}
//获取系统名称
public static String getSystemName(){
Property("file.separator").equals("/")){//linux
return "linux";
}else{
return "windows";
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论