You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
4.5 KiB

2 years ago
package com.zky.pub;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.log4j.Logger;
/**
* @author dy
*
*/
public class FileImport {
private Logger log = Logger.getLogger(FileImport.class);
private String[] column;
private String split = "\t";//默认以制表符TAB键间隔
public FileImport() {
column = new String[0];
}
public FileImport(String[] column) {
this.column = column;
}
public FileImport(String column) {
this.column = new String[] { column };
}
public FileImport(String[] column, String split) {
this.column = column;
this.split = split;
}
/**
* List Map keyform.xxx
* @param request
* @param p_otherParamList
* @return
* @throws Exception
*/
public List importSingleFileAsList(HttpServletRequest request,
Map p_otherParamMap) throws Exception {
List result = new ArrayList();
String path = request.getRealPath("/temp/");
DiskFileUpload fu = new DiskFileUpload();
//设置允许用户上传文件大小,单位:字节
fu.setSizeMax(10000000);
//设置最多只允许在内存中存储的数据,单位:字节
fu.setSizeThreshold(4096);
//设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
fu.setRepositoryPath(path);
//得到所有的文件
List fileItems = fu.parseRequest(request);
Iterator i = fileItems.iterator();
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
//忽略其他不是文件域的所有表单信息
if (!fi.isFormField()) {
//获得文件名,这个文件名包括路径
String fileName = fi.getName();
log.debug("# upload file:" + fileName);
if (fileName != null) {
InputStream stream = fi.getInputStream();
BufferedReader in = new BufferedReader(
new InputStreamReader(stream));
String line = "";
while ((line = in.readLine()) != null) {
line = line.trim();
if (line.length() == 0) { //忽略空行
continue;
}
result.add(line);
}
}
} else {
if (p_otherParamMap != null) {
p_otherParamMap.put("form." + fi.getFieldName(), fi
.getString());
}
}
}
if (result.size() == 0) {
result = null;
}
return result;
}
public HashFmlBuf importSingleFile(HttpServletRequest request)
throws Exception {
if (column == null || column.length == 0) {
return null;
}
HashFmlBuf buf = new HashFmlBuf();
String path = request.getRealPath("/temp/");
DiskFileUpload fu = new DiskFileUpload();
//设置允许用户上传文件大小,单位:字节
fu.setSizeMax(10000000);
//设置最多只允许在内存中存储的数据,单位:字节
fu.setSizeThreshold(4096);
//设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
fu.setRepositoryPath(path);
//得到所有的文件
List fileItems = fu.parseRequest(request);
Iterator i = fileItems.iterator();
//处理文件
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
//忽略其他不是文件域的所有表单信息
if (!fi.isFormField()) {
//获得文件名,这个文件名包括路径
String fileName = fi.getName();
log.debug("# upload file:" + fileName);
if (fileName != null) {
InputStream stream = fi.getInputStream();
BufferedReader in = new BufferedReader(
new InputStreamReader(stream));
String line = "";
String[] values = null;
int j = 0;
while ((line = in.readLine()) != null) {
line = line.trim();
if (line.length() == 0) { //忽略空行
continue;
}
if (column.length > 1) {
values = line.split(split);
for (int k = 0; k < values.length; k++) {
buf.fchg(column[k], j, values[k]);
}
} else {
buf.fchg(column[0], j, line);
}
j++;
}
buf.fchg("rowcount", 0, Integer.toString(j));
buf.setRowCount(j);
}
} else {
buf.fchg("form." + fi.getFieldName(), 0, fi.getString());
}
}
return buf;
}
/**
* @param column The column to set.
*/
public void setColumn(String[] column) {
this.column = column;
}
/**
* @param split The split to set.
*/
public void setSplit(String split) {
this.split = split;
}
}