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;
PacificRack最近促销上瘾了,活动频繁,接二连三的追加便宜VPS秒杀,PacificRack在 7月中下旬已经推出了五款秒杀VPS套餐,现在商家又新增了三款更便宜的特价套餐,年付低至7.2美元,这已经是本月第三波促销,带宽都是1Gbps。PacificRack 7月秒杀VPS整个系列都是PR-M,也就是魔方的后台管理。2G内存起步的支持Windows 7、10、Server 2003\20...
SpinServers服务商也不算是老牌的服务商,商家看介绍是是2018年成立的主机品牌,隶属于Majestic Hosting Solutions LLC旗下。商家主要经营独立服务器租用和Hybrid Dedicated服务器等,目前包含的数据中心在美国达拉斯、圣何塞机房,自有硬件和IP资源等,商家还自定义支持用户IP广播到机房。看到SpinServers推出了美国独服的夏季优惠促销活动,最低月...
vpsdime上了新产品系列-Windows VPS,配置依旧很高但是价格依旧是走低端线路。或许vpsdime的母公司Nodisto IT想把核心产品集中到vpsdime上吧,当然这只是站长个人的猜测,毕竟winity.io也是专业卖Windows vps的,而且也是他们自己的品牌。vpsdime是一家新上来不久的奇葩VPS提供商,实际是和backupspy以及crowncloud等都是同一家公司...