importjava.awt.image.BufferedImage;importjava.io.File;importjava.io.FileOutputStream;importjava.io.InputStream;importjava.io.OutputStream;importjava.text.SimpleDateFormat;importjava.util.Date;importjava.util.HashMap;importjava.util.Map;importjava.util.UUID;importjavax.imageio.ImageIO;importorg.apache.struts.upload.FormFile;importorg.apache.struts.util.MessageResources;importorg.apache.commons.logging.Log;importorg.apache.commons.logging.LogFactory;public classUploadFile {/*** 按照ID和当前时间生成目录
*@paramOrderId
*@return
*/
public staticString getAbsolutePath(){
String currYear= new SimpleDateFormat("yyyy").format(newDate());
String currMonth= new SimpleDateFormat("MM").format(newDate());
String currDay= new SimpleDateFormat("dd").format(newDate());
String path= new StringBuffer(currYear).append('/').append(currMonth).append('/').append(currDay).append('/').toString();returnpath;
}/*** 上传课程资料
*@paramdir 文件目录
*@paramtrueName 文件名称
*@paramformFile FormFile
*@returnString
*@throwsException*/
public static String uploadFile(int courseId,String dir,FormFile file,MessageResources message) throwsException {
String fileName=file.getFileName();int i = fileName.lastIndexOf(".");
String logoFormat= fileName.substring(i + 1);int size =file.getFileSize();//*************限制文件的上传格式和文件大小*******************
String fileFormat = message.getMessage("file.fileFormat"); //文件格式
int imageSize = Integer.valueOf(message.getMessage("file.fileSize"));//文件大小
String format = UploadFile.checkFileExt(logoFormat, fileFormat); //判断文件格式
if (format != null && size <=imageSize) {
fileName= courseId+"_"+String.valueOf(System.currentTimeMillis()) + "." +logoFormat;
File dirPath= new File(dir + "/" + fileName);//存储位置
mkdirIfNotExists(dirPath);
InputStream streamIn= file.getInputStream(); //创建读取用户上传文件的对象
File uploadFile = new File(dir); //创建把上传数据写到目标文件的对象//判断指定路径是否存在,不存在则创建路径
if (!uploadFile.exists() || uploadFile == null) {
uploadFile.mkdirs();
}
OutputStream streamOut= newFileOutputStream(dirPath);int bytesRead = 0;byte[] buffer = new byte[8192];while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
streamOut.write(buffer,0, bytesRead);
}
streamOut.close();
streamIn.close();
file.destroy();returnfileName;
}else{return null;
}
}/*** 上传文章附件
*@paramdir
*@paramfile
*@parammessage
*@return*@throwsException*/
public static String uploadFile(String dir,FormFile file,MessageResources message) throwsException {
String fileName=file.getFileName();int i = fileName.lastIndexOf(".");
String logoFormat= fileName.substring(i + 1);int size =file.getFileSize();//*************限制文件的上传格式和文件大小*******************
String fileFormat = message.getMessage("file.fileFormat"); //文件格式
int imageSize = Integer.valueOf(message.getMessage("file.fileSize"));//文件大小
String format = UploadFile.checkFileExt(logoFormat, fileFormat); //判断文件格式
if (format != null && size <=imageSize) {
fileName= String.valueOf(System.currentTimeMillis()) + "." +logoFormat;
File dirPath= new File(dir + "/" + fileName);//存储位置
mkdirIfNotExists(dirPath);
InputStream streamIn= file.getInputStream(); //创建读取用户上传文件的对象
File uploadFile = new File(dir); //创建把上传数据写到目标文件的对象//判断指定路径是否存在,不存在则创建路径
if (!uploadFile.exists() || uploadFile == null) {
uploadFile.mkdirs();
}
OutputStream streamOut= newFileOutputStream(dirPath);int bytesRead = 0;byte[] buffer = new byte[8192];while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
streamOut.write(buffer,0, bytesRead);
}
streamOut.close();
streamIn.close();
file.destroy();returnfileName;
}else{return null;
}
}public static String uploadFile1(String dir,FormFile file,MessageResources message) throwsException {
String fileName=file.getFileName();
String s=UUID.randomUUID().toString();int i = fileName.lastIndexOf(".");
String logoFormat= fileName.substring(i + 1);int size =file.getFileSize();//*************限制文件的上传格式和文件大小*******************
String fileFormat = message.getMessage("file.fileFormat"); //文件格式
int imageSize = Integer.valueOf(message.getMessage("file.fileSize"));//文件大小
String format = UploadFile.checkFileExt(logoFormat, fileFormat); //判断文件格式
if (format != null && size <=imageSize) {
fileName= String.valueOf(System.currentTimeMillis()+s) + "." +logoFormat;
File dirPath= new File(dir + "/" + fileName);//存储位置
mkdirIfNotExists(dirPath);
InputStream streamIn= file.getInputStream(); //创建读取用户上传文件的对象
File uploadFile = new File(dir); //创建把上传数据写到目标文件的对象//判断指定路径是否存在,不存在则创建路径
if (!uploadFile.exists() || uploadFile == null) {
uploadFile.mkdirs();
}
OutputStream streamOut= newFileOutputStream(dirPath);int bytesRead = 0;byte[] buffer = new byte[8192];while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
streamOut.write(buffer,0, bytesRead);
}
streamOut.close();
streamIn.close();
file.destroy();returnfileName;
}else{return null;
}
}/***@param文件扩展名
*@parampermitFormatsConfig 文件类型
*@returnString 文件匹配类型*/
public staticString checkFileExt(String type, String permitFormatsConfig) {
String[] formats= permitFormatsConfig.split(";");for (int i = 0; i < formats.length; i++) {if(type.toLowerCase().endsWith(formats[i].toLowerCase())) {returnformats[i];
}
}return null;
}public staticFile mkdirIfNotExists(File file) {if(file.exists()) {returnfile;
}if (file.isDirectory() &&file.mkdirs()) {returnfile;
}
File parentDirectory=file.getParentFile();if(parentDirectory.exists()) {returnfile;
}else if(parentDirectory.mkdirs()) {returnfile;
}throw newjava.lang.RuntimeException("Failed to make some necessary parent directories. Please trying.");
}/*** 上传图片,并获得缩小图片
* 图片大小交由显示页面控制 后台将不负责修改图像大小
*@parampicDir
*@paramfile
*@parammessage
*@paramwidth
*@return*@throwsException*/
public staticString uploadFile(String picDir, FormFile file,
MessageResources message,int width,int height) throwsException {
String fileName=file.getFileName();int ff = fileName.lastIndexOf(".");
ImageScale imageScale= newImageScale();
String logoFormat= fileName.substring(ff + 1);int size =file.getFileSize();//*************限制文件的上传格式和文件大小*******************
String fileFormat = message.getMessage("file.imageFormat"); //文件格式
int imageSize =Integer.valueOf(message
.getMessage("file.imageSize"));//文件大小
String format = UploadFile.checkFileExt(logoFormat, fileFormat); //判断文件格式
if (format != null && size <=imageSize) {
fileName= String.valueOf(System.currentTimeMillis()) + "." +logoFormat;
File dirPath= new File(picDir + "/" +fileName);
mkdirIfNotExists(dirPath);//存储位置
InputStream stream =file.getInputStream();
OutputStream bos= newFileOutputStream(dirPath);
BufferedImage Bi=ImageIO.read(file.getInputStream());
bos= newFileOutputStream(dirPath);//图片大小交由显示页面控制 后台将不负责修改图像大小
/*if (Bi.getWidth() > width || Bi.getHeight() > height) {
Bi = imageScale.imageZoomOut(Bi, width, height);
ImageIO.write(Bi, logoFormat, dirPath);
} else {*/ImageIO.write(Bi, logoFormat, dirPath);//}
bos.close();
stream.close();
file.destroy();returnfileName;
}else{
file.destroy();return null;
}
}/*** 上传图片,并获得缩小图片
*@parampicDir
*@paramfile
*@parammessage
*@paramwidth
*@return*@throwsException*/
public static String uploadFile(String picDir, FormFile file,MessageResources message, int width) throwsException {
String fileName=file.getFileName();int ff = fileName.lastIndexOf(".");
ImageScale imageScale= newImageScale();
String logoFormat= fileName.substring(ff + 1);int size =file.getFileSize();//*************限制文件的上传格式和文件大小*******************
String fileFormat = message.getMessage("file.imageFormat"); //文件格式
int imageSize = Integer.valueOf(message.getMessage("file.imageSize"));//文件大小
String format = UploadFile.checkFileExt(logoFormat, fileFormat); //判断文件格式
if (format != null && size <=imageSize) {
fileName= String.valueOf(System.currentTimeMillis()) + "."+logoFormat;
File dirPath= new File(picDir + "/" + fileName);//存储位置
mkdirIfNotExists(dirPath);
InputStream stream=file.getInputStream();
OutputStream bos= newFileOutputStream(dirPath);
BufferedImage Bi=ImageIO.read(file.getInputStream());if (Bi.getWidth() > width || Bi.getHeight() >width) {
Bi=imageScale.imageZoomOut(Bi, width, width);
ImageIO.write(Bi, logoFormat, dirPath);
}else{
ImageIO.write(Bi, logoFormat, dirPath);
}
bos.close();
stream.close();
file.destroy();
dirPath= new File(picDir + "/hoy_" +fileName);
bos= newFileOutputStream(dirPath);if (Bi.getWidth() > 142 || Bi.getHeight() > 60) {
Bi= imageScale.imageZoomOut(Bi, 142, 60);
ImageIO.write(Bi, logoFormat, dirPath);
}else{
ImageIO.write(Bi, logoFormat, dirPath);
}
bos.close();
stream.close();
file.destroy();returnfileName;
}else{
file.destroy();return null;
}
}
}