本文整理汇总了Java中org.apache.commons.fileupload.FileItem类的典型用法代码示例。如果您正苦于以下问题:Java FileItem类的具体用法?Java FileItem怎么用?Java FileItem使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
FileItem类属于org.apache.commons.fileupload包,在下文中一共展示了FileItem类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: upload
import org.apache.commons.fileupload.FileItem; //导入依赖的package包/类
/**
* 文件上传处理类
*
* @return {@link UploadResult}
* @throws Exception 抛出异常
*/
public UploadResult upload() throws Exception {
boolean multipartContent = ServletFileUpload.isMultipartContent(request);
if (!multipartContent) {
throw new RuntimeException("上传表单enctype类型设置错误");
}
List<TextFormResult> textFormResultList = Lists.newArrayList();
List<FileFormResult> fileFormResultList = Lists.newArrayList();
List<FileItem> fileItems = upload.parseRequest(request);
for (FileItem item : fileItems) {
if (item.isFormField())
textFormResultList.add(processFormField(item));
else
fileFormResultList.add(processUploadedFile(item));
}
return new UploadResult(textFormResultList, fileFormResultList);
}
示例2: handle
import org.apache.commons.fileupload.FileItem; //导入依赖的package包/类
@Override
public void handle( List<FileItem> fileItems )
{
for ( FileItem file : fileItems )
{
byte[] fileName = file.getName().getBytes();
try
{
String uploadName = new String( fileName, "utf-8" );
System.out.println( uploadName );
File writeFile = new File( "D:\\" + uploadName );
file.write( writeFile );
} catch ( Exception e )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
示例3: getHashtable
import org.apache.commons.fileupload.FileItem; //导入依赖的package包/类
public Hashtable getHashtable(HttpServletRequest request)
throws FileUploadException, IOException {
Hashtable ht = new Hashtable();
DiskFileUpload upload = new DiskFileUpload();
List fileItems = upload.parseRequest(request);
Iterator iter = fileItems.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
ht.put(item.getFieldName(), item.getString());
} else {
if (item.getName().equals("")) {
//ht.put(item.getFieldName(), null);
} else if (item.getSize() == 0) {
//ht.put(item.getFieldName(), null);
} else {
ht.put(item.getFieldName(), item.getInputStream());
}
}
}
return ht;
}
示例4: excelToArrayList
import org.apache.commons.fileupload.FileItem; //导入依赖的package包/类
/**
* 获取Excel数据,返回List
*
* @param sheetNumber
* 读取工作表的下标(从1开始).可有可无,默认读取所有表单.
*/
public static final List<String[]> excelToArrayList(String fileName, FileItem fileItem, int... sheetNumber)
throws Exception {
List<String[]> resultList = null;
InputStream is = null;
try {
is = fileItem.getInputStream();
resultList = excelToArrayList(fileName, is, sheetNumber);
} catch (Exception e) {
throw e;
} finally {
if (is != null) {
is.close();
}
}
return resultList;
}
示例5: process
import org.apache.commons.fileupload.FileItem; //导入依赖的package包/类
@Override
public void process(Exchange exchange) throws Exception {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
ExchangeFileUpload upload = new ExchangeFileUpload(factory);
java.util.List<FileItem> items = upload.parseExchange(exchange);
if(items.size() >= 1){
exchange.getIn().setBody(items.get(0).getInputStream());
for (int i = 1; i < items.size(); i++) {
exchange.setProperty(items.get(i).getName(), items.get(i).getInputStream());
}
}
}
示例6: excelToArrayList
import org.apache.commons.fileupload.FileItem; //导入依赖的package包/类
/**
* 获取Excel数据,返回List<String[]>
*
* @param sheetNumber
* 读取工作表的下标(从1开始).可有可无,默认读取所有表单.
*/
public static final List<String[]> excelToArrayList(String fileName, FileItem fileItem, int... sheetNumber)
throws Exception {
List<String[]> resultList = null;
InputStream is = null;
try {
is = fileItem.getInputStream();
resultList = excelToArrayList(fileName, is, sheetNumber);
} catch (Exception e) {
throw e;
} finally {
if (is != null) {
is.close();
}
}
return resultList;
}
示例7: importProject
import org.apache.commons.fileupload.FileItem; //导入依赖的package包/类
public void importProject(HttpServletRequest req, HttpServletResponse resp) throws Exception {
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletContext servletContext = req.getSession().getServletContext();
File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
factory.setRepository(repository);
ServletFileUpload upload = new ServletFileUpload(factory);
InputStream inputStream=null;
boolean overwriteProject=true;
List<FileItem> items = upload.parseRequest(req);
if(items.size()==0){
throw new ServletException("Upload file is invalid.");
}
for(FileItem item:items){
String name=item.getFieldName();
if(name.equals("overwriteProject")){
String overwriteProjectStr=new String(item.get());
overwriteProject=Boolean.valueOf(overwriteProjectStr);
}else if(name.equals("file")){
inputStream=item.getInputStream();
}
}
repositoryService.importXml(inputStream,overwriteProject);
IOUtils.closeQuietly(inputStream);
resp.sendRedirect(req.getContextPath()+"/urule/frame");
}
示例8: importExcelTemplate
import org.apache.commons.fileupload.FileItem; //导入依赖的package包/类
public void importExcelTemplate(HttpServletRequest req, HttpServletResponse resp) throws Exception {
DiskFileItemFactory factory=new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = upload.parseRequest(req);
Iterator<FileItem> itr = items.iterator();
List<Map<String,Object>> mapData=null;
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
String name=item.getFieldName();
if(!name.equals("file")){
continue;
}
InputStream stream=item.getInputStream();
mapData=parseExcel(stream);
httpSessionKnowledgeCache.put(req, IMPORT_EXCEL_DATA, mapData);
stream.close();
break;
}
httpSessionKnowledgeCache.put(req, IMPORT_EXCEL_DATA, mapData);
writeObjectToJson(resp, mapData);
}
示例9: getServiceResult
import org.apache.commons.fileupload.FileItem; //导入依赖的package包/类
protected void getServiceResult(HttpServletRequest request, Document document) throws Exception {
// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart)
throw new IllegalArgumentException("Not multipart content!");
FileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Parse the request
List<FileItem> items = GenericUtils.cast(upload.parseRequest(request));
// Process the uploaded items
handleFormFields(request);
for (FileItem item : items) {
doUpload(request, document, item);
}
}
示例10: upload
import org.apache.commons.fileupload.FileItem; //导入依赖的package包/类
@Override
public void upload(HttpServletRequest request, HttpServletResponse response) {
try {
serviceHelper.setCors(request, response);
OutputStream outputStream = serviceHelper.setContext(request, response, URI);
List<UploadReader> readers = new ArrayList<>();
for (FileItem item : getUpload(request).parseRequest(request))
if (!item.isFormField())
readers.add(new HttpUploadReader(item));
outputStream.write(json.toBytes(uploadService.uploads(readers)));
outputStream.flush();
outputStream.close();
} catch (Throwable e) {
logger.warn(e, "处理文件上传时发生异常!");
} finally {
closables.close();
}
}
示例11: makeTempFromFileItem
import org.apache.commons.fileupload.FileItem; //导入依赖的package包/类
/**
* {@inheritDoc}
* <pre>
* 一時ファイルの保存処理ですが、性能を考慮し本来のフォルダに保存します。
* </pre>
*/
@Override
public File makeTempFromFileItem(final FileItem fileItem) throws Exception {
this.fileName = FileUtil.getFileName(fileItem.getName());
File file = this.makeUniqFile();
FileOutputStream os = new FileOutputStream(file);
try {
InputStream is = fileItem.getInputStream();
try {
FileUtil.copyStream(is, os);
} finally {
is.close();
}
} finally {
os.close();
}
return file;
}
示例12: uploadFile
import org.apache.commons.fileupload.FileItem; //导入依赖的package包/类
/**
* 文件上传
*
* @param request
* @return infos info[0] 验证文件域返回错误信息 info[1] 上传文件错误信息 info[2] savePath
* info[3] saveUrl info[4] fileUrl
*/
@SuppressWarnings("unchecked")
public String[] uploadFile(HttpServletRequest request) {
String[] infos = new String[5];
// 验证
infos[0] = this.validateFields(request);
// 初始化表单元素
Map<String, Object> fieldsMap = new HashMap<String, Object>();
if (infos[0].equals("true")) {
fieldsMap = this.initFields(request);
}
// 上传
List<FileItem> fiList = (List<FileItem>) fieldsMap.get(UploadUtils.FILE_FIELDS);
if (fiList != null) {
for (FileItem item : fiList) {
infos[1] = this.saveFile(item);
}
infos[2] = savePath;
infos[3] = saveUrl;
infos[4] = fileUrl;
}
return infos;
}
示例13: map
import org.apache.commons.fileupload.FileItem; //导入依赖的package包/类
public Map<String, Object> map(HttpServletRequest request) {
ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
upload.setSizeMax(20 * 1024);
upload.setFileSizeMax(10 * 1024);
List<FileItem> items;
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
throw new RequestMappingException("", e);
}
return items.stream().map(item -> {
String key = item.getFieldName();
if (item.isFormField()) {
String value = item.getString();
return new SimpleKeyValue<String, Object>(key, value);
} else {
return new SimpleKeyValue<String, Object>(key, item);
}
}).collect(Collectors.toMap(
SimpleKeyValue::getKey,
SimpleKeyValue::getValue
));
}
示例14: makeTempFromFileItem
import org.apache.commons.fileupload.FileItem; //导入依赖的package包/类
/**
* {@inheritDoc}
* <pre>
* BLOBに保存するためヘッダ情報 + ファイル本体の形式の一時ファイルを作成します。
* </pre>
*/
@Override
protected File makeTempFromFileItem(final FileItem fileItem) throws Exception {
// log.error("makeTempFromFileItem", new Exception());
String fileName = FileUtil.getFileName(fileItem.getName());
long length = fileItem.getSize();
File file = null;
InputStream is = fileItem.getInputStream();
try {
file = this.makeBlobTempFile(fileName, length, is);
} finally {
is.close();
}
// this.tempFile = file;
return file;
}
示例15: getMultiParameter
import org.apache.commons.fileupload.FileItem; //导入依赖的package包/类
/**
* Retrieve a parameter passed to the servlet as part of a multi part content.
*
* @param parameter
* the name of the parameter to be retrieved
* @param requestFilter
* filter used to validate the input against list of allowed inputs
* @return String containing the value of the parameter. Empty string if the content is not
* multipart or the parameter is not found.
*/
public String getMultiParameter(String parameter, RequestFilter requestFilter) {
if (!isMultipart || items == null)
return "";
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
FileItem item = iter.next();
if (item.isFormField() && item.getFieldName().equals(parameter)) {
try {
String value = item.getString("UTF-8");
filterRequest(requestFilter, value);
return value;
} catch (Exception ex) {
ex.printStackTrace();
return "";
}
}
}
return "";
}