文件工具类,一些常用的文件方法
2018-02-17 21:03
507 查看
[java] view plain copy 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();
}
}
}
}
}
/**
* 将文件输出到客户端,一般用于预览
* @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();
}
}
}
}
}
相关文章推荐
- 文件工具类,一些常用的文件方法
- 时间日期工具类 封装工作中常用的一些时间日期计算方法(转+自己修改)
- Java常用的一些多媒体文件基本操作方法简介
- masm中list文件和宏的一些常用编译调试查看方法
- OC学习笔记16--OC中文件操作-NSFileManager的使用及文件操作中常用的一些字符串方法
- masm中list文件和宏的一些常用编译调试查看方法
- masm中list文件和宏的一些常用编译调试查看方法
- C#File类常用的文件操作方法(创建、移动、删除、复制等) File类,是一个静态类,主要是来提供一些函数库用的。静态实用类,提供了很多静态的方法,支持对文件的基本操作,包括创建,拷贝,移动,删除和
- 一些常用的方法工具类
- 常用文件(夹)处理方法工具类
- Android文件、内存、SDCard管理常用工具类、方法
- commons-io中操作文件的一些常用方法
- 文件操作工具类 FileUtils常用方法
- 常用JSP文件上传,下载的一些方法
- 常用JSP文件上传,下载的一些方法
- python 判断文件和文件夹是否存在的方法 和一些文件常用操作符
- 自己写的JSON 工具类,封装一些常用的方法
- Date 工具类(包含常用的一些时间方法)
- C#遍历指定文件夹中的所有文件,DirectoryInfo和FileInfo的一些常用方法
- [Android开发工具类]对控件的一些常用方法