操作文件类二

//操作文件类二

/**
 * 项目名称:uploadmanager
 * 文件名称:FileUtil.java
 * 版权所有:版权所有(C) 2009-2049
 * 公   司:
 * 编写日期:Jun 9, 2009_3:46:17 PM
 * 作   者:anthony
 */
package guokang.rdcenter.upload.util;

import guokang.rdcenter.upload.model.Uploadmanager;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.upload.FormFile;

/**
 *
 * FileUtil
 * Jun 9, 2009_3:46:17 PM
 * @author anthony
 */
public class FileUtil
{
 private static UUID uid=null;
 private static BufferedInputStream bis  = null;
 
 private static BufferedOutputStream bos  = null;
 
 private static Logger    utilLog = Logger.getLogger("Web");
 
 /**
  * 字符集设置的处理方法
  * @param fileName 文件名
  * @return Content-Type类型
  * getContentType
  * Jun 9, 2009_4:20:32 PM
  * String
  * @author anthony
  */
 public static String getContentType(String fileName)
 {
  String fileNameTmp = fileName.toLowerCase();
  String ret = "";
  if (fileNameTmp.endsWith("txt"))
  {
   ret = "text/plain";
  }
  if (fileNameTmp.endsWith("gif"))
  {
   ret = "image/gif";
  }
  if (fileNameTmp.endsWith("jpg"))
  {
   ret = "image/jpeg";
  }
  if (fileNameTmp.endsWith("jpeg"))
  {
   ret = "image/jpeg";
  }
  if (fileNameTmp.endsWith("jpe"))
  {
   ret = "image/jpeg";
  }
  if (fileNameTmp.endsWith("zip"))
  {
   ret = "application/zip";
  }
  if (fileNameTmp.endsWith("rar"))
  {
   ret = "application/rar";
  }
  if (fileNameTmp.endsWith("doc"))
  {
   ret = "application/msword";
  }
  if (fileNameTmp.endsWith("ppt"))
  {
   ret = "application/vnd.ms-powerpoint";
  }
  if (fileNameTmp.endsWith("xls"))
  {
   ret = "application/vnd.ms-excel";
  }
  if (fileNameTmp.endsWith("html"))
  {
   ret = "text/html";
  }
  if (fileNameTmp.endsWith("htm"))
  {
   ret = "text/html";
  }
  if (fileNameTmp.endsWith("tif"))
  {
   ret = "image/tiff";
  }
  if (fileNameTmp.endsWith("tiff"))
  {
   ret = "image/tiff";
  }
  if (fileNameTmp.endsWith("pdf"))
  {
   ret = "application/pdf";
  }
  return ret;
 }
 
 /**
  * 读文件的处理方法
  * @param filePath 文件路径名
  * @param fileName 文件名
  * readFile
  * Jun 9, 2009_4:28:20 PM
  * void
  * @author anthony
  */
 public static void readFile(HttpServletResponse response , String filePath ,
  String fileName)
 {
  try
  {
   //缓从输入流对象
   bis = new BufferedInputStream(new FileInputStream(filePath));
   
   //缓从输出流对象
   bos = new BufferedOutputStream(response.getOutputStream());
   
   //文件长度设置
   response.setContentLength(bis.available());
   
   // 文件的存储类型,打开文件下载框是文件名乱码的问题
   response.setContentType(getContentType(fileName+";charset=GBK"));
   
   //打开文件下载框是文件名乱码的问题
   response.setHeader("Content-disposition" , "attachment;filename="
    +URLEncoder.encode(fileName , "GBK"));
   
   int b = 0;
   byte[] buff = new byte[1024*1024];
   while ((b = bis.read(buff))!= -1)
   {
    bos.write(buff , 0 , b);//将文件发送到客户端
   }
   //关闭流
   bos.flush();
   bos.close();
   bis.close();
  }
  catch (FileNotFoundException fnfe)
  {
   utilLog.debug(fnfe.getCause()+"_"+fnfe.getLocalizedMessage());
  }
  catch (IOException ioe)
  {
   utilLog.debug(ioe.getCause()+"_"+ioe.getLocalizedMessage());
  }
 }
 
 /**
  * 文件保存的处理方法
  * @param ff 文件对象
  * @param uploadFile pojo
  * saveFile
  * Jun 9, 2009_3:55:42 PM
  * void
  * @author anthony
  * @throws ParseException
  */
 public static Uploadmanager saveFile(FormFile ff , Uploadmanager um,String root)
  throws ParseException
 {
  uid=UUID.randomUUID();
  StringBuilder sb=new StringBuilder(16);
  sb.append(root);
  
  System.out.println("content_"+ff.getContentType()+"_");
  
  double tempSize = ff.getFileSize()/1024.0;// 文件的KB数
  float filesize = Float.valueOf(String.valueOf(tempSize));
  um.setFilename(ff.getFileName());
  um.setFilesize(filesize);
  um.setFilepath(sb.append("a").toString());
  return um;
 }
 
 /**
  * 写文件的处理方法
  * @param file 文件对象
  * @param path 文件所在的目录
  * writeFile
  * Jun 9, 2009_4:02:18 PM
  * void
  * @author anthony
  */
 public static void writeFile(FormFile file , String path)
 {
  try
  {
   FormFile f = file;
   bis = new BufferedInputStream(f.getInputStream());
   
   //获取文件流
   bos = new BufferedOutputStream(new FileOutputStream(path+"/"
    +f.getFileName()));//新建输出流对象
   
   int bytesRead = 0;
   byte[] buff = new byte[1024*1024]; //定义缓冲区
   
   //开始保存文件
   while ((bytesRead = bis.read(buff))!= -1)
   {
    //把读取进来的数据保存到缓冲区,然后再输出到文件中
    bos.write(buff , 0 , bytesRead);
   }
   //关闭流
   bos.flush();
   bos.close();
   bis.close();
  }
  catch (FileNotFoundException ffe)
  {
   utilLog.debug(ffe.getCause()+"_"+ffe.getLocalizedMessage());
  }
  catch (IOException ioe)
  {
   utilLog.debug(ioe.getCause()+"_"+ioe.getLocalizedMessage());
  }
 }
 
 /**
  * 截取特定长度的路径
  * @param str 文件路径
  * @return   /*.jsp
  * strEnd
  * Dec 22, 2009_1:43:52 PM
  * String
  * @author Reserach_Center_Anthony
  */
 public static String strEnd(String str)
 {
  StringBuffer sb = new StringBuffer();
  int starone = str.lastIndexOf(".");
  sb.append(str.substring(starone+1 , str.length()));
  return sb.toString();
 }
 public static void main(String[] args)
 {
  System.out.println(strEnd("f:\\huangt.ao.doc"));
 }
 /**
  * 
  * Jun 9, 2009_3:46:17 PM
  * @author anthony
  */
 public FileUtil()
 {}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值