资源简介

package com.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import com.alibaba.fastjson.JSONObject;

public class FileUtil {
private static long identity = System.currentTimeMillis();
private final static String METAEXT = ".meta";
public final static String CHARACTER = "UTF-8";

/**
* 保存文件
* @param dir
* @param file
* @param fileMeta
* @return
*/
public static JSONObject save(File dir, File file, JSONObject fileMeta, String prefix)throws IOException{
JSONObject data = new JSONObject();
if(fileMeta != null){
data.put("name", fileMeta.getString("name"));
}
if(data.getString("name") == null || data.getString("name").trim().equals("")){
data.put("name", file.getName());
}
return save(dir, new FileInputStream(file), data, prefix);

}
//生成的新的文件ID
private static synchronized String createNewId(String prefix){
String no = Long.toString(identity , 16);
return (prefix==null?"":prefix) no;
}
/**
* 删除文件及meta文件
* @param fileId
*/
public static boolean remove(File dir, String fileId){
File metafile = new File(dir, fileId METAEXT);
File target = new File(dir, fileId);
boolean flag = false;
if(target.exists()){
flag = target.delete();
}
if(metafile.exists()){
metafile.delete();
}
return flag;
}
/**
* 获取文件的元数据
* @return
*/
public static JSONObject getFileMeta(File dir, String fileId){
File metafile = new File(dir, fileId METAEXT);
System.out.println(metafile);
JSONObject data = null;
if(metafile.exists()){
try {
String str = FileUtils.readFileToString(metafile, CHARACTER);
data = JSONObject.parseObject(str);
} catch (Exception e) {
data = new JSONObject();
data.put("name", fileId);
}
}else{
data = new JSONObject();
data.put("name", fileId);
}
File target = new File(dir, fileId);
if(!target.exists())return null;
data.put("length", target.length());
data.put("lastModified", target.lastModified());
data.put("fileId", fileId);
return data;
}
public static File getFile(File dir, String fileId) {
File file = new File(dir, fileId);
if(file.exists())return file;
return null;
}
/**
* 获取文件
* @param dir
* @param fileId
* @param param 对图片的特殊处理,P100x100,表示将图片等比缩放到100x100取中间部分
* @return
*/
public static File getFile(File dir, String fileId, String param) {
String tofileid = param==null||param.equals("")?fileId:(fileId param) ".jpg";
File file = new File(dir, tofileid);
param = param == null?null:param.toLowerCase();
if(file.exists()){
return file;
}else{
File pfile = new File(dir, fileId);
if(!pfile.exists()){
file = new File(dir, "image404");
if(file.exists()){
return file;
}
}else if(param != null && !param.equals("") && param.indexOf("x") > 0){
param = param.toLowerCase();
int width = Integer.parseInt(param.substring(1, param.indexOf("x")));
int height = Integer.parseInt(param.substring(param.indexOf("x") 1));
try {
//System.out.println(file.getAbsolutePath());
ImageHepler.saveImageAsJpg(pfile.getAbsolutePath(), file.getAbsolutePath(), width, height, true);
return file;
} catch (Exception e) {
file = new File(dir, "image404");
if(file.exists()){
return file;
}
}
}else{
return pfile;
}
}
return null;
}
public static InputStream readFile(File dir, String fileId) {
File file = new File(dir, fileId);
if(file.exists())
try{
return new FileInputStream(file);
}catch(Exception e){
return null;
}
return null;
}
public static JSONObject save(File dir, InputStream inputStream,
JSONObject fileMeta, String prefix) throws IOException{
if(fileMeta == null){
throw new IOException("Save file must give meta!");
}
//新的文件编号
String nid = createNewId(prefix);

//删除原文件
if(fileMeta != null && fileMeta.containsKey("fileId")){
remove(dir, fileMeta.getString("fileId"));
nid = fileMeta.getString("fileId");
}
FileUtils.write(new File(dir, nid METAEXT), fileMeta.toJSONString(), CHARACTER);
FileOutputStream fos = new FileOutputStream(new File(dir, nid));
IOUtils.copy(inputStream, fos);
fos.flush();
fos.close();
inputStream.close();
/**删除原文件
if(fileMeta != null && fileMeta.containsKey("fileId")){
remove(dir, fileMeta.getString("fileId"));
}*/
return getFileMeta(dir, nid);
}
}

资源截图

代码片段和文件信息

package com.file;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class FileConfig {
private static String filefolder;
static {
InputStream inputstream = FileConfig.class.getResourceAsStream(
“/config.properties“);
if(inputstream != null){
try {
Properties properties = new Properties();
properties.load(inputstream);
filefolder = properties.getProperty(“filefolder“);
} catch (Exception e) {
System.err.println(“Read config.properties error!“);
}
}
}

public static File getFolder(){
filefolder = (filefolder==null||filefolder.equals(““)?“outfile“:filefolder);
File f = new File(filefolder);
if(!f.exists()){
f.mkdirs();
}
return f;
}



/**

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       4260  2016-04-06 09:27  fileprj\.classpath

     文件        379  2016-04-06 09:16  fileprj\.project

     文件       2116  2016-04-06 12:46  fileprj\bin\com\file\FileConfig.class

     文件       8435  2016-04-06 12:46  fileprj\bin\com\file\FileFilter.class

     文件       5540  2016-04-06 12:46  fileprj\bin\com\file\FileUtil.class

     文件       4439  2016-04-06 12:46  fileprj\bin\com\file\ImageHepler.class

     文件      17007  2016-04-06 12:46  fileprj\bin\com\file\ImageUtils.class

     文件       7249  2016-04-06 12:46  fileprj\bin\com\file\run\Application.class

     文件         18  2016-03-23 17:43  fileprj\bin\config.properties

     文件       1127  2016-03-23 17:43  fileprj\bin\log4j.properties

     文件        582  2016-04-06 12:46  fileprj\bin\test\RunFileApp.class

     文件        875  2016-03-23 17:43  fileprj\build\jetty\etc\jetty.crt

     文件       2225  2016-03-23 17:43  fileprj\build\jetty\etc\keystore

     文件         58  2016-03-23 17:43  fileprj\build\jetty\etc\webapps.xml

     文件      23271  2016-03-23 17:43  fileprj\build\jetty\etc\webdefault.xml

     文件     188671  2016-03-23 17:42  fileprj\build\jetty\lib\common\commons-beanutils-1.7.0.jar

     文件      46725  2016-03-23 17:42  fileprj\build\jetty\lib\common\commons-codec.jar

     文件      69002  2016-03-23 17:42  fileprj\build\jetty\lib\common\commons-fileupload-1.3.1.jar

     文件     173587  2016-03-23 17:42  fileprj\build\jetty\lib\common\commons-io-2.2.jar

     文件     315805  2016-03-23 17:42  fileprj\build\jetty\lib\common\commons-lang3-3.1.jar

     文件     280983  2016-03-23 17:42  fileprj\build\jetty\lib\common\commons-net-3.3.jar

     文件     108036  2016-03-23 17:42  fileprj\build\jetty\lib\common\commons-pool2-2.2.jar

     文件     313898  2016-03-23 17:42  fileprj\build\jetty\lib\common\dom4j-1.6.1.jar

     文件     322178  2016-03-23 17:42  fileprj\build\jetty\lib\common\fastjson-1.1.17.jar

     文件     136113  2016-03-23 17:42  fileprj\build\jetty\lib\common\gif4j.jar

     文件     639592  2016-03-23 17:42  fileprj\build\jetty\lib\common\google-collections-1.0.jar

     文件     153253  2016-03-23 17:42  fileprj\build\jetty\lib\common\jdom-1.0.jar

     文件      79251  2016-03-23 17:43  fileprj\build\jetty\lib\jetty-annotations-9.1.4.v20140401.jar

     文件     209914  2016-03-23 17:42  fileprj\build\jetty\lib\jetty-client-9.1.4.v20140401.jar

     文件      15988  2016-03-23 17:42  fileprj\build\jetty\lib\jetty-continuation-9.1.4.v20140401.jar

............此处省略68个文件信息

评论

共有 条评论