java文件下载 带有错误提示框 弹出的
思路:
1.用隐藏的iframe去访问下载地址实现,成功out.write("文件流"),失败out.write('div 里面有数据')
2.编写$(iframe).load()方法,处理失败的动作
代码:
页面里写:
java里
/**
* 获取文件不存在时,的html提示 代码
* **/
public static String getNotFileHtml(String fileName,String message){
StringBuffer sb = new StringBuffer();
sb.append("");
sb.append("");
sb.append("
");
sb.append("");
sb.append("");
sb.append("
");
sb.append("");
sb.append("");
return sb.toString();
}
/**
* downloadFileByAbsolutePath(文件下载)
* @param path 文件的路径(绝对路径E://text.txt)
* @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 BufferedOutputStream(response.getOutputStream());
String filePath =path;
String fileName_encode = URLEncoder.encode(fileName, "UTF-8").replaceAll("[+]","%20");
byte[] data = null;
try{
data = FileOperate.getBytes(filePath);
}catch(FileNotFoundException e2){
response.setCharacterEncoding("UTF-8");
String notFileHtml = FileOperate.getNotFileHtml(fileName,"文件找
不到!");
out.write(notFileHtml.getBytes("UTF-8"));
out.flush();
}
request.setCharacterEncoding("UTF-8");
response.reset();
if (request.getHeader("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 = FileOperate.getNotFileHtml(fileName,"文件下载出错!");
try {
response.setCharacterEncoding("UTF-8");
out.write(notFileHtml.getBytes("UTF-8"));
out.flush();
} catch (Exception e1) {
e1.printStackTrace();
logger.error("文件下载出错");
}
} finally {
try {
if(out != null){
out.close();
}
} catch (IOException e) {
logger.error("关闭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 = FileOperate.getFileRealPath(path,
request.getSession().getServletContext());
downloadFileByAbsolutePath(path, prefixFilename, suffFilename, response, request);
}
/**
* downloadFileByAbsolutePath(文件下载)
* @param path 文件的路径(绝对路径E://text.txt)
* @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
*/
public 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 = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
}catch(Exception e){
logger.error("文件转换成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) {
logger.error("path的编码不支持decode转码");
e3.printStackTrace();
}
}
String savePath = path;
int index = savePath.lastIndexOf("/");
String fileName = savePath.substring(index + 1);
fileName = fileName.replaceAll("%20","");
fileName = fileName.replaceAll(" ","");
String rpath = getRealPath(ctx)+ savePath;//服务器上的文件真实路径
return rpath;
}
//获取不 同系统下的文件真正路径
public static String getRealPath(ServletContext cxt){
String rpath = "";
if(getSystemName().equals("linux")){//linux
rpath = File.separator + cxt.getRealPath(File.separator);
}else{//windows
rpath = cxt.getRealPath(File.separator);
}
return rpath;
}
//获取系统名称
public static String getSystemName(){
if(System.getProperty("file.separator").equals("/")){//linux
return "linux";
}else{
return "windows";
}
}