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

美国200G美国高防服务器16G,800元

美国高防服务器提速啦专业提供美国高防服务器,美国高防服务器租用,美国抗攻击服务器,高防御美国服务器租用等。我们的海外高防服务器带给您坚不可摧的DDoS防护,保障您的业务不受攻击影响。HostEase美国高防服务器位于加州和洛杉矶数据中心,均为国内访问速度最快最稳定的美国抗攻击机房,带给您快速的访问体验。我们的高防服务器配有最高层级的DDoS防护系统,每款抗攻击服务器均拥有免费DDoS防护额度,让您...

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

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

弘速云20.8元/月 ,香港云服务器 2核 1g 10M

弘速云元旦活动本公司所销售的弹性云服务器、虚拟专用服务器(VPS)、虚拟主机等涉及网站接入服务的云产品由具备相关资质的第三方合作服务商提供官方网站:https://www.hosuyun.com公司名:弘速科技有限公司香港沙田直营机房采用CTGNET高速回国线路弹性款8折起优惠码:hosu1-1 测试ip:69.165.77.50​地区CPU内存硬盘带宽价格购买地址香港沙田2-8核1-16G20-...

上传工具为你推荐
绵阳电信绵阳电信宽带怎么收费的万网核心代理哪里可以注册免费代理?eset最新用户名密码ESET4.0最新用户名和密码ghostxp3GhostXP3电脑公司特别版V499怎么安装ps抠图技巧如何使用PS抠图如何建立自己的网站怎么创建自己的网站godaddy通过什么网址可以查godaddy的域名信息ios7固件下载ios 7及以上固件请在设备上点信任在哪点创维云电视功能创维健康云电视有什么功能?ios系统ios系统和安卓系统对比起来有什么优点和缺点?
出租服务器 新网域名解析 阿里云os 新加坡主机 分销主机 韩国加速器 腾讯云数据库 美国php主机 智能骨干网 七夕快乐英文 jsp空间 电信虚拟主机 ftp免费空间 微软服务器操作系统 paypal注册教程 google台湾 网页提速 中国电信网络测速 免费asp空间 数据库空间 更多