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

久久网云-目前最便宜的国内,香港,美国,日本VPS云服务器19.9元/月起,三网CN2,2天内不满意可以更换其他机房机器,IP免费更换!。

久久网云怎么样?久久网云好不好?久久网云是一家成立于2017年的主机服务商,致力于为用户提供高性价比稳定快速的主机托管服务,久久网云目前提供有美国免费主机、香港主机、韩国服务器、香港服务器、美国云服务器,香港荃湾CN2弹性云服务器。专注为个人开发者用户,中小型,大型企业用户提供一站式核心网络云端服务部署,促使用户云端部署化简为零,轻松快捷运用云计算!多年云计算领域服务经验,遍布亚太地区的海量节点为...

Virtono:€23.7/年,KVM-2GB/25GB/2TB/洛杉矶&达拉斯&纽约&罗马尼亚等

Virtono最近推出了夏季促销活动,为月付、季付、半年付等提供9折优惠码,年付已直接5折,而且下单后在LET回复订单号还能获得双倍内存,不限制付款周期。这是一家成立于2014年的国外VPS主机商,提供VPS和服务器租用等产品,商家支持PayPal、信用卡、支付宝等国内外付款方式,可选数据中心包括罗马尼亚、美国洛杉矶、达拉斯、迈阿密、英国和德国等。下面列出几款VPS主机配置信息,请留意,下列配置中...

域名注册需要哪些条件(新手注册域名考虑的问题)

今天下午遇到一个网友聊到他昨天新注册的一个域名,今天在去使用的时候发现域名居然不见。开始怀疑他昨天是否付款扣费,以及是否有实名认证过,毕竟我们在国内域名注册平台注册域名是需要实名认证的,大概3-5天内如果不验证那是不可以使用的。但是如果注册完毕的域名找不到那也是奇怪。同时我也有怀疑他是不是忘记记错账户。毕竟我们有很多朋友在某个商家注册很多账户,有时候自己都忘记是用哪个账户的。但是我们去找账户也不办...

上传工具为你推荐
深圳公交车路线深圳公交线路中国论坛大全安徽论坛都有哪些?bluestacks安卓模拟器BlueStacks如何安装使用?网站运营网站运营的工作做什么照片转手绘如何把真人图片用photoshop做成手绘图片天天酷跑刷金币天天酷跑怎么刷金币?人人逛街人人逛街评论怎么不显示链接了?好像4月28日就不能显示了。是什么原因呢?2012年正月十五2012年正月十五 几月几号虚拟机软件下载谁有好的虚拟机软件?gbk编码表如何制作GBK与Unicode的对照表
查域名 vps代理 踢楼 kvmla godaddy优惠券 密码泄露 火车票抢票攻略 主机合租 linux空间 200g硬盘 789电视网 cdn加速原理 南通服务器 免费高速空间 drupal安装 七夕快乐英语 电信网络测速器 石家庄服务器 碳云 美国主机 更多