/**
* 将文本转码
* @param args
* @throws Exception
*/
public static String
encodeString(String text){
try {
return URLEncoder. encode(text,"utf-8");
} catch (UnsupportedEncodingException
e) {
// TODO Auto-generated
catch block
e.printStackTrace();
}
return null;
}
/**
* 产生MP3文件
* @param is
* @param fileName
*/
public static String
createNewFile(InputStream is,String fileName) {
FileOutputStream fos = null;
//
将输入流is写入文件输出流 fos中
int ch
= 0;
try {
//
打开一个已存在文件的输出流
fos = new FileOutputStream(fileName);
while((ch=is.read())
!= -1){
fos.write(ch);
}
return fileName;
} catch (Exception
e) {
e.printStackTrace();
} finally{
try {
fos.close();
is.close();
} catch (IOException
e) {
// TODO Auto-generated
catch block
e.printStackTrace();
}
}
return null;
}
/**
*java普通类获取WEB-INF/的路径
*
*/
package common;
import java.io.File;
public class FileUtis
{
public String
getPath(){
String pathStr = this.getClass().getClassLoader().getResource( "").getPath();
//
对 Windows 下获取 物理路径 做 特殊处理
if( "\\".equals(File. separator))
{
pathStr = pathStr.substring(1).replaceAll( "/", "\\\\");
}
String path=pathStr.split("WEB-INF")[0]+ "upload"+File. separatorChar;
File file= new File(path);
if(!file.exists()){ //当文件路径不存在
file.mkdir();
}
return path;
}
//删除文件
public void removeFile(String
filePath){
File file= new File(filePath);
if(file.exists()){ //判断文件是否存在
file.delete();
} else{
System. out.println( "文件不存在#删除文件
" +filePath+"
失败" );
}
}
public static void main(String[]
args) {
FileUtis fu= new FileUtis();
System. out.println(fu.getPath());
fu.removeFile( "D:\\zhangliangFile\\work8.6\\car_timer\\WebRoot\\upload\\sss.txt" );
}
}
//文件大小转换
public static String
convertFileSize( long size)
{
long kb
= 1024;
long mb
= kb * 1024;
long gb
= mb * 1024;
if (size
>= gb) {
return String. format("%.1f
GB", ( float)
size / gb);
} else if (size
>= mb) {
float f
= ( float)
size / mb;
return String. format(f
> 100 ? "%.0f MB" : "%.1f
MB", f);
} else if (size
>= kb) {
float f
= ( float)
size / kb;
return String. format(f
> 100 ? "%.0f KB" : "%.1f
KB", f);
} else
return String. format("%d
B", size);
}
/*** 去除String[]中的重复元素@param temp*@return**/
public static List<String>
removeCopyAttr(String[] temp){
List<String> obj= new ArrayList<String>();
List<String> list = new LinkedList<String>();
for( int i
= 0; i < temp. length;
i++) {
if(!list.contains(temp[i]))
{
list.add(temp[i]);
}
}
String[] rowsTemp = list.toArray( new String[list.size()]);
if(rowsTemp!= null &&
rowsTemp. length >
0) {
for( int i=0;
i<rowsTemp. length;
i++) {
obj.add(rowsTemp[i]);
}
}
return obj;
}
/**
* 把String数组转换成Integer数组
* @param strArray
* @return
*/
public static Integer[]
convertStrArrayToInt(String[]strArray){
if(strArray!= null&&strArray. length>0){
Integer array[]= new Integer[strArray. length];
for( int i=0;i<strArray. length;i++){
array[i]=Integer. parseInt(strArray[i]);
}
return array;
} else{
return null;
}
}