package com.*.hylm.web.manager.common.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.*.framework.core.Result;
/**
* 文件工具类
*/
public class FileUtil
{
private static Logger logger = LoggerFactory.getLogger(FileUtil.class);
/**
* 文件分隔符
*/
private static final char separtator = File.separatorChar;
/**
* 为了安全性考虑,下载的文件路径必须是上传时所规定的上传路径,其他路径都非法,不可下载
*/
private static final String fileDir = separtator + "upload";
/**
* 国际化资源文件加载
*/
private static ResourceBundle messgeResource = ResourceBundle.getBundle(
"conf" + separtator + "hylmMessage", Locale.getDefault());
/**
*
* @Title: downLoadFile
* @Description: 文件下载公共方法
*
* @param request
* HttpServletRequest
* @param response
* HttpServletResponse
* @param relativePath
* 相对于upload/的路径 如:relativePath=vehicle/XXX.XXX
* @return: Result 下载结果
* @Author: boliur
* @date: 2015年5月8日 下午7:00:09
*/
public static Result downLoadFile(HttpServletRequest request,
HttpServletResponse response, String relativePath)
{
Result result = new Result();
result.setSuccess(true);
result.setMsgCode("hylm.110000");
result.setMsg(messgeResource.getString("hylm.110000"));
String filePath = request.getSession().getServletContext()
.getRealPath(fileDir)
+ separtator + relativePath;
File file = new File(filePath);
if (file.isDirectory() || !file.exists())
{
result.setSuccess(false);
result.setMsgCode("hylm.110001");
result.setMsg(messgeResource.getString("hylm.110001"));
return result;
}
// 文件名
String fileName = filePath
.substring(filePath.lastIndexOf(separtator) + 1);
// String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
BufferedOutputStream output = null;
BufferedInputStream input = null;
try
{
// 文件类型
response.setContentType("unknown");
// 设置文件名
response.setHeader("Content-Disposition", "attachment;filename=\""
+ new String(fileName.getBytes("GB2312"), "ISO8859-1")
+ "\"");
response.setContentLength((int) file.length());
byte[] buffer = new byte[1024];
output = new BufferedOutputStream(response.getOutputStream());
input = new BufferedInputStream(new FileInputStream(file));
int n = -1;
// 遍历,开始下载
while ((n = input.read(buffer, 0, 1024)) > -1)
{
output.write(buffer, 0, n);
}
output.flush();
response.flushBuffer();
}
catch (Exception e)
{
result.setSuccess(false);
result.setMsgCode("hylm.110002");
result.setMsg(messgeResource.getString("hylm.110002"));
logger.error("File exception:" + e.getMessage());
}
finally
{
// 关闭流
if (input != null)
try
{
input.close();
}
catch (IOException e)
{
result.setSuccess(false);
result.setMsgCode("hylm.110003");
result.setMsg(messgeResource.getString("hylm.110003"));
logger.error("The inputstream is colsed exception:"
+ e.getMessage());
}
if (output != null)
try
{
output.close();
}
catch (IOException e)
{
result.setSuccess(false);
result.setMsgCode("hylm.110004");
result.setMsg(messgeResource.getString("hylm.110004"));
logger.error("The outputstream is colsed exception:"
+ e.getMessage());
}
}
return result;
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.*.framework.core.Result;
/**
* 文件工具类
*/
public class FileUtil
{
private static Logger logger = LoggerFactory.getLogger(FileUtil.class);
/**
* 文件分隔符
*/
private static final char separtator = File.separatorChar;
/**
* 为了安全性考虑,下载的文件路径必须是上传时所规定的上传路径,其他路径都非法,不可下载
*/
private static final String fileDir = separtator + "upload";
/**
* 国际化资源文件加载
*/
private static ResourceBundle messgeResource = ResourceBundle.getBundle(
"conf" + separtator + "hylmMessage", Locale.getDefault());
/**
*
* @Title: downLoadFile
* @Description: 文件下载公共方法
*
* @param request
* HttpServletRequest
* @param response
* HttpServletResponse
* @param relativePath
* 相对于upload/的路径 如:relativePath=vehicle/XXX.XXX
* @return: Result 下载结果
* @Author: boliur
* @date: 2015年5月8日 下午7:00:09
*/
public static Result downLoadFile(HttpServletRequest request,
HttpServletResponse response, String relativePath)
{
Result result = new Result();
result.setSuccess(true);
result.setMsgCode("hylm.110000");
result.setMsg(messgeResource.getString("hylm.110000"));
String filePath = request.getSession().getServletContext()
.getRealPath(fileDir)
+ separtator + relativePath;
File file = new File(filePath);
if (file.isDirectory() || !file.exists())
{
result.setSuccess(false);
result.setMsgCode("hylm.110001");
result.setMsg(messgeResource.getString("hylm.110001"));
return result;
}
// 文件名
String fileName = filePath
.substring(filePath.lastIndexOf(separtator) + 1);
// String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
BufferedOutputStream output = null;
BufferedInputStream input = null;
try
{
// 文件类型
response.setContentType("unknown");
// 设置文件名
response.setHeader("Content-Disposition", "attachment;filename=\""
+ new String(fileName.getBytes("GB2312"), "ISO8859-1")
+ "\"");
response.setContentLength((int) file.length());
byte[] buffer = new byte[1024];
output = new BufferedOutputStream(response.getOutputStream());
input = new BufferedInputStream(new FileInputStream(file));
int n = -1;
// 遍历,开始下载
while ((n = input.read(buffer, 0, 1024)) > -1)
{
output.write(buffer, 0, n);
}
output.flush();
response.flushBuffer();
}
catch (Exception e)
{
result.setSuccess(false);
result.setMsgCode("hylm.110002");
result.setMsg(messgeResource.getString("hylm.110002"));
logger.error("File exception:" + e.getMessage());
}
finally
{
// 关闭流
if (input != null)
try
{
input.close();
}
catch (IOException e)
{
result.setSuccess(false);
result.setMsgCode("hylm.110003");
result.setMsg(messgeResource.getString("hylm.110003"));
logger.error("The inputstream is colsed exception:"
+ e.getMessage());
}
if (output != null)
try
{
output.close();
}
catch (IOException e)
{
result.setSuccess(false);
result.setMsgCode("hylm.110004");
result.setMsg(messgeResource.getString("hylm.110004"));
logger.error("The outputstream is colsed exception:"
+ e.getMessage());
}
}
return result;
}
}