0,0 → 1,79 |
package ak.photoalbum.util; |
|
import java.util.Map; |
import java.util.HashMap; |
import java.io.File; |
import java.io.InputStream; |
import java.io.OutputStream; |
import java.io.IOException; |
|
public class FileUtils |
{ |
protected static final int COPY_BUFFER_SIZE = 4096; |
protected static final Map MIME_MAP = new HashMap(); |
|
static { |
MIME_MAP.put("jpg", "image/jpeg"); |
MIME_MAP.put("jpeg", "image/jpeg"); |
MIME_MAP.put("png", "image/png"); |
} |
|
public static String getMime(String ext) |
{ |
if(ext == null) |
return null; |
else |
return (String)MIME_MAP.get(ext.toLowerCase()); |
} |
|
public static void copyStreams(InputStream in, OutputStream out) |
throws IOException |
{ |
byte[] buf = new byte[COPY_BUFFER_SIZE]; |
int len; |
|
while((len = in.read(buf)) > 0) { |
out.write(buf, 0, len); |
} |
} |
|
public static String extractFileName(String fullName) |
{ |
if(fullName == null) return null; |
|
int pointPos = fullName.lastIndexOf('.'); |
|
if(pointPos <= 0) |
return fullName; |
else |
return fullName.substring(0, pointPos); |
} |
|
public static String extractFileExt(String fullName) |
{ |
if(fullName == null) return null; |
|
int pointPos = fullName.lastIndexOf('.'); |
|
if(pointPos <= 0) |
return ""; |
else |
return fullName.substring(pointPos + 1); |
} |
|
public static String replaceFileSeparator(String path, String replaceWith) |
{ |
String sep = File.separator; |
StringBuffer buf = new StringBuffer(); |
int pos = -1; |
int oldPos = -1; |
|
while((pos = path.indexOf(sep, pos + 1)) >= 0) { |
buf.append(path.substring(oldPos + 1, pos)).append(replaceWith); |
oldPos = pos; |
} |
buf.append(path.substring(oldPos + 1)); |
|
return buf.toString(); |
} |
} |