本文整理匯總了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 "";
}