package struts02.util;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import org.apache.struts.upload.FormFile;
public class BoardUtil {
public static String saveFile(FormFile uploadFile) {
String physicalFileName=null;
InputStream is;
try {
is = uploadFile.getInputStream();
String uploadFileName = uploadFile.getFileName();
physicalFileName = getPhysicalFileName( uploadFileName );
String filePath = "F:/Struts_And_iBatis/workspace/struts-board-02/WebContent/upload/";
OutputStream os = new FileOutputStream(filePath + physicalFileName);
int byteRead = 0;
byte[] buf = new byte[8192];
while ((byteRead = is.read(buf)) != -1) {
os.write(buf, 0, byteRead);
}
os.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return physicalFileName;
}//end saveFile()
private static String getPhysicalFileName(String uploadFileName) {
String systemTime=new Date().getTime()+"";//
if( uploadFileName.indexOf(".") == -1 ){ //파일이름에 .이 없으면
return uploadFileName+"_"+systemTime;
}else{
int index = uploadFileName.lastIndexOf(".");
//확장자를뺀파일명
String _tmpFileName = uploadFileName.substring(0, index);
// Extension
String _extName = uploadFileName.substring( index );
return _tmpFileName + "_"+systemTime+_extName;
}
}
}