java保存文件

package com.singlee.util;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import org.apache.commons.io.FileUtils;

/**
 * 保存文件到服务器
 * 
 * @author mingzl
 * 
 */
public class SaveFile {

	/**
	 * 改名称的方法,(新名称=原名称+_+年月日时分秒时间+_+八位随机数)
	 * 
	 * @param oldname
	 *            原名称
	 * @return newName 新名称
	 */
	public String changName(String oldName) {

		// 第一步:得到系统时间
		SimpleDateFormat ff = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
		Date d = new Date();
		String time = ff.format(d);
		// 第二步:得到一个随机数
		int rad = new Random().nextInt(900000000);
		// 第三步:得到原文件的后缀名如:.jpg .doc .txt
		String lastName = oldName.substring(oldName.lastIndexOf("."));
		// 第四步:拼装新名(保证唯一性)
		String newName = oldName + "_" + time + "_" + rad + lastName;
		System.out.println("文件新名称:" + newName);
		return newName;
	}

	/**
	 * 保存文件
	 * 
	 * @param file
	 *            保存的文件
	 * @param filename
	 *            保存文件的名称(参考)
	 * @param savepath
	 *            文件保存的路径
	 * @param saveasnewname
	 *            是否以新名字保存
	 * @return boolean (true:保存成功;false:保存失败)
	 */
	public boolean save(File file, String filename, String savepath,
			boolean saveasnewname) {
		filename = saveasnewname ? changName(filename) : filename;
		File f = new File(savepath, filename);
		try {
			FileUtils.copyFile(file, f);
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值