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

RAKsmartCloud服务器,可自定义配置月$7.59

RAKsmart商家一直以来在独立服务器、站群服务器和G口和10G口大端口流量服务器上下功夫比较大,但是在VPS主机业务上仅仅是顺带,尤其是我们看到大部分主流商家都做云服务器,而RAKsmart商家终于开始做云服务器,这次试探性的新增美国硅谷机房一个方案。月付7.59美元起,支持自定义配置,KVM虚拟化,美国硅谷机房,VPC网络/经典网络,大陆优化/精品网线路,支持Linux或者Windows操作...

DogYun春节优惠:动态云7折,经典云8折,独立服务器月省100元,充100送10元

传统农历新年将至,国人主机商DogYun(狗云)发来了虎年春节优惠活动,1月31日-2月6日活动期间使用优惠码新开动态云7折,经典云8折,新开独立服务器可立减100元/月;使用优惠码新开香港独立服务器优惠100元,并次月免费;活动期间单笔充值每满100元赠送10元,还可以参与幸运大转盘每日抽取5折码,流量,余额等奖品;商家限量推出一款年付特价套餐,共100台,每个用户限1台,香港VPS年付199元...

Central美国65折优惠,美国达拉斯机房VPS季付赠送双倍内存

Central美国独立日活动正在进行中,旗下美国达拉斯机房VPS 65折优惠,季付赠送双倍内存(需要发工单),Central租用的Hivelocity的机房,只支持信用卡和加密货币付款,不支持paypal,需要美国独服的可以谨慎入手试试。Central怎么样?Central便宜服务器,Central自称成立于2019年,主营美国达拉斯机房Linux vps、Windows vps、专用服务器和托管...

上传工具为你推荐
安装程序配置服务器失败win10安装程序配置服务器失败怎么办bluestacksBluestacks安卓模拟器是什么机型的?数码资源网手机练习打字的软件中小企业信息化中小企业信息化途径有哪些网易公开课怎么下载哪位高手指导一下,如何下载网易公开课啊?vbscript教程vbs 学习方法以及 vbs 实例 有编程基础qq空间打扮如何打扮QQ空间?2012年正月十五农历2012年正月15早上9点多生的!命里缺什么!是什么命相网络广告投放网络广告投放有哪些技巧?系统分析员考系统分析员有什么好处?
域名批量查询 buyvm winhost rackspace 美国仿牌空间 512m 42u标准机柜尺寸 云图标 丹弗 500m空间 本网站服务器在美国 帽子云 河南m值兑换 佛山高防服务器 爱奇艺vip免费试用7天 最好的qq空间 metalink 外贸空间 forwarder alexa世界排名 更多