packagecom.beisun.mbp.util;importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.ByteArrayOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.io.UnsupportedEncodingException;importjava.nio.channels.FileChannel;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.springframework.util.FileCopyUtils;importorg.springframework.web.multipart.MultipartFile;public classFileUtil {/*** 上传文件
*@paramfilePath 上传文件目录
*@paramfileName 保存的文件名称
*@paramfile 文件流类
*@paramrequest 请求
*@throwsIOException IO异常*/
public static voidupdoadFile(String filePath,String fileName, MultipartFile file,
HttpServletRequest request)throwsIOException{//如果文件夹不存在,则创建文件夹
File dirPath = newFile(filePath);if(!dirPath.exists()){
dirPath.mkdir();
}
File uploadFile= new File(dirPath +"/"+fileName);
FileCopyUtils.copy(file.getBytes(), uploadFile);
}/*** 复制文件
*@paramlocalFileName 原文件地址和文件名
*@paramtmpFileName 目标文件地址和文件名
*@throwsIOException*/
public static void copyFile(String localFileName,String tmpFilePath,String tmpFileName) throwsIOException{
File localFile= newFile(localFileName);
File tmpFile= newFile(tmpFilePath,tmpFileName);
FileCopyUtils.copy(localFile, tmpFile);
}/*** 删除文件
*@paramfileName 文件地址和文件名
*@throwsIOException*/
public static void deleteFile(String fileName) throwsIOException{
File localFile= newFile(fileName);
localFile.delete();
}/*** 下载文件,是向页面输出流,不返回流
*@paramfilePath 文件服务器存储目录
*@paramdownloadName 下载文件保存的文件名
*@paramfileName 服务器存储文件名
*@paramrequest
*@paramresponse
*@throwsUnsupportedEncodingException*/@SuppressWarnings("static-access")public static void downloadFile(String filePath,String downloadName,String fileName,HttpServletRequest request,HttpServletResponse response) throwsUnsupportedEncodingException{
fileName= new java.net.URLDecoder().decode(fileName, "utf-8");
downloadName= new java.net.URLDecoder().decode(downloadName, "utf-8");
String path= filePath+fileName;
response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + new String((downloadName).getBytes("GBK"), "iso8859-1"));try{//以流的形式下载文件
InputStream fis = new BufferedInputStream(newFileInputStream(path));byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
OutputStream toClient= newBufferedOutputStream(response.getOutputStream());
toClient.write(buffer);
toClient.flush();
toClient.close();
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
}/*** 获取文件的is
*@paramfilePath 文件服务器存储目录
*@paramfileName 服务器存储文件名
*@paramrequest
*@paramresponse
*@throwsUnsupportedEncodingException*/@SuppressWarnings("static-access")public static InputStream getFileIS(String filePath,String fileName,HttpServletRequest request,HttpServletResponse response) throwsUnsupportedEncodingException{
fileName= new java.net.URLDecoder().decode(fileName, "utf-8");
String path= filePath+fileName;
InputStream fis= null;try{//以流的形式下载文件
fis = new BufferedInputStream(newFileInputStream(path));
}catch(FileNotFoundException e) {
e.printStackTrace();
}returnfis;
}/*** 保存文件
*@paramdatas 文件数据
*@parampathName 文件路径*/
public static void saveFile(byte[] datas,String pathName){
File file= newFile(pathName);//寤虹珛杈撳嚭瀛楄妭娴�
FileOutputStream fos;try{
fos= newFileOutputStream(file);//鐢‵ileOutputStream 鐨剋rite鏂规硶鍐欏叆瀛楄妭鏁扮粍
fos.write(datas);//涓轰簡鑺傜渷IO娴佺殑寮�攢锛岄渶瑕佸叧闂�
fos.close();
}catch(Exception e) {
e.printStackTrace();
}
}/*** 获取文件数据
*@parampathName 文件路径
*@return
*/
public static byte[] getFile(String pathName){
File file= newFile(pathName);byte[] buffer = null;try{
FileInputStream fis= newFileInputStream(file);
ByteArrayOutputStream bos= new ByteArrayOutputStream(1000);byte[] b = new byte[1000];intn;while ((n = fis.read(b)) != -1) {
bos.write(b,0, n);
}
fis.close();
bos.close();
buffer=bos.toByteArray();
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}returnbuffer;
}/*** 计算文件大小文件大小
*@paramfilePath 文件路径例如:E:\\imgData\\afr\\9211496189393485.jpg
*@return文件大小 Kb*/
public static longGetFileSize(String filePath){long fileSize=0l;
FileChannel fc= null;try{
File f= newFile(filePath);if (f.exists() &&f.isFile()){
FileInputStream fis= newFileInputStream(f);
fc=fis.getChannel();
fileSize=fc.size()/1024;//logger.info(fileSize);
}else{//logger.info("file doesn't exist or is not a file");
}
}catch(FileNotFoundException e) {//logger.error(e);
} catch(IOException e) {//logger.error(e);
} finally{if (null!=fc){try{
fc.close();
}catch(IOException e){//logger.error(e);
}
}
}returnfileSize;
}/*** getImage 根据图片的路径获取图片给前台
*@authorsunjianbo
* @time 2016年8月25日上午10:41:37
*@paramresponse
*@paramrequest
*@paramfilePath
*@throwsException*/
//@RequestMapping(value = "/getImage.do", method = RequestMethod.GET)
public voidgetImage(HttpServletResponse response, HttpServletRequest request,String filePath)throwsException {
BufferedOutputStream bos= null;try{
bos= newBufferedOutputStream(response.getOutputStream());byte[] data =getFile(filePath);
bos.write(data);
bos.flush();
}catch(Exception e) {
e.printStackTrace();
}finally{if (bos != null)
bos.close();
}
}
}