package com.thinkgem.jeesite.modules.meeting.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.utils.Exceptions;
public class FileUtil {
private static final Logger logger = LoggerFactory.getLogger(FileUtil.class);
public static final String UPLOAD_PATH = Global.getConfig("uploadPath");
public static Boolean isUploadFile(MultipartFile[] files) {
boolean isUpload = false;
for (MultipartFile file : files) {
if (StringUtils.isNotBlank(file.getOriginalFilename())) {
isUpload = true;
break;
}
}
return isUpload;
}
public static Boolean saveFile(InputStream is,String savePath)throws IOException{
OutputStream os = null;
try{
String rootPath = UPLOAD_PATH+File.separator+savePath.substring(0, savePath.lastIndexOf(File.separator)-1);
String fileName = savePath.substring(savePath.lastIndexOf(File.separator));
File saveDir = new File(rootPath);
if(!saveDir.exists()){
saveDir.mkdirs();
}
os = new FileOutputStream(rootPath+File.separator+fileName);
byte[] b = new byte[100*1024];
int bytesRead = 0;
while ((bytesRead = is.read(b)) != -1) {
os.write(b, 0, bytesRead);
}
return true;
}finally{
try{
os.close();
is.close();
}catch(Exception ex){
Exceptions.unchecked(ex);
}
}
}
public static String upload(MultipartFile multipartFile, String uploadPath) {
String originalFilename = multipartFile.getOriginalFilename();
logger.debug("开始上传文件:" + originalFilename);
String fileName = getSaveFileName(originalFilename);
File dir = new File(uploadPath);
File file = new File(uploadPath, fileName);
try {
if (!dir.exists()) {
dir.mkdirs();
}
if (!file.exists()) {
file.createNewFile();
}
multipartFile.transferTo(file);
logger.debug(uploadPath + File.separator + originalFilename + "\t上传成功,保存为:" + fileName);
return transformRelativePath(UPLOAD_PATH, file.getPath());
} catch (IOException e) {
logger.debug(originalFilename + "上传失败!");
e.printStackTrace();
}
return null;
}
public static void upload(MultipartFile[] multipartFiles, String uploadPath) {
for(int i = 0; i < multipartFiles.length; i ++) {
upload(multipartFiles[0], uploadPath);
}
}
public static String getFileUploadPath(String bsId, String type){
String uploadPath = UPLOAD_PATH;
DateFormat format=new SimpleDateFormat("yyyyMMdd");
String date = format.format(new Date());
if (StringUtils.isNotBlank(bsId)) {
uploadPath += File.separator + type +File.separator + bsId + File.separator + date;
} else {
uploadPath += File.separator + type +File.separator + date;
}
logger.debug("上传路径:" + uploadPath);
return uploadPath;
}
public static String transformAbsolutePath(String relativePath){
if (StringUtils.isNotBlank(relativePath)) {
String absolutePath = UPLOAD_PATH+relativePath;
return absolutePath;
}
return "";
}
public static String transformRelativePath(String propertiesPath, String savePath){
if (StringUtils.isNotBlank(propertiesPath) && StringUtils.isNotBlank(savePath)) {
propertiesPath = new File(propertiesPath).getPath().replace("//", File.separator);
String relativePath = savePath.replace(propertiesPath, "");
return relativePath;
}
return "";
}
private static String getSaveFileName(String fileName) {
StringBuffer savaFileName = new StringBuffer();
savaFileName.append(System.currentTimeMillis());
savaFileName.append(fileName.substring(fileName.lastIndexOf(".")));
return savaFileName.toString();
}
public static void downLoad(HttpServletResponse response,String fileName,
String filePath, FileType fileType) {
filePath = transformAbsolutePath(filePath);
String contentType;
switch (fileType) {
case jpg :
case png :
case jpeg :
contentType = "image/*";
break;
case doc :
case docx:
contentType = "application/msword";
default :
contentType = "multipart/form-data";
break;
}
InputStream inputStream = null;
OutputStream os = null;
try {
response.setCharacterEncoding("utf-8");
if (!StringUtils.isNotBlank(contentType)) {
response.setContentType(contentType);
}
response.setHeader("Content-Disposition", "attachment;fileName="
+ new String(fileName.getBytes("gbk"),"iso-8859-1"));
File file = new File(filePath);
if (file.exists()) {
File targetFile = new File(filePath);
inputStream = new FileInputStream(targetFile);
os = response.getOutputStream();
byte[] b = new byte[2048];
int length;
while ((length = inputStream.read(b)) > 0) {
os.write(b, 0, length);
}
}
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static boolean deleteFile(String filePath){
filePath = transformAbsolutePath(filePath);
File file = new File(filePath);
if (file.exists() && file.isFile()) {
if (file.delete()) {
logger.debug("删除文件" + filePath + "成功!");
return true;
} else {
logger.debug("删除文件" + filePath + "失败!");
return false;
}
} else {
logger.debug("文件不存在!");
return false;
}
}
public static byte[] readByte(String filePath) {
filePath = transformAbsolutePath(filePath);
return readByteFilePath(filePath);
}
public static byte[] readByteFilePath(String filePath) {
FileInputStream fis;
try {
fis = new FileInputStream(filePath);
try {
byte r[] = new byte[fis.available()];
fis.read(r);
return r;
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
logger.debug("文件不存在!");
e.printStackTrace();
}
return null;
}
public static String getSystemAccessPath(HttpServletRequest request) {
return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
}
}
package com.thinkgem.jeesite.modules.meeting.utils;
public enum FileType {
doc,
docx,
xls,
xlsx,
pdf,
jpg,
png,
jpeg,
other;
@Override
public String toString() {
return this.name();
}
public static FileType create(String value) {
try {
return FileType.valueOf(value);
} catch (Exception e) {
return FileType.other;
}
}
}