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();
}
}
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);
}
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);
}
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");
}
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;
}
Aggregations