本文整理匯總了Java中org.apache.commons.fileupload.FileItem.getContentType方法的典型用法代碼示例。如果您正苦於以下問題:Java FileItem.getContentType方法的具體用法?Java FileItem.getContentType怎麽用?Java FileItem.getContentType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.fileupload.FileItem
的用法示例。
在下文中一共展示了FileItem.getContentType方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: FileData
import org.apache.commons.fileupload.FileItem; //導入方法依賴的package包/類
FileData(FileItem item)
{
this.fileName = item.getName();
this.type = item.getContentType();
this.data = item.get();
if(fileName != null)
{
int i = fileName.lastIndexOf("/");
if(i>0)
{
fileName = fileName.substring(i+1);
}
else
{
i = fileName.lastIndexOf("\\");
if(i>0)
{
fileName = fileName.substring(i+1);
}
}
}
this.item = item;
}
示例2: HttpUploadReader
import org.apache.commons.fileupload.FileItem; //導入方法依賴的package包/類
HttpUploadReader(FileItem item) throws IOException {
this.item = item;
fieldName = item.getFieldName();
fileName = item.getName();
contentType = item.getContentType();
size = item.getSize();
inputStream = item.getInputStream();
}
示例3: createParam
import org.apache.commons.fileupload.FileItem; //導入方法依賴的package包/類
/**
* 創建請求對象
*/
public static Param createParam(HttpServletRequest request) throws IOException {
List<FormParam> formParamList = new ArrayList<FormParam>();
List<FileParam> fileParamList = new ArrayList<FileParam>();
try{
Map<String, List<FileItem>> fileItemListMap = servletFileUpload.parseParameterMap(request);
if (CollectionUtil.isNotEmpty(fileItemListMap)) {
for (Map.Entry<String, List<FileItem>> fileItemListEntry : fileItemListMap.entrySet()) {
String fieldName = fileItemListEntry.getKey();
List<FileItem> fileItemList = fileItemListEntry.getValue();
if (CollectionUtil.isNotEmpty(fileItemList)) {
for (FileItem fileItem : fileItemList) {
if(fileItem.isFormField()){
String fieldValue = fileItem.getString("UTF-8");
formParamList.add(new FormParam(fieldName, fieldValue));
} else{
String fileName = FileUtil.getRealFileName(new String(fileItem.getName().getBytes(), "UTF-8"));
if (StringUtil.isNotEmpty(fileName)) {
long fileSize = fileItem.getSize();
String contentType = fileItem.getContentType();
InputStream inputSteam = fileItem.getInputStream();
fileParamList.add(new FileParam(fieldName, fileName, fileSize, contentType, inputSteam));
}
}
}
}
}
}
}catch (FileUploadException e){
LOGGER.error("create param failure", e);
throw new RuntimeException(e);
}
return new Param(formParamList, fileParamList);
}
示例4: createMultipartParamList
import org.apache.commons.fileupload.FileItem; //導入方法依賴的package包/類
public static List<Object> createMultipartParamList(HttpServletRequest request) throws Exception {
// 定義參數列表
List<Object> paramList = new ArrayList<Object>();
// 創建兩個對象,分別對應 普通字段 與 文件字段
Map<String, String> fieldMap = new HashMap<String, String>();
List<Multipart> multipartList = new ArrayList<Multipart>();
// 獲取並遍曆表單項
List<FileItem> fileItemList;
try {
fileItemList = fileUpload.parseRequest(request);
} catch (FileUploadBase.FileSizeLimitExceededException e) {
// 異常轉換(拋出自定義異常)
throw new UploadException(e);
}
for (FileItem fileItem : fileItemList) {
// 分兩種情況處理表單項
String fieldName = fileItem.getFieldName();
if (fileItem.isFormField()) {
// 處理普通字段
String fieldValue = fileItem.getString(FrameworkConstant.DEFAULT_CHARSET);
fieldMap.put(fieldName, fieldValue);
} else {
// 處理文件字段
String originalFileName = FileUtil.getRealFileName(fileItem.getName());
String uploadedFileName = FileUtil.getEncodedFileName(originalFileName);
String contentType = fileItem.getContentType();
long fileSize = fileItem.getSize();
InputStream inputSteam = fileItem.getInputStream();
// 創建 Multipart 對象,並將其添加到 multipartList 中
Multipart multipart = new Multipart(uploadedFileName, contentType, fileSize, inputSteam);
multipartList.add(multipart);
// 將所上傳文件的文件名存入 fieldMap 中
fieldMap.put(fieldName, uploadedFileName);
}
}
// 初始化參數列表
paramList.add(fieldMap);
if (multipartList.size() > 1) {
paramList.add(multipartList);
} else if (multipartList.size() == 1) {
paramList.add(multipartList.get(0));
} else {
paramList.add(null);
}
// 返回參數列表
return paramList;
}
示例5: getFileParameterValue
import org.apache.commons.fileupload.FileItem; //導入方法依賴的package包/類
/**
* Responsible for constructing a FileBean object for the named file parameter. If there is no
* file parameter with the specified name this method should return null.
*
* @param name the name of the file parameter
* @return a FileBean object wrapping the uploaded file
*/
public FileBean getFileParameterValue(String name) {
final FileItem item = this.files.get(name);
if (item == null
|| ((item.getName() == null || item.getName().length() == 0) && item.getSize() == 0)) {
return null;
}
else {
// Attempt to ensure the file name is just the basename with no path included
String filename = item.getName();
int index;
if (WINDOWS_PATH_PREFIX_PATTERN.matcher(filename).find())
index = filename.lastIndexOf('\\');
else
index = filename.lastIndexOf('/');
if (index >= 0 && index + 1 < filename.length() - 1)
filename = filename.substring(index + 1);
// Use an anonymous inner subclass of FileBean that overrides all the
// methods that rely on having a File present, to use the FileItem
// created by commons upload instead.
return new FileBean(null, item.getContentType(), filename, this.charset) {
@Override public long getSize() { return item.getSize(); }
@Override public InputStream getInputStream() throws IOException {
return item.getInputStream();
}
@Override public void save(File toFile) throws IOException {
try {
item.write(toFile);
delete();
}
catch (Exception e) {
if (e instanceof IOException) throw (IOException) e;
else {
IOException ioe = new IOException("Problem saving uploaded file.");
ioe.initCause(e);
throw ioe;
}
}
}
@Override
public void delete() throws IOException { item.delete(); }
};
}
}