N长时间没有来了.......
忙中偷闲,整理点代码。
/**
* 复制单个文件
* @param oldPath String 原文件路径 如:c:/fqf.txt
* @param newPath String 复制后路径 如:f:/fqf.txt
* @return boolean
*/
public void copyFile(String oldPath, String newPath)
{
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPath); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
int length;
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
}
catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
}
//保存文件的方法
public String Savefiles(FormFile file,String path)
{
String fileName= file.getFileName();
String contentType = file.getContentType();
String size = (file.getFileSize() + " bytes");
try
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream stream = file.getInputStream();
OutputStream bos = new FileOutputStream(path+fileName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.close();
stream.close();
}
catch (IOException e) {
return e.getMessage();
}
return "1";
}
//生成15位的字符串
public String GenTradeId()
{
String tradeId = "";
RandomStrg.setCharset("a-zA-Z0-9");
RandomStrg.setLength("15");
try
{
RandomStrg.generateRandomObject();
tradeId=RandomStrg.getRandom();
}
catch (Exception e)
{
}
return tradeId;
}
/**
* 删除字符串中的空格,至多只保留一个空格
*
* @String txt:输入需要处理的字符串
* @return String :返回处理后的字符串
*
*/
public String deleteWhitespace(String txt){
if((txt!=null)&&(txt.trim().length()>1)){
}
else{
return "";
}
txt = txt.replaceAll("\n"," ").replaceAll("\t"," ");
String temp="";
try{
int flag =0,num=0;
char c = 'x';
StringBuffer sb = new StringBuffer("");
for(int x=0;x<txt.length();x++){
c = txt.charAt(x);
if((c==' ')&&(flag==0)){
sb.append(c);
flag =1;
}
else if((c!=' ')){
sb.append(c);
flag =0;
}
}
temp = sb.toString().trim();
}
catch(Exception e){
;
}
return temp;
}