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;
virmach这是第二波出这种一次性周期的VPS了,只需要缴费1一次即可,用完即抛,也不允许你在后面续费。本次促销的是美国西海岸的圣何塞和美国东海岸的水牛城,周期为6个月,过后VPS会被自动且是强制性取消。需要临时玩玩的,又不想多花钱的用户,可以考虑下!官方网站:https://www.virmach.comTemporary Length Service Specials圣何塞VPS-一次性6个...
CloudCone针对中国农历新年推出了几款特别套餐, 其中2019年前注册的用户可以以13.5美元/年的价格购买一款1G内存特价套餐,以及另外提供了两款不限制注册时间的用户可购买年付套餐。CloudCone是Quadcone旗下成立于2017年的子品牌,提供VPS及独立服务器租用,也是较早提供按小时计费VPS的商家之一,支持使用PayPal或者支付宝等付款方式。下面列出几款特别套餐配置信息。CP...
近日CloudCone商家对旗下的大硬盘VPS云服务器进行了少量库存补货,也是悄悄推送了一批便宜VPS云服务器产品,此前较受欢迎的特价20美元/年、1核心1G内存1Gbps带宽的VPS云服务器也有少量库存,有需要美国便宜大硬盘VPS云服务器的朋友可以关注一下。CloudCone怎么样?CloudCone服务器好不好?CloudCone值不值得购买?CloudCone是一家成立于2017年的美国服务...