public class FileUtil {
/**
* 将文件输出到客户端,一般用于预览
* @param file
* @param contentType 图片 image/jpeg
* 视频 audio/mpeg
* 应用程序 application/octet-stream
*/
public static void renderFileToClient(File file, String contentType) {
OutputStream toClient = null;
InputStream fis = null;
byte[] buffer = null;
HttpServletResponse response = SpringMVCUtil.getResponse();
try {
fis = new BufferedInputStream(new FileInputStream(file));
buffer = new byte[1024];
response.reset();
response.setContentLength(Long.valueOf(file.length()).intValue());
response.setContentType(contentType);
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
toClient = new BufferedOutputStream(response.getOutputStream());
int bytesRead;
while (-1 != (bytesRead = fis.read(buffer, 0, buffer.length))) {
toClient.write(buffer, 0, bytesRead);
toClient.flush();
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (toClient != null) {
try {
toClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 获取附件存储相对路径
*
* @param attachmentConfig
* @return
*/
public static String getRelativePath() {
StringBuilder sBuilder=new StringBuilder();
// 模块名未定
String dateStr = DateUtil.getCurrDateString().replaceAll("-", "");
sBuilder.append(dateStr.substring(0, 4) + "/");
sBuilder.append(dateStr.substring(4, 6) + "/");
sBuilder.append(dateStr.substring(6, 8) + "/");
return sBuilder.toString();
}
/**
* 获取临时目录
*
* @return
*/
public static String getTempDir() {
String tempPath = System.getProperty("java.io.tmpdir");
File tempFile = new File(tempPath);
if (!tempFile.exists()) {
tempFile.mkdirs();
}
return tempFile.getAbsolutePath() + "/";
}
/**
* 下载文件
*
* @date 2015-8-25 下午9:13:13
* @param srcPath
*/
public static void download(String downLoadPath) {
HttpServletResponse response = SpringMVCUtil.getResponse();
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("UTF-8");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
File file = new File(downLoadPath);
try {
response.setContentType("application/x-msdownload;");
response.setHeader("Content-disposition", "attachment; filename="+ new String(file.getName().getBytes("utf-8"), "ISO8859-1"));
response.setHeader("Content-Length", String.valueOf(file.length()));
bis = new BufferedInputStream(new FileInputStream(downLoadPath));
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 (Exception e) {
e.printStackTrace();
} finally {
if (bis != null)
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
if (bos != null)
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 向客户端输出浏览内容
*
* @date 2015-8-31 下午8:10:48
* @param file 视频文件
* @param contentType "audio/mpeg"
*/
public static void renderMediaToClient(File file, String contentType) {
OutputStream toClient = null;
InputStream fis = null;
byte[] buffer = null;
HttpServletResponse response = SpringMVCUtil.getResponse();
try {
fis = new BufferedInputStream(new FileInputStream(file));
buffer = new byte[1024];
response.reset();
response.setContentLength(Long.valueOf(file.length()).intValue());
response.setContentType(contentType);
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
toClient = new BufferedOutputStream(response.getOutputStream());
int bytesRead;
while (-1 != (bytesRead = fis.read(buffer, 0, buffer.length))) {
toClient.write(buffer, 0, bytesRead);
toClient.flush();
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (toClient != null) {
try {
toClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}