Search in sources :

Example 66 with FileItem

use of org.apache.commons.fileupload.FileItem in project wcomponents by BorderTech.

the class WFileWidget method getRequestValue.

/**
 * {@inheritDoc}
 */
@Override
public FileItemWrap getRequestValue(final Request request) {
    if (isPresent(request)) {
        FileItem value = request.getFileItem(getId());
        // No file selected
        if (Util.empty(value.getName()) && value.getSize() == 0) {
            return null;
        }
        FileItemWrap wrapper = new FileItemWrap(value);
        return wrapper;
    } else {
        return getValue();
    }
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) FileItemWrap(com.github.bordertech.wcomponents.file.FileItemWrap)

Example 67 with FileItem

use of org.apache.commons.fileupload.FileItem in project wcomponents by BorderTech.

the class FileUtil_Test method testValidateFileTypeImageFile.

@Test
public void testValidateFileTypeImageFile() throws IOException {
    FileItem newFileItem = createFileItem("/image/x1.gif");
    boolean validateFileType = FileUtil.validateFileType(new FileItemWrap(newFileItem), Arrays.asList("text/plain", "image/gif"));
    Assert.assertTrue(validateFileType);
    newFileItem = createFileItem(null);
    validateFileType = FileUtil.validateFileType(new FileItemWrap(newFileItem), Arrays.asList("text/plain", "image/gif"));
    Assert.assertFalse(validateFileType);
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) MockFileItem(com.github.bordertech.wcomponents.util.mock.MockFileItem) FileItemWrap(com.github.bordertech.wcomponents.file.FileItemWrap) Test(org.junit.Test)

Example 68 with FileItem

use of org.apache.commons.fileupload.FileItem in project wcomponents by BorderTech.

the class FileUtil_Test method testValidateFileTypeAnyFile.

@Test
public void testValidateFileTypeAnyFile() throws IOException {
    boolean validateFileType = FileUtil.validateFileType(null, null);
    Assert.assertFalse(validateFileType);
    FileItem newFileItem = createFileItem(null);
    validateFileType = FileUtil.validateFileType(new FileItemWrap(newFileItem), Collections.EMPTY_LIST);
    Assert.assertTrue(validateFileType);
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) MockFileItem(com.github.bordertech.wcomponents.util.mock.MockFileItem) FileItemWrap(com.github.bordertech.wcomponents.file.FileItemWrap) Test(org.junit.Test)

Example 69 with FileItem

use of org.apache.commons.fileupload.FileItem in project MassBank-web by MassBank.

the class AdminActions method doUpload.

@Action(name = "doUpload", post = true)
public Redirect doUpload(HttpServletRequest req) throws ServletException {
    RequestContext reqContext = new ServletRequestContext(req);
    boolean isMultipart = ServletFileUpload.isMultipartContent(reqContext);
    if (isMultipart) {
        try {
            // Create a factory for disk-based file items
            FileItemFactory factory = new DiskFileItemFactory();
            // Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload(factory);
            List<?> items = upload.parseRequest(req);
            // Process the uploaded items
            Iterator<?> iter = items.iterator();
            while (iter.hasNext()) {
                FileItem item = (FileItem) iter.next();
                if (!item.isFormField()) {
                    String fileName = item.getName();
                    String fileExtesion = fileName;
                    fileExtesion = fileExtesion.toLowerCase();
                    if (!(fileExtesion.endsWith(".jar") || fileExtesion.endsWith(".aar"))) {
                        return new Redirect(UPLOAD).withStatus(false, "Unsupported file type " + fileExtesion);
                    } else {
                        String fileNameOnly;
                        if (fileName.indexOf("\\") < 0) {
                            fileNameOnly = fileName.substring(fileName.lastIndexOf("/") + 1, fileName.length());
                        } else {
                            fileNameOnly = fileName.substring(fileName.lastIndexOf("\\") + 1, fileName.length());
                        }
                        File uploadedFile = new File(serviceDir, fileNameOnly);
                        item.write(uploadedFile);
                        return new Redirect(UPLOAD).withStatus(true, "File " + fileNameOnly + " successfully uploaded");
                    }
                }
            }
        } catch (Exception e) {
            return new Redirect(UPLOAD).withStatus(false, "The following error occurred: " + e.getMessage());
        }
    }
    throw new ServletException("Invalid request");
}
Also used : ServletRequestContext(org.apache.commons.fileupload.servlet.ServletRequestContext) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItemFactory(org.apache.commons.fileupload.FileItemFactory) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) ServletException(javax.servlet.ServletException) ServletException(javax.servlet.ServletException) FileItem(org.apache.commons.fileupload.FileItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) ServletRequestContext(org.apache.commons.fileupload.servlet.ServletRequestContext) RequestContext(org.apache.commons.fileupload.RequestContext) File(java.io.File)

Example 70 with FileItem

use of org.apache.commons.fileupload.FileItem in project MassBank-web by MassBank.

the class FileUpload method doUpload.

/**
 * ファイルアップロード
 * multipart/form-dataに含まれているファイルをアップロードする
 * 失敗した場合はnullを返却する
 * @return アップロードファイル情報MAP<ファイル名, アップロード結果>
 */
@SuppressWarnings("unchecked")
public HashMap<String, Boolean> doUpload() {
    if (fileItemList == null) {
        try {
            fileItemList = (List<FileItem>) parseRequest(req);
        } catch (FileUploadException e) {
            e.printStackTrace();
            return null;
        }
    }
    upFileMap = new HashMap<String, Boolean>();
    for (FileItem fItem : fileItemList) {
        // ファイルフィールドの場合
        if (!fItem.isFormField()) {
            String key = "";
            boolean val = false;
            // ファイル名取得(環境依存でパス情報が含まれる場合がある)
            String filePath = (fItem.getName() != null) ? fItem.getName() : "";
            // ファイル名取得(確実にファイル名のみを取得)
            String fileName = (new File(filePath)).getName();
            int pos = fileName.lastIndexOf("\\");
            fileName = fileName.substring(pos + 1);
            pos = fileName.lastIndexOf("/");
            fileName = fileName.substring(pos + 1);
            // ファイルアップロード
            if (!fileName.equals("")) {
                key = fileName;
                File upFile = new File(outPath + "/" + fileName);
                try {
                    fItem.write(upFile);
                    val = true;
                } catch (Exception e) {
                    e.printStackTrace();
                    upFile.delete();
                }
            }
            upFileMap.put(key, val);
        }
    }
    return upFileMap;
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) File(java.io.File) FileUploadException(org.apache.commons.fileupload.FileUploadException) FileUploadException(org.apache.commons.fileupload.FileUploadException)

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