文件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;

腾讯云轻量服务器两款低价年付套餐 2核4GB内存8M带宽 年74元

昨天,有在"阿里云秋季促销活动 轻量云服务器2G5M配置新购年60元"文章中记录到阿里云轻量服务器2GB内存、5M带宽一年60元的活动,当然这个也是国内机房的。我们很多人都清楚备案是需要接入的,如果我们在其他服务商的域名备案的,那是不能解析的。除非我们不是用来建站,而是用来云端的,是可以用的。这不看到其对手腾讯云也有推出两款轻量服务器活动。其中一款是4GB内存、8M带宽,这个比阿里云还要狠。这个真...

EdgeNat 新年开通优惠 - 韩国独立服务器原生IP地址CN2线路七折优惠

EdgeNat 商家在之前也有分享过几次活动,主要提供香港和韩国的VPS主机,分别在沙田和首尔LG机房,服务器均为自营硬件,电信CN2线路,移动联通BGP直连,其中VPS主机基于KVM架构,宿主机采用四路E5处理器、raid10+BBU固态硬盘!最高可以提供500Gbps DDoS防御。这次开年活动中有提供七折优惠的韩国独立服务器,原生IP地址CN2线路。第一、优惠券活动EdgeNat优惠码(限月...

易探云:香港CN2云服务器低至18元/月起,183.60元/年

易探云怎么样?易探云最早是主攻香港云服务器的品牌商家,由于之前香港云服务器性价比高、稳定性不错获得了不少用户的支持。易探云推出大量香港云服务器,采用BGP、CN2线路,机房有香港九龙、香港新界、香港沙田、香港葵湾等,香港1核1G低至18元/月,183.60元/年,老站长建站推荐香港2核4G5M+10G数据盘仅799元/年,性价比超强,关键是延迟全球为50ms左右,适合国内境外外贸行业网站等,如果需...

上传工具为你推荐
回收站在哪手机回收站在哪里打开最新qq空间代码QQ空间代码有哪些???二叉树遍历二叉树三种遍历方式原则?手机区号打电话怎么加区号?镜像文件是什么什么叫镜像文件,作用是什么?qq怎么发邮件怎样在QQ上发送邮件?xp系统停止服务xp系统停止服务怎么办?idc前线永恒之塔内侧 删档吗 ?2012年正月十五2012年正月十五 几月几号iphone6上市时间苹果6什么时候在中国大陆上市
西部数码vps 域名服务dns的主要功能为 cn域名备案 荣耀欧洲 technetcal Dedicated 韩国电信 ev证书 好看qq空间 dux 域名与空间 沈阳主机托管 免费ftp qq金券 万网空间 深圳主机托管 以下 挂马检测工具 内存 neicun 更多