package little.upload;
/**
* Created by IntelliJ IDEA.
* User: Sunjianwei
* Date: 11-11-20
* Time: 上午11:56
* To change this template use File | Settings | File Templates.
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
//FileUpload主要上传和删除图片的功能
public class FileUpload {
public static final Logger logger = Logger.getLogger(FileUpload.class);
private static int status = 0;
private static String uploadPath = null;// 上传文件的目录
private static String tempPath = null; // 临时文件目录
private static File uploadFile = null;
private static File tempPathFile = null;
private static int sizeThreshold = 4096; //4K
private static int sizeMax = 4194304;
//初始化
static {
sizeMax = Integer.parseInt(ReadUploadFileType.getValue("sizeMax"));
sizeThreshold = Integer.parseInt(ReadUploadFileType.getValue("sizeThreshold"));
uploadPath = ReadUploadFileType.getValue("uploadPath");
uploadFile = new File(uploadPath);
if (!uploadFile.exists()) {
uploadFile.mkdirs();
}
tempPath = ReadUploadFileType.getValue("tempPath");
tempPathFile = new File(tempPath);
if (!tempPathFile.exists()) {
tempPathFile.mkdirs();
}
}
/**
* 上传文件
*
* @param request
* @return true 上传成功 false上传失败
*/
@SuppressWarnings("unchecked")
public static boolean upload(HttpServletRequest request) {
boolean flag = true;
//检查输入请求是否为multipart表单数据。
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
//若果是的话
if (isMultipart) {
/**为该请求创建一个DiskFileItemFactory对象,通过它来解析请求。执行解析后,所有的表单项目都保存在一个List中。**/
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(sizeThreshold); // 设置缓冲区大小,这里是4kb
factory.setRepository(tempPathFile);// 设置缓冲区目录
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");//解决文件乱码问题
upload.setSizeMax(sizeMax);// 设置最大文件尺寸 4MbB
List<FileItem> items = upload.parseRequest(request);
// 检查是否符合上传的类型
status = checkFileType(items);
if(status == 0){
flag = false;
return flag;
}
Iterator<FileItem> itr = items.iterator();//所有的表单项
//保存文件
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();//循环获得每个表单项
if (!item.isFormField()) {//如果是文件类型
if (item.getName() != null) {//获得文件名 包括路径啊
File fullFile = new File(item.getName());
File savedFile = new File(uploadPath, fullFile.getName());
item.write(savedFile);
System.out.println("保存文件完成");
if(status == 2){
//解析excle
System.out.println("解析excel");
InputStream inputStream = new FileInputStream(uploadPath + fullFile.getName());
Workbook wb = new HSSFWorkbook(new POIFSFileSystem(inputStream));
Sheet sheet = null;
int sheetNumber = wb.getNumberOfSheets();
for (int i = 0; i < sheetNumber; i++) {
sheet = wb.getSheetAt(i);
for (Row row : sheet) {
for (Cell cell : row) {
String name = cell.getStringCellValue().trim();
System.out.println("name="+name);
}
}
}
inputStream.close();
}
}
}
}
} catch (FileUploadException e) {
flag = false;
e.printStackTrace();
logger.warn("FileUploadException");
} catch (Exception e) {
System.out.println("Exception");
flag = false;
e.printStackTrace();
logger.warn("Exception");
} finally {
return flag;
}
} else {
logger.warn("the enctype must be multipart/form-data");
flag = false;
}
return false;
}
/**
* 删除一组文件
*
* @param filePath 文件路径数组
*/
public static void deleteFile(String[] filePath) {
if (filePath != null && filePath.length > 0) {
for (String path : filePath) {
String realPath = uploadPath + path;
File delfile = new File(realPath);
if (delfile.exists()) {
delfile.delete();
}
}
}
}
/**
* 删除单个文件
*
* @param filePath 单个文件路径
*/
public static void deleteFile(String filePath) {
if (filePath != null && !"".equals(filePath)) {
String[] path = new String[]{filePath};
deleteFile(path);
}
}
/**
* 判断上传文件类型
*
* @param items
* @return
*/
private static int checkFileType(List<FileItem> items) {
Iterator<FileItem> itr = items.iterator();//所有的表单项
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();//循环获得每个表单项
if (!item.isFormField()) {//如果是文件类型
String name = item.getName();//获得文件名 包括路径啊
if (name != null) {
File fullFile = new File(name);
int isType = ReadUploadFileType.readUploadFileType(fullFile);
return isType;
}
}
}
return 0;
}
}