📄 fileio.java
字号:
int index = fileName.lastIndexOf(".");
fileExt = fileName.substring(index, fileName.length());
return fileExt;
}
/**
* getRealFilePath(ServletContext context,String filePath)
* 输入一个文件的相对路径,返回他的绝对路径,需要ServletContext信息
* @param context ServletContext
* @param filePath String
* @return String
*/
public static
String getRealFilePath(ServletContext context, String filePath) {
String realFilePath = context.getRealPath(filePath);
return realFilePath;
}
/**
* deleteFile(String filePath)
* @param realFilePath String
* @return boolean
* 删除文件或目录
*/
public static
boolean deleteFile(String realFilePath) {
File aFile = new File(realFilePath);
return aFile.delete();
}
/**
* checkFilesFolder(String realFilePath)
* 检查文件路径是否存在
* @param realFilePath String
* @return boolean
*/
public static
boolean checkFilesFolder(String realFilePath) {
boolean flag = false;
try {
File aFile = new File(realFilePath);
flag = aFile.exists();
}
catch (Exception ex) {
ex.printStackTrace();
}
return flag;
}
/**
* isAllowFile(String flag, String fileName)
* 检查图形文件名是否合法--*.jpg;*.gif;*.gif
* @param flag String
* @param fileName String
* @return boolean
*/
public static
boolean isAllowFile(String flag, String fileName) {
boolean isAllow = false;
if (flag.equals("pic")) {
fileName = fileName.toLowerCase();
if (fileName.endsWith(".jpg") || fileName.endsWith(".gif")
|| fileName.endsWith(".gif")) {
isAllow = true;
}
}
return isAllow;
}
/**
* creatFile(InputStream stream, String outPutRealPath)
* 以文件流的形式建立文件
* @param stream InputStream
* @param outPutRealPath String
* @throws IOException
* @return boolean
*/
public static
boolean creatFile(InputStream stream, String outPutRealPath) throws java.
io.IOException {
boolean flag = false;
OutputStream bos = null;
try {
bos = new FileOutputStream(outPutRealPath);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ( (bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
flag = true;
}
catch (Exception e) {
System.out.println("建立文件出错!");
}
finally {
stream.close();
bos.close();
}
return flag;
}
/**
* shrinkImage(String inRealPath, String outRealPath,
int outWidth, int outHeigh)
* 将图形文件按照指定最大宽高缩放,如果小于指定宽高,则拷贝
* @param inRealPath String
* @param outRealPath String
* @param outWidth int
* @param outHeigh int
* @return boolean
*/
public static boolean
imageOperate(String inRealPath, String outRealPath,
int outWidth, int outHeigh) {
boolean opFlag = false;
try {
File _file = new File(inRealPath); //读入文件
Image src = javax.imageio.ImageIO.read(_file); //构造Image对象
int width = src.getWidth(null); //得到源图宽
int height = src.getHeight(null); //得到源图长
int newWidth = 1;
int newHeight = 1;
//根据源长宽缩放
if (width < outWidth && height < outHeigh) {
//拷贝原图
copyFile(new File(inRealPath), new File(outRealPath));
}
else {
if (width >= height) {
newWidth = outWidth;
newHeight = outWidth * height / width;
}
else {
newHeight = outHeigh;
newWidth = outHeigh * width / height;
}
//绘制缩小后的图
BufferedImage tag = new BufferedImage(newWidth, newHeight,
BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0, newWidth, newHeight, null);
//输出到文件流
FileOutputStream out = new FileOutputStream(outRealPath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
//近JPEG编码
encoder.encode(tag);
out.close();
}
opFlag = true;
}
catch (Exception ex) {
ex.printStackTrace();
}
return opFlag;
}
/**
* getDirectoryFilesNum(String realPath)
* 获得一个目录下的文件数量
* @param realPath String
* @return int
*/
public static int getDirectoryFilesNum(String realPath) {
int filesNum = 0;
if (checkFilesFolder(realPath)) {
File directory = new File(realPath);
if (directory.isDirectory()) {
File[] entries = directory.listFiles();
filesNum = entries.length;
}
}
return filesNum;
}
/**
* getDirectoryFilesName(String realPath)
* 获得该目录下所有文件名字(包含扩展名)
* @param realPath String
* @return Vector
*/
@SuppressWarnings("unchecked")
public static Vector getDirectoryFilesName(String realPath) {
Vector theVector = new Vector();
if (checkFilesFolder(realPath)) {
File directory = new File(realPath);
if (directory.isDirectory()) {
File[] entries = directory.listFiles();
for (int i = 0; i < entries.length; i++) {
if (entries[i].getAbsolutePath().indexOf(".") > 0) {
theVector.add(entries[i].getAbsolutePath().substring(entries[i].
getAbsolutePath().lastIndexOf(File.separator) + 1));
}
}
}
}
return theVector;
}
/**
* getSubDirsName(String realPath)
* 获得该目录下所有子目录
* @param realPath String
* @return Vector
*/
@SuppressWarnings("unchecked")
public static Vector getSubDirsName(String realPath) {
Vector theVector = new Vector();
if (checkFilesFolder(realPath)) {
File directory = new File(realPath);
if (directory.isDirectory()) {
File[] entries = directory.listFiles();
for (int i = 0; i < entries.length; i++) {
if (entries[i].getAbsolutePath().indexOf(".") < 0) {
theVector.add(entries[i].getAbsolutePath().substring(entries[i].
getAbsolutePath().lastIndexOf(File.separator) + 1));
}
}
}
}
return theVector;
}
/**
* smartMakeDir(ServletContext context,String filePath)
* 输入context、相对路径建立目录
* @param context ServletContext
* @param filePath String
* @return int 建立目录的数量 -1错误无法建立目录
*/
public static int smartMakeDir(ServletContext context, String filePath) {
int opFlag = 0;
//分解路径
String[] tempPath = filePath.substring(1).split("/");
String tempStr = "";
String realPath;
for (int i = 0; i < tempPath.length; i++) {
//检查该路径是否存在,不存在就建立
tempStr += "/" + tempPath[i];
realPath = getRealFilePath(context, tempStr);
if (!checkFilesFolder(realPath)) {
if (makeDir(realPath)) {
++opFlag;
}
else {
opFlag = -1;
break;
}
}
}
return opFlag;
}
/**
* String makeDirName(int buildID)
* 根据任意ID对23,19,17取模生成目录名,从而生成相应的目录结构;
* @param buildID int
* @return String
*/
public static String makeDirName(int buildID) {
int dirID = buildID;
StringBuffer dirStructure = new StringBuffer();
dirStructure.append("/").append(String.valueOf(dirID % 23));
dirStructure.append("/").append(String.valueOf(dirID % 19));
dirStructure.append("/").append(String.valueOf(dirID % 17));
return dirStructure.toString();
}
/**
* getDirectoryAllFiles(String realPath)
* @param realpath String
* @return AarrayList
* @todo get all files in the realPath
*/
@SuppressWarnings("unchecked")
public static ArrayList getDirectoryAllFiles(String realPath) {
ArrayList fileList = new ArrayList();
if (checkFilesFolder(realPath)) {
File directory = new File(realPath);
if (directory.isDirectory()) {
File[] entries = directory.listFiles();
for (int i = 0; i < entries.length; i++) {
if (entries[i].getAbsolutePath().indexOf(".") > 0) {
fileList.add(entries[i].getAbsolutePath());
}
}
}
}
return fileList;
}
/**
* getAllSubDirs(String realPath)
* @param realPath String
* @return ArrayList
* @todo get all directories in the realPath
*/
@SuppressWarnings("unchecked")
public static ArrayList getAllSubDirs(String realPath) {
ArrayList dirList = new ArrayList();
if (checkFilesFolder(realPath)) {
File directory = new File(realPath);
if (directory.isDirectory()) {
File[] entries = directory.listFiles();
for (int i = 0; i < entries.length; i++) {
if (entries[i].getAbsolutePath().indexOf(".") < 0) {
dirList.add(entries[i].getAbsolutePath());
}
}
}
}
return dirList;
}
/**
* createImgWaterMark(String filePath, String watermark)
* 在图片上打上水印
* @param filePath String
* @param watermark String
* @return ArrayList
*/
public static boolean createImgWaterMark(String filePath, String watermark) {
try {
File _file = new File(filePath); //读入文件
Image theImg = javax.imageio.ImageIO.read(_file); //构造Image对象
ImageIcon waterIcon = new ImageIcon("temp");
Image waterImg = waterIcon.getImage();
int width = theImg.getWidth(null);
int height = theImg.getHeight(null);
BufferedImage bimage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = bimage.createGraphics();
g.setColor(Color.red);
g.setBackground(Color.white);
g.drawImage(theImg, 0, 0, null);
g.drawImage(waterImg, 100, 100, null);
g.drawString(watermark, 10, 10); //添加文字
g.dispose();
FileOutputStream out = new FileOutputStream(filePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
param.setQuality(50f, true);
encoder.encode(bimage, param);
out.close();
}
catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -