文件struts2上传工具类

上传工具  时间:2021-02-23  阅读:()

s truts 2上传工具类

*@vers ion:$$Rev:35 $$

*@date:$$Date:2014-07-25 16:12:49+0800(Fri,25 Jul 2014)$$

*@las tUp date:$$Author$$

*Modific ation History:

*Date Author Version Description

*-------------------------------------------------------------public class FileUtil {private static final Logger logger=LoggerFactory.getLogger(FileUtil.class);private static final int BUFFER_SIZE=1024;

*取得文件的路径

*@param fileName

*@return String pathpublic static String getFilePath(String fileName) {if(StringUt ils.isNot Blank(fileName)) {fileNam e=fileNam e.replac eAll( \\\\ , /return fileName.substring(0, fileName.lastIndexOf( /));r eturn

*取得文件名不包括扩展名

*@param path文件路径

*@return String不包括扩展名的文件名public static String getFileName(String path) {if(StringUt ils.isNot Blank(path)) {

p ath=p ath.r ep lac eAl l( \\\\ , /if(path.c ontains( . )) {return path.substring(path.lastIndexOf( /)+1,path.lastIndexOf( . ));} els e {return path.substring(path.lastIndexOf( /)+1);r eturn

*取得包括扩展名的文件名

*@param path文件路径

*@return String包括扩展名的文件名public static String getFileNameWithExtension(String path) {if(StringUt ils.isNot Blank(path)) {p ath=p ath.r ep lac eAl l( \\\\ , /return path.substring(path.lastIndexOf( /)+1);r eturn

*取得文件扩展名

*@param path文件路径

*@return String文件扩展名public static String getExtension(String path) {if(StringUt ils.isNot Blank(path)) {return path.substring(path.lastIndexOf( . )+1).toLow erCas e();r eturn

*p单个文件压缩成zip文件/p

*@param in压缩文件输入流

*@param out压缩文件输出流

*@param fileName文件名

*@return true:成功/fals e:失败public static boolean zipF ile(InputStream in,OutputStream out,String fileName) {try{

ZipOutputStream gzout=new ZipOutputStream(out);

ZipEntry entry=new ZipEntry(fileName);gzout.putNextEntry(entry);byte[]buf=new byte[BUFFER_SIZE];int num;w hile((num=in.read(buf)) !=-1) {g z o ut.w r it e(b u f,0,num);gzout.c los e();in.close();

}catch(IOException e) {logger.error(e.getMess age());r eturn fals e;return true;

*p多文件压缩成zip文件/p

*@param files待压缩文件

*@param out压缩文件输出流

*@return true:成功/fals e:失败public static boolean zipFile(List File files,OutputStream out) {try{

ZipOutputStream gzout=new ZipOutputStream(out);

FileInputStream in;byte[]buf;int num;if(file s !=null) {for(File oneFile :files) {if(!(oneFile.ex ists()oneFile.c anRead()))c ontinue;in=new FileInputStream(oneFile);gzout.putNextEntry(new ZipEntry(oneFile.getName()));buf=new byte[BUFFER_SIZE];w hile((num=in.read(buf)) !=-1) {g z o ut.w r it e(b u f,0,num);in.close();gzout.c los e();

}catch(IOException e) {logger.error(e.getMess age());r eturn fals e;return true;

*解压ZIP文件

*@param file zip文件

*@r eturn解压出来的文件名public static String unZipF ile(File file,String extension) {try{

ZipFile zipfile=new ZipFile(file);

Enumeration?extends ZipEntry en=zipfile.entries();

ZipEntry entry=null;

String fileName=null;while(en.hasMoreElements()) {entry=en.nextElement();if(StringUt ils.isNot Blank(extens ion) !getExtens ion(entry.getName()).equals(extens ion)) {c ontinue;fileNam e=entry.g etName();

InputS tream entryis=zip fil e.getInput S tream(entry);

RandomAc c es sFile fos=new RandomAc c es sFile(file.getPath()+entry.getName(),rwint n;byte[]bytes=new byte[BUFFER_SIZE];w hile((n=entryis.read(bytes)) !=-1) {fos.write(bytes,0,n);fos.close();entryis.c lo s e();zipfile.close();

r eturn fileName;

} catch(Exc eption e) {logger.error(e.getMess age());r eturn null;private static void writeFile(File src,File dst) {try{

InputStream in=null;

OutputStream out=null;try{in=new BufferedInputStream(new FileInputStream(src),BUF F ER_S IZ E);out=new BufferedOutputStream(new FileOutputStream(dst),BUF F ER_S IZ E);byte[]buffer=new byte[BUFFER_S IZE];while(in.read(buffer)0) {out.write(buffer);

}final ly{if(nu ll !=in) {in.close();if(nu ll !=o ut) {out.c lo s e();

} catch(Exc eption e) {

e.printStackTrac e();

FileInputStream fr=new FileInputStream(htmlFile);

InputStreamReader brs=new InputStreamReader(fr,utf-8

BufferedReader br=new BufferedReader(brs);

FileOutputStream fos=new FileOutputStream(dst);

BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(fos,UTF8));

String input=null;

//每读一行进行一次写入动作w hile(true)input=br.r e adLin e();if(inp ut==nu ll){break;bw.w rite(input);

//newLine()方法写入与操作系统相依的换行字符依执行环境当时的OS来决定该输出那种换行字符bw.newLine();br.close();bw.close();

Res ourc eBund le rb=Res ourc eBundle.get Bundle( in it

String path=rb.getString(pic ture.path

Date d=new Date();

//生产环境文件保存路径

String fileName=d.getTime()+getFileExp(up loadF ileName);

//开发环境文件保存路径

//String savePath=/upload+/+d.getTime()+getFileExp(up lo adFileName);

//String toSrc=ServletActionCont ext.getS ervletContext().getRealPath(s avePath); //使用時間戳作為文件名

String toSrc=path+/+fileName;

//生产环境文件保存路径

File f=new File(path);

//开发环境文件保存路径

//File f=new File(ServletActionCont ext.getServletContext().getRealPath(upload));logger.info(======================================+toSrc+

=====================

//创建文件夹if(!f.exists()) {f.mkdirs();

File toFile=new File(toSrc);writeFile(up load, toFile);return fileName;

//此方法可上传public static String updateWWWFile(F ile upload,String uploadF ileName,String dir){

Res ourc eBund le rb=Res ourc eBundle.get Bundle( in it

//S tring path=rb.getString(ww w.pic ture.p ath

String loc alP ath=rb.getString(www.pic tur e.path

Date d=new Date();

//生产环境文件保存路径

String fileName=d.getTime()+getFileExp(up loadF ileName);

//开发环境文件保存路径

//String savePath=/upload+/+d.getTime()+getFileExp(up loadFileName);

//String toSrc=ServletActionCont ext.getS ervletContext().getRealPath(s avePath); //使用時間戳作為文件名if(S tringUt ils.isNotEmpty(dir)) {lo c alP ath+=/+dir ;

String toSrc=loc alPath+/+fileName;

//生产环境文件保存路径

File f=new File(localPath);

//开发环境文件保存路径

//File f=new File(ServletActionCont ext.getServletContext().getRealPath(upload));logger.info(======================================+toSrc+

=====================

//创建文件夹if(!f.exists()) {f.mkdirs();

File toFile=new File(toSrc);writeFile(up load, toFile);return fileName;

RAKsmart 黑色星期五云服务器七折优惠 站群服务器首月半价

一年一度的黑色星期五和网络星期一活动陆续到来,看到各大服务商都有发布促销活动。同时RAKsmart商家我们也是比较熟悉的,这次是继双十一活动之后的促销活动。在活动产品中基本上沿袭双11的活动策略,比如有提供云服务器七折优惠,站群服务器首月半价、还有新人赠送红包等活动。如果我们有需要RAKsmart商家VPS、云服务器、独立服务器等产品的可以看看他们家的活动。这次活动截止到11月30日。第一、限时限...

LOCVPS洛杉矶CN2线路KVM上线,洛杉矶/香港云地/香港邦联7折

LOCVPS发来了新的洛杉矶CN2线路主机上线通知,基于KVM架构,目前可与香港云地、香港邦联机房XEN架构主机一起适用7折优惠码,优惠后最低美国洛杉矶CN2线路KVM架构2GB内存套餐月付38.5元起。LOCPVS是一家成立较早的国人VPS服务商,目前提供洛杉矶MC、洛杉矶C3、和香港邦联、香港沙田电信、香港大埔、日本东京、日本大阪、新加坡、德国和荷兰等机房VPS主机,基于KVM或者XEN架构。...

韩国服务器租用优惠点评大全

韩国服务器怎么样?韩国云服务器租用推荐?韩国服务器距离中国近,有天然的地域优势,韩国服务器速度快而且非常稳定!有不少有亚洲市场的外贸公司选择韩国服务器开拓业务,韩国服务器因自身的优势也受到不少用户的青睐。目前的IDC市场上,韩国、香港、美国三个地方的服务器几乎占据了海外服务器的百分之九十以上。韩国服务器相比美国服务器来说速度更快,而相比香港机房来说则带宽更充足,占用市场份额非常大。那么,韩国服务器...

上传工具为你推荐
赵雨润情人节“我们约会吧”电影DVD_我们约会吧高清DVD下载_qvod快播??简体翻译成繁体简体字怎么换成繁体。。?缓冲区溢出教程如何防止高手使用缓冲区溢出?个性qq资料QQ个性资料邮箱打不开怎么办126邮箱打不开怎么办网站运营网络运营具体做什么呢显卡温度多少正常显卡温度多少算正常?qq空间装扮QQ空间装扮天天酷跑刷金币天天酷跑如何刷分刷金币?硬盘人移动硬盘的优缺点
游戏服务器租用 如何注销域名备案 花生壳免费域名 com域名抢注 泛域名绑定 免费博客空间 佛山高防服务器 php空间购买 hktv vip域名 卡巴斯基是免费的吗 海外空间 太原联通测速 lamp什么意思 时间服务器 crontab vim命令 ddos攻击工具 正在登陆游戏服务器 运维堡垒主机 更多