本文整理汇总了Java中org.apache.commons.fileupload.FileItem.get方法的典型用法代码示例。如果您正苦于以下问题:Java FileItem.get方法的具体用法?Java FileItem.get怎么用?Java FileItem.get使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.fileupload.FileItem
的用法示例。
在下文中一共展示了FileItem.get方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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");
}
示例2: 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;
}
示例3: handleRequestInternal
import org.apache.commons.fileupload.FileItem; //导入方法依赖的package包/类
@RequestMapping(method = RequestMethod.POST)
protected ModelAndView handleRequestInternal(HttpServletRequest request) throws Exception {
String username = securityService.getCurrentUsername(request);
// Check that we have a file upload request.
if (!ServletFileUpload.isMultipartContent(request)) {
throw new Exception("Illegal request.");
}
Map<String, Object> map = new HashMap<String, Object>();
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<?> items = upload.parseRequest(request);
// Look for file items.
for (Object o : items) {
FileItem item = (FileItem) o;
if (!item.isFormField()) {
String fileName = item.getName();
byte[] data = item.get();
if (StringUtils.isNotBlank(fileName) && data.length > 0) {
createAvatar(fileName, data, username, map);
} else {
map.put("error", new Exception("Missing file."));
LOG.warn("Failed to upload personal image. No file specified.");
}
break;
}
}
map.put("username", username);
map.put("avatar", settingsService.getCustomAvatar(username));
return new ModelAndView("avatarUploadResult","model",map);
}
示例4: getMultipartFormParametersMap
import org.apache.commons.fileupload.FileItem; //导入方法依赖的package包/类
private Map<String, Part> getMultipartFormParametersMap() {
if (!ServletFileUpload.isMultipartContent(this)) { // isMultipartContent also checks the content type
return new HashMap<>();
}
Map<String, Part> output = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
try {
List<FileItem> items = upload.parseRequest(this);
for (FileItem item : items) {
AwsProxyRequestPart newPart = new AwsProxyRequestPart(item.get());
newPart.setName(item.getName());
newPart.setSubmittedFileName(item.getFieldName());
newPart.setContentType(item.getContentType());
newPart.setSize(item.getSize());
Iterator<String> headerNamesIterator = item.getHeaders().getHeaderNames();
while (headerNamesIterator.hasNext()) {
String headerName = headerNamesIterator.next();
Iterator<String> headerValuesIterator = item.getHeaders().getHeaders(headerName);
while (headerValuesIterator.hasNext()) {
newPart.addHeader(headerName, headerValuesIterator.next());
}
}
output.put(item.getFieldName(), newPart);
}
} catch (FileUploadException e) {
log.error("Could not read multipart upload file", e);
}
return output;
}
示例5: handleRequestInternal
import org.apache.commons.fileupload.FileItem; //导入方法依赖的package包/类
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
String username = securityService.getCurrentUsername(request);
// Check that we have a file upload request.
if (!ServletFileUpload.isMultipartContent(request)) {
throw new Exception("Illegal request.");
}
Map<String, Object> map = new HashMap<String, Object>();
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<?> items = upload.parseRequest(request);
// Look for file items.
for (Object o : items) {
FileItem item = (FileItem) o;
if (!item.isFormField()) {
String fileName = item.getName();
byte[] data = item.get();
if (StringUtils.isNotBlank(fileName) && data.length > 0) {
createAvatar(fileName, data, username, map);
} else {
map.put("error", new Exception("Missing file."));
LOG.warn("Failed to upload personal image. No file specified.");
}
break;
}
}
map.put("username", username);
map.put("avatar", settingsService.getCustomAvatar(username));
ModelAndView result = super.handleRequestInternal(request, response);
result.addObject("model", map);
return result;
}
示例6: doPost
import org.apache.commons.fileupload.FileItem; //导入方法依赖的package包/类
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
boolean isMultipart = ServletFileUpload.isMultipartContent(req);
HttpSession session = req.getSession(false);
Integer userId = (int)session.getAttribute("id_user");
Integer orderId = (int)session.getAttribute("currentOrder");
PrintWriter out = resp.getWriter();
JsonObject jsonObject = new JsonObject();
if(isMultipart && userId != -1 && orderId != -1){
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// Configure a repository (to ensure a secure temp location is used)
ServletContext servletContext = this.getServletConfig().getServletContext();
FileCleaningTracker tracker = FileCleanerCleanup.getFileCleaningTracker(servletContext);
factory.setFileCleaningTracker(tracker);
File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
factory.setRepository(repository);
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List<FileItem> items = upload.parseRequest(req);
for (FileItem imageFile : items){
byte[] imageByte = imageFile.get();
Order order = new Order();
order.setId(orderId);
Image image = new Image();
image.setDataimage(imageByte);
image.setOrder(order);
new ImageDBManager().create(image);
}
jsonObject.addProperty("success", true);
} catch (FileUploadException e) {
jsonObject.addProperty("success", false);
e.printStackTrace();
}
} else {
jsonObject.addProperty("success", false);
}
out.append(jsonObject.toString());
out.flush();
}