package com.shs.framework.web.utls;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.StringTokenizer;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggerFactory;
/**
* <P>Description:文件生成器</P>
* @ClassName: FileUtils
* @author wangmingyu 2015-4-9 下午04:30:11
* @see TODO
*/
public class FileUtils {
private static Logger logger=Logger.getLogger(FileUtils.class);
private static FileUtils fileUtils;
public static FileUtils newFileUtils() {
if (null == fileUtils)
fileUtils = new FileUtils();
return fileUtils;
}
private byte[] getRequestData(HttpServletRequest request) throws IOException {
InputStream cin = request.getInputStream();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = cin.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
cin.close();
byte[] fileData = outStream.toByteArray();
outStream.close();
return fileData;
}
private String getRequestDataToString(HttpServletRequest request) throws IOException {
InputStream cin = request.getInputStream();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = cin.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
cin.close();
byte[] fileData = outStream.toByteArray();
outStream.close();
return new String(fileData, "UTF-8");
}
/**
* <p>Title: readTxtFile</p>
* <p>Description: 读取数据</p>
* @param filePath
* @return
* @author wangmingyu 2015-4-8 下午02:17:19
*/
public String readTxtFile(String filePath) {
String lineTxt = "";
StringBuilder str = new StringBuilder();
try {
String encoding = "UTF-8";
File file = new File(filePath);
if (file.isFile() && file.exists()) { //判断文件是否存在
InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
while ((lineTxt = bufferedReader.readLine()) != null) {
str.append(lineTxt);
}
read.close();
} else {
logger.info("找不到指定的文件");
}
} catch (Exception e) {
logger.info("读取文件内容出错");
e.printStackTrace();
}
return str.toString();
}
/**
* <p>Title: saveFileTitles</p>
* <p>Description: 生成文件</p>
* @param fileDes 文件地址
* @param filedata 文件内容
* @throws Exception
* @author wangmingyu 2015-4-9 下午04:29:23
*/
public void saveFileTitles( String fileDes, String filedata) throws Exception {
boolean isSave=false;
String filePath = fileDes.substring(0, fileDes.lastIndexOf("\\"));
File toFile = new File(filePath);
if(StringUtils.isBlank(filedata)){
return;
}
if (!toFile.exists() && !toFile.isDirectory()) {
// throw new Exception("file exist");
createFolders(filePath);
}
toFile = new File(fileDes);
String fileData = "";
if (toFile.exists()) {
fileData = readTxtFile(fileDes);
String fileDatas[]=fileData.split(",");
//循环判断是否已经存储过
for(String d:fileDatas){
if(filedata.equals(d)){
isSave=true;
}
}
if(!isSave){//未存储的 进行数据保存
if(!StringUtils.isBlank(fileData)) fileData = "," + filedata;else fileData = filedata;
}else{
fileData="";
}
} else {
fileData = filedata;
}
Writer writer = new OutputStreamWriter(new FileOutputStream(toFile, true));
// FileOutputStream outImgStream = new FileOutputStream(toFile);
writer.write(fileData);
writer.close();
}
/**
* <p>Title: clearFileData</p>
* <p>Description: 清除 文件信息</p>
* @param request
* @param fileDes
* @param filedata
* @throws Exception
* @author wangmingyu 2015-4-9 下午04:28:22
*/
public void clearFileData( String fileDes) throws Exception {
File toFile = new File(fileDes);
if (!toFile.exists() && !toFile.isDirectory()) {
// throw new Exception("file exist");
return ;
}
toFile.delete();
}
/**
* <p>Title: saveUrlFile</p>
* <p>Description:存储文件</p>
* @param request
* @param fileDes
* @throws Exception
* @author wangmingyu 2015-4-9 下午03:37:26
*/
public void saveUrlFile(HttpServletRequest request, String fileDes) throws Exception {
String filePath = fileDes.substring(0, fileDes.lastIndexOf("\\"));
File toFile = new File(filePath);
if (!toFile.exists() && !toFile.isDirectory()) {
createFolders(filePath);
}
File file = new File(fileDes);
FileOutputStream outImgStream = new FileOutputStream(file);
outImgStream.write(getRequestData(request));
outImgStream.close();
}
/**
* <p>Title: saveUrlFile</p>
* <p>Description:存储文件</p>
* @param data 存储内容
* @param fileDes
* @throws Exception
* @author wangmingyu 2015-4-9 下午03:37:26
*/
public boolean saveUrlFile(String data,String fileDes) {
try {
String filePath = fileDes.substring(0, fileDes.lastIndexOf("\\"));
File toFile = new File(filePath);
if (!toFile.exists() && !toFile.isDirectory()) {
// throw new Exception("file exist");
createFolders(filePath);
}
toFile = new File(fileDes);
String fileData = data;
if(toFile.exists()){
return true;
}
toFile.createNewFile();
Writer writer = new OutputStreamWriter(new FileOutputStream(toFile, true));
// FileOutputStream outImgStream = new FileOutputStream(toFile);
writer.write(fileData);
writer.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return false;
}
return true;
}
/**
* <p>Title: CreateFolders</p>
* <p>Description: 创建多级目录</p>
* @param fileDes
* @throws Exception
* @author wangmingyu 2015-4-9 下午03:31:28
*/
public static void createFolders(String fileDes) throws Exception {
StringTokenizer st = new StringTokenizer(fileDes, File.separator);
StringBuilder sb = new StringBuilder();
String osname = System.getProperty("os.name");
if (osname.compareToIgnoreCase("linux") == 0)
sb.append(File.separator);
while (st.hasMoreTokens()) {
sb.append(st.nextToken());
File file = new File(sb.toString());
if (!file.exists())
file.mkdir();
sb.append(File.separator);
}
}
}
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.StringTokenizer;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggerFactory;
/**
* <P>Description:文件生成器</P>
* @ClassName: FileUtils
* @author wangmingyu 2015-4-9 下午04:30:11
* @see TODO
*/
public class FileUtils {
private static Logger logger=Logger.getLogger(FileUtils.class);
private static FileUtils fileUtils;
public static FileUtils newFileUtils() {
if (null == fileUtils)
fileUtils = new FileUtils();
return fileUtils;
}
private byte[] getRequestData(HttpServletRequest request) throws IOException {
InputStream cin = request.getInputStream();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = cin.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
cin.close();
byte[] fileData = outStream.toByteArray();
outStream.close();
return fileData;
}
private String getRequestDataToString(HttpServletRequest request) throws IOException {
InputStream cin = request.getInputStream();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = cin.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
cin.close();
byte[] fileData = outStream.toByteArray();
outStream.close();
return new String(fileData, "UTF-8");
}
/**
* <p>Title: readTxtFile</p>
* <p>Description: 读取数据</p>
* @param filePath
* @return
* @author wangmingyu 2015-4-8 下午02:17:19
*/
public String readTxtFile(String filePath) {
String lineTxt = "";
StringBuilder str = new StringBuilder();
try {
String encoding = "UTF-8";
File file = new File(filePath);
if (file.isFile() && file.exists()) { //判断文件是否存在
InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
while ((lineTxt = bufferedReader.readLine()) != null) {
str.append(lineTxt);
}
read.close();
} else {
logger.info("找不到指定的文件");
}
} catch (Exception e) {
logger.info("读取文件内容出错");
e.printStackTrace();
}
return str.toString();
}
/**
* <p>Title: saveFileTitles</p>
* <p>Description: 生成文件</p>
* @param fileDes 文件地址
* @param filedata 文件内容
* @throws Exception
* @author wangmingyu 2015-4-9 下午04:29:23
*/
public void saveFileTitles( String fileDes, String filedata) throws Exception {
boolean isSave=false;
String filePath = fileDes.substring(0, fileDes.lastIndexOf("\\"));
File toFile = new File(filePath);
if(StringUtils.isBlank(filedata)){
return;
}
if (!toFile.exists() && !toFile.isDirectory()) {
// throw new Exception("file exist");
createFolders(filePath);
}
toFile = new File(fileDes);
String fileData = "";
if (toFile.exists()) {
fileData = readTxtFile(fileDes);
String fileDatas[]=fileData.split(",");
//循环判断是否已经存储过
for(String d:fileDatas){
if(filedata.equals(d)){
isSave=true;
}
}
if(!isSave){//未存储的 进行数据保存
if(!StringUtils.isBlank(fileData)) fileData = "," + filedata;else fileData = filedata;
}else{
fileData="";
}
} else {
fileData = filedata;
}
Writer writer = new OutputStreamWriter(new FileOutputStream(toFile, true));
// FileOutputStream outImgStream = new FileOutputStream(toFile);
writer.write(fileData);
writer.close();
}
/**
* <p>Title: clearFileData</p>
* <p>Description: 清除 文件信息</p>
* @param request
* @param fileDes
* @param filedata
* @throws Exception
* @author wangmingyu 2015-4-9 下午04:28:22
*/
public void clearFileData( String fileDes) throws Exception {
File toFile = new File(fileDes);
if (!toFile.exists() && !toFile.isDirectory()) {
// throw new Exception("file exist");
return ;
}
toFile.delete();
}
/**
* <p>Title: saveUrlFile</p>
* <p>Description:存储文件</p>
* @param request
* @param fileDes
* @throws Exception
* @author wangmingyu 2015-4-9 下午03:37:26
*/
public void saveUrlFile(HttpServletRequest request, String fileDes) throws Exception {
String filePath = fileDes.substring(0, fileDes.lastIndexOf("\\"));
File toFile = new File(filePath);
if (!toFile.exists() && !toFile.isDirectory()) {
createFolders(filePath);
}
File file = new File(fileDes);
FileOutputStream outImgStream = new FileOutputStream(file);
outImgStream.write(getRequestData(request));
outImgStream.close();
}
/**
* <p>Title: saveUrlFile</p>
* <p>Description:存储文件</p>
* @param data 存储内容
* @param fileDes
* @throws Exception
* @author wangmingyu 2015-4-9 下午03:37:26
*/
public boolean saveUrlFile(String data,String fileDes) {
try {
String filePath = fileDes.substring(0, fileDes.lastIndexOf("\\"));
File toFile = new File(filePath);
if (!toFile.exists() && !toFile.isDirectory()) {
// throw new Exception("file exist");
createFolders(filePath);
}
toFile = new File(fileDes);
String fileData = data;
if(toFile.exists()){
return true;
}
toFile.createNewFile();
Writer writer = new OutputStreamWriter(new FileOutputStream(toFile, true));
// FileOutputStream outImgStream = new FileOutputStream(toFile);
writer.write(fileData);
writer.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return false;
}
return true;
}
/**
* <p>Title: CreateFolders</p>
* <p>Description: 创建多级目录</p>
* @param fileDes
* @throws Exception
* @author wangmingyu 2015-4-9 下午03:31:28
*/
public static void createFolders(String fileDes) throws Exception {
StringTokenizer st = new StringTokenizer(fileDes, File.separator);
StringBuilder sb = new StringBuilder();
String osname = System.getProperty("os.name");
if (osname.compareToIgnoreCase("linux") == 0)
sb.append(File.separator);
while (st.hasMoreTokens()) {
sb.append(st.nextToken());
File file = new File(sb.toString());
if (!file.exists())
file.mkdir();
sb.append(File.separator);
}
}
}