use of org.apache.commons.fileupload.FileItem in project summer-bean by cn-cerc.
the class ImportExcel method readFileData.
public DataSet readFileData(Record record) throws Exception {
FileItem file = this.getFile(record);
// 获取Excel文件对象
Workbook rwb = Workbook.getWorkbook(file.getInputStream());
// 获取文件的指定工作表 默认的第一个
Sheet sheet = rwb.getSheet(0);
Template template = this.getTemplate();
if (template.getColumns().size() != sheet.getColumns())
throw new RuntimeException(String.format("导入的文件:<b>%s</b>, 其总列数为 %d,而模版总列数为 %d 二者不一致,无法导入!", file.getName(), sheet.getColumns(), template.getColumns().size()));
DataSet ds = new DataSet();
for (int row = 0; row < sheet.getRows(); row++) {
if (row == 0) {
for (int col = 0; col < sheet.getColumns(); col++) {
Cell cell = sheet.getCell(col, row);
String value = cell.getContents();
String title = template.getColumns().get(col).getName();
if (!title.equals(value))
throw new RuntimeException(String.format("导入的文件:<b>%s</b>,其标题第 %d 列为【 %s】, 模版中为【%s】,二者不一致,无法导入!", file.getName(), col + 1, value, title));
}
} else {
ds.append();
for (int col = 0; col < sheet.getColumns(); col++) {
Cell cell = sheet.getCell(col, row);
String value = cell.getContents();
if (cell.getType() == CellType.NUMBER) {
NumberCell numberCell = (NumberCell) cell;
double d = numberCell.getValue();
value = formatFloat("0.######", d);
}
Column column = template.getColumns().get(col);
if (!column.validate(row, col, value)) {
ColumnValidateException err = new ColumnValidateException("其数据不符合模版要求,当前值为:" + value);
err.setTitle(column.getName());
err.setValue(value);
err.setCol(col);
err.setRow(row);
if (errorHandle == null || !errorHandle.process(err))
throw err;
}
ds.setField(column.getCode(), value);
}
if (readHandle != null && !readHandle.process(ds.getCurrent()))
break;
}
}
return ds;
}
use of org.apache.commons.fileupload.FileItem in project felix by apache.
the class ServletRequestWrapper method handleMultipart.
private void handleMultipart() throws IOException {
// Create a new file upload handler
final ServletFileUpload upload = new ServletFileUpload();
upload.setSizeMax(this.multipartConfig.multipartMaxRequestSize);
upload.setFileSizeMax(this.multipartConfig.multipartMaxFileSize);
upload.setFileItemFactory(new DiskFileItemFactory(this.multipartConfig.multipartThreshold, new File(this.multipartConfig.multipartLocation)));
// Parse the request
List<FileItem> items = null;
try {
items = upload.parseRequest(new ServletRequestContext(this));
} catch (final FileUploadException fue) {
throw new IOException("Error parsing multipart request", fue);
}
parts = new ArrayList<>();
for (final FileItem item : items) {
parts.add(new Part() {
@Override
public InputStream getInputStream() throws IOException {
return item.getInputStream();
}
@Override
public String getContentType() {
return item.getContentType();
}
@Override
public String getName() {
return item.getFieldName();
}
@Override
public String getSubmittedFileName() {
return item.getName();
}
@Override
public long getSize() {
return item.getSize();
}
@Override
public void write(String fileName) throws IOException {
try {
item.write(new File(fileName));
} catch (IOException e) {
throw e;
} catch (Exception e) {
throw new IOException(e);
}
}
@Override
public void delete() throws IOException {
item.delete();
}
@Override
public String getHeader(String name) {
return item.getHeaders().getHeader(name);
}
@Override
public Collection<String> getHeaders(String name) {
final List<String> values = new ArrayList<>();
final Iterator<String> iter = item.getHeaders().getHeaders(name);
while (iter.hasNext()) {
values.add(iter.next());
}
return values;
}
@Override
public Collection<String> getHeaderNames() {
final List<String> names = new ArrayList<>();
final Iterator<String> iter = item.getHeaders().getHeaderNames();
while (iter.hasNext()) {
names.add(iter.next());
}
return names;
}
});
}
}
use of org.apache.commons.fileupload.FileItem in project felix by apache.
the class ScriptConsolePlugin method getContentFromFilePart.
private String getContentFromFilePart(HttpServletRequest req, String paramName) throws IOException {
String value = WebConsoleUtil.getParameter(req, paramName);
if (value != null) {
return value;
}
final Map params = (Map) req.getAttribute(AbstractWebConsolePlugin.ATTR_FILEUPLOAD);
if (params == null) {
return null;
}
FileItem[] codeFile = getFileItems(params, paramName);
if (codeFile.length == 0) {
return null;
}
InputStream is = null;
try {
is = codeFile[0].getInputStream();
StringWriter sw = new StringWriter();
IOUtils.copy(is, sw, "utf-8");
return sw.toString();
} finally {
IOUtils.closeQuietly(is);
}
}
use of org.apache.commons.fileupload.FileItem in project felix by apache.
the class WebConsolePlugin method installSubsystem.
private void installSubsystem(HttpServletRequest req) throws IOException {
@SuppressWarnings("rawtypes") Map params = (Map) req.getAttribute(AbstractWebConsolePlugin.ATTR_FILEUPLOAD);
final boolean start = getParameter(params, "subsystemstart") != null;
FileItem[] subsystemItems = getFileItems(params, "subsystemfile");
for (final FileItem subsystemItem : subsystemItems) {
File tmpFile = null;
try {
// copy the data to a file for better processing
tmpFile = File.createTempFile("installSubsystem", ".tmp");
subsystemItem.write(tmpFile);
} catch (Exception e) {
log(LogService.LOG_ERROR, "Problem accessing uploaded subsystem file: " + subsystemItem.getName(), e);
// remove the temporary file
if (tmpFile != null) {
tmpFile.delete();
tmpFile = null;
}
}
if (tmpFile != null) {
final File file = tmpFile;
// TODO support install in other subsystems than the root one
// TODO currently this means that when installing more than one subsystem they
// will be installed concurrently. Not sure if this is the best idea.
// However the client UI does not support selecting more than one file, so
// from a practical point of view this is currently not an issue.
asyncSubsystemOperation(0, new SubsystemOperation() {
@Override
public void exec(Subsystem ss) {
try {
InputStream is = new FileInputStream(file);
try {
Subsystem nss = ss.install("inputstream:" + subsystemItem.getName(), is);
if (start)
nss.start();
} finally {
is.close();
file.delete();
}
} catch (IOException e) {
log(LogService.LOG_ERROR, "Problem installing subsystem", e);
}
}
});
}
}
}
use of org.apache.commons.fileupload.FileItem in project felix by apache.
the class BundlesServlet method getFileItems.
private FileItem[] getFileItems(Map params, String name) {
final List files = new ArrayList();
FileItem[] items = (FileItem[]) params.get(name);
if (items != null) {
for (int i = 0; i < items.length; i++) {
if (!items[i].isFormField() && items[i].getSize() > 0) {
files.add(items[i]);
}
}
}
return (FileItem[]) files.toArray(new FileItem[files.size()]);
}
Aggregations