Search in sources :

Example 11 with FileItem

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;
}
Also used : NumberCell(jxl.NumberCell) DataSet(cn.cerc.jdb.core.DataSet) Workbook(jxl.Workbook) WritableWorkbook(jxl.write.WritableWorkbook) FileItem(org.apache.commons.fileupload.FileItem) WritableSheet(jxl.write.WritableSheet) Sheet(jxl.Sheet) Cell(jxl.Cell) NumberCell(jxl.NumberCell)

Example 12 with FileItem

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;
            }
        });
    }
}
Also used : InputStream(java.io.InputStream) ServletRequestContext(org.apache.commons.fileupload.servlet.ServletRequestContext) IOException(java.io.IOException) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) FileUploadException(org.apache.commons.fileupload.FileUploadException) FileItem(org.apache.commons.fileupload.FileItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) Part(javax.servlet.http.Part) Iterator(java.util.Iterator) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 13 with FileItem

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);
    }
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) Map(java.util.Map)

Example 14 with FileItem

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);
                    }
                }
            });
        }
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) FileItem(org.apache.commons.fileupload.FileItem) Subsystem(org.osgi.service.subsystem.Subsystem) Map(java.util.Map) File(java.io.File)

Example 15 with FileItem

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()]);
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

FileItem (org.apache.commons.fileupload.FileItem)165 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)78 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)72 FileUploadException (org.apache.commons.fileupload.FileUploadException)59 File (java.io.File)55 IOException (java.io.IOException)51 ArrayList (java.util.ArrayList)40 HashMap (java.util.HashMap)32 ServletException (javax.servlet.ServletException)30 List (java.util.List)27 InputStream (java.io.InputStream)24 FileItemFactory (org.apache.commons.fileupload.FileItemFactory)23 DiskFileItem (org.apache.commons.fileupload.disk.DiskFileItem)16 Map (java.util.Map)15 UnsupportedEncodingException (java.io.UnsupportedEncodingException)12 ServletRequestContext (org.apache.commons.fileupload.servlet.ServletRequestContext)10 Test (org.junit.Test)10 Iterator (java.util.Iterator)9 FileItemWrap (com.github.bordertech.wcomponents.file.FileItemWrap)8 Locale (java.util.Locale)8